mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 07:12:47 +08:00
Merge branch 'master' of https://github.com/top-think/framework
This commit is contained in:
@@ -121,7 +121,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
|||||||
* 通过使用用户自定义函数,以字符串返回数组
|
* 通过使用用户自定义函数,以字符串返回数组
|
||||||
*
|
*
|
||||||
* @param callable $callback
|
* @param callable $callback
|
||||||
* @param mixed $initial
|
* @param mixed $initial
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function reduce(callable $callback, $initial = null)
|
public function reduce(callable $callback, $initial = null)
|
||||||
@@ -149,6 +149,132 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
|||||||
return array_shift($this->items);
|
return array_shift($this->items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把一个数组分割为新的数组块.
|
||||||
|
*
|
||||||
|
* @param int $size
|
||||||
|
* @param bool $preserveKeys
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function chunk($size, $preserveKeys = false)
|
||||||
|
{
|
||||||
|
$chunks = [];
|
||||||
|
|
||||||
|
foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
|
||||||
|
$chunks[] = new static($chunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new static($chunks);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在数组开头插入一个元素
|
||||||
|
* @param mixed $value
|
||||||
|
* @param null $key
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function unshift($value, $key = null)
|
||||||
|
{
|
||||||
|
if (is_null($key)) {
|
||||||
|
array_unshift($this->items, $value);
|
||||||
|
} else {
|
||||||
|
$this->items = [$key => $value] + $this->items;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给每个元素执行个回调
|
||||||
|
*
|
||||||
|
* @param callable $callback
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function each(callable $callback)
|
||||||
|
{
|
||||||
|
foreach ($this->items as $key => $item) {
|
||||||
|
if ($callback($item, $key) === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用回调函数过滤数组中的元素
|
||||||
|
* @param callable|null $callback
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function filter(callable $callback = null)
|
||||||
|
{
|
||||||
|
if ($callback) {
|
||||||
|
return new static(array_filter($this->items, $callback));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new static(array_filter($this->items));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回数组中指定的一列
|
||||||
|
* @param $column_key
|
||||||
|
* @param null $index_key
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function column($column_key, $index_key = null)
|
||||||
|
{
|
||||||
|
if (function_exists('array_column')) {
|
||||||
|
return array_column($this->items, $column_key, $index_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
foreach ($this->items as $row) {
|
||||||
|
$key = $value = null;
|
||||||
|
$keySet = $valueSet = false;
|
||||||
|
if ($index_key !== null && array_key_exists($index_key, $row)) {
|
||||||
|
$keySet = true;
|
||||||
|
$key = (string)$row[$index_key];
|
||||||
|
}
|
||||||
|
if ($column_key === null) {
|
||||||
|
$valueSet = true;
|
||||||
|
$value = $row;
|
||||||
|
} elseif (is_array($row) && array_key_exists($column_key, $row)) {
|
||||||
|
$valueSet = true;
|
||||||
|
$value = $row[$column_key];
|
||||||
|
}
|
||||||
|
if ($valueSet) {
|
||||||
|
if ($keySet) {
|
||||||
|
$result[$key] = $value;
|
||||||
|
} else {
|
||||||
|
$result[] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对数组排序
|
||||||
|
*
|
||||||
|
* @param callable|null $callback
|
||||||
|
* @return static
|
||||||
|
*/
|
||||||
|
public function sort(callable $callback = null)
|
||||||
|
{
|
||||||
|
$items = $this->items;
|
||||||
|
|
||||||
|
$callback ? uasort($items, $callback) : uasort($items, function ($a, $b) {
|
||||||
|
|
||||||
|
if ($a == $b) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ($a < $b) ? -1 : 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
return new static($items);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将数组打乱
|
* 将数组打乱
|
||||||
@@ -167,8 +293,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
|||||||
/**
|
/**
|
||||||
* 截取数组
|
* 截取数组
|
||||||
*
|
*
|
||||||
* @param int $offset
|
* @param int $offset
|
||||||
* @param int $length
|
* @param int $length
|
||||||
* @param bool $preserveKeys
|
* @param bool $preserveKeys
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -150,22 +150,26 @@ class Url
|
|||||||
if (Config::get('url_domain_deploy')) {
|
if (Config::get('url_domain_deploy')) {
|
||||||
// 根域名
|
// 根域名
|
||||||
$urlDomainRoot = Config::get('url_domain_root');
|
$urlDomainRoot = Config::get('url_domain_root');
|
||||||
foreach (Route::domain() as $key => $rule) {
|
$route_domain = array_keys(Route::domain());
|
||||||
$rule = is_array($rule) ? $rule[0] : $rule;
|
foreach($route_domain as $domain_prefix) {
|
||||||
if (false === strpos($key, '*') && 0 === strpos($url, $rule)) {
|
if(strpos($domain, trim($domain_prefix, '*.')) !== false) {
|
||||||
$url = ltrim($url, $rule);
|
foreach (Route::domain() as $key => $rule) {
|
||||||
$domain = $key;
|
$rule = is_array($rule) ? $rule[0] : $rule;
|
||||||
// 生成对应子域名
|
if (false === strpos($key, '*') && 0 === strpos($url, $rule)) {
|
||||||
if(!empty($urlDomainRoot)){
|
$url = ltrim($url, $rule);
|
||||||
$domain .= $urlDomainRoot;
|
$domain = $key;
|
||||||
|
// 生成对应子域名
|
||||||
|
if(!empty($urlDomainRoot)){
|
||||||
|
$domain .= $urlDomainRoot;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}else if(false !== strpos($key, '*')){
|
||||||
|
if(!empty($urlDomainRoot)){
|
||||||
|
$domain .= $urlDomainRoot;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}else if(false !== strpos($key, '*')){
|
|
||||||
$domain = str_replace('*',strstr($domain,'.',true),$key);
|
|
||||||
if(!empty($urlDomainRoot)){
|
|
||||||
$domain .= $urlDomainRoot;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,10 +98,11 @@ class Cx extends Taglib
|
|||||||
} else {
|
} else {
|
||||||
$name = $this->autoBuildVar($name);
|
$name = $this->autoBuildVar($name);
|
||||||
}
|
}
|
||||||
$parseStr .= 'if(is_array(' . $name . ')): $' . $key . ' = 0;';
|
|
||||||
|
$parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection): $' . $key . ' = 0;';
|
||||||
// 设置了输出数组长度
|
// 设置了输出数组长度
|
||||||
if (0 != $offset || 'null' != $length) {
|
if (0 != $offset || 'null' != $length) {
|
||||||
$parseStr .= '$__LIST__ = array_slice(' . $name . ',' . $offset . ',' . $length . ', true); ';
|
$parseStr .= '$__LIST__ = is_array(' . $name . ') ? array_slice(' . $name . ',' . $offset . ',' . $length . ', true) : ' . $name . '->slice(' . $offset . ',' . $length . ', true); ';
|
||||||
} else {
|
} else {
|
||||||
$parseStr .= ' $__LIST__ = ' . $name . ';';
|
$parseStr .= ' $__LIST__ = ' . $name . ';';
|
||||||
}
|
}
|
||||||
@@ -144,6 +145,7 @@ class Cx extends Taglib
|
|||||||
$name = $tag['name'];
|
$name = $tag['name'];
|
||||||
$key = !empty($tag['key']) ? $tag['key'] : 'key';
|
$key = !empty($tag['key']) ? $tag['key'] : 'key';
|
||||||
$item = !empty($tag['id']) ? $tag['id'] : $tag['item'];
|
$item = !empty($tag['id']) ? $tag['id'] : $tag['item'];
|
||||||
|
$empty = isset($tag['empty']) ? $tag['empty'] : '';
|
||||||
$offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0;
|
$offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0;
|
||||||
$length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 'null';
|
$length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 'null';
|
||||||
|
|
||||||
@@ -157,16 +159,20 @@ class Cx extends Taglib
|
|||||||
} else {
|
} else {
|
||||||
$name = $this->autoBuildVar($name);
|
$name = $this->autoBuildVar($name);
|
||||||
}
|
}
|
||||||
$parseStr .= 'if(is_array(' . $name . ')): ';
|
$parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection): ';
|
||||||
// 设置了输出数组长度
|
// 设置了输出数组长度
|
||||||
if (0 != $offset || 'null' != $length) {
|
if (0 != $offset || 'null' != $length) {
|
||||||
if (!isset($var)) {
|
if (!isset($var)) {
|
||||||
$var = '$_' . uniqid();
|
$var = '$_' . uniqid();
|
||||||
}
|
}
|
||||||
$parseStr .= $var . ' = array_slice(' . $name . ',' . $offset . ',' . $length . ', true); ';
|
$parseStr .= $var . ' = is_array(' . $name . ') ? array_slice(' . $name . ',' . $offset . ',' . $length . ', true) : ' . $name . '->slice(' . $offset . ',' . $length . ', true); ';
|
||||||
} else {
|
} else {
|
||||||
$var = &$name;
|
$var = &$name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$parseStr .= 'if( count(' . $var . ')==0 ) : echo "' . $empty . '" ;';
|
||||||
|
$parseStr .= 'else: ';
|
||||||
|
|
||||||
// 设置了索引项
|
// 设置了索引项
|
||||||
if (isset($tag['index'])) {
|
if (isset($tag['index'])) {
|
||||||
$index = $tag['index'];
|
$index = $tag['index'];
|
||||||
@@ -185,11 +191,7 @@ class Cx extends Taglib
|
|||||||
$parseStr .= '?>';
|
$parseStr .= '?>';
|
||||||
// 循环体中的内容
|
// 循环体中的内容
|
||||||
$parseStr .= $content;
|
$parseStr .= $content;
|
||||||
$parseStr .= '<?php endforeach; endif; ?>';
|
$parseStr .= '<?php endforeach; endif; else: echo "' . $empty . '" ;endif; ?>';
|
||||||
// 设置了数组为空时的显示内容
|
|
||||||
if (isset($tag['empty'])) {
|
|
||||||
$parseStr .= '<?php if(empty(' . $var . ')): echo \'"' . $tag['empty'] . '\'; endif; ?>';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!empty($parseStr)) {
|
if (!empty($parseStr)) {
|
||||||
return $parseStr;
|
return $parseStr;
|
||||||
@@ -430,7 +432,7 @@ class Cx extends Taglib
|
|||||||
{
|
{
|
||||||
$name = $tag['name'];
|
$name = $tag['name'];
|
||||||
$name = $this->autoBuildVar($name);
|
$name = $this->autoBuildVar($name);
|
||||||
$parseStr = '<?php if(empty(' . $name . ')): ?>' . $content . '<?php endif; ?>';
|
$parseStr = '<?php if(empty(' . $name . ') || (' . $name . ' instanceof \think\Collection && ' . $name . '->isEmpty())): ?>' . $content . '<?php endif; ?>';
|
||||||
return $parseStr;
|
return $parseStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -447,7 +449,7 @@ class Cx extends Taglib
|
|||||||
{
|
{
|
||||||
$name = $tag['name'];
|
$name = $tag['name'];
|
||||||
$name = $this->autoBuildVar($name);
|
$name = $this->autoBuildVar($name);
|
||||||
$parseStr = '<?php if(!empty(' . $name . ')): ?>' . $content . '<?php endif; ?>';
|
$parseStr = '<?php if(!(empty(' . $name . ') || (' . $name . ' instanceof \think\Collection && ' . $name . '->isEmpty()))): ?>' . $content . '<?php endif; ?>';
|
||||||
return $parseStr;
|
return $parseStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user