mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
修正模板标签解析
新增几个常用的数组处理方法
This commit is contained in:
@@ -121,7 +121,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
* 通过使用用户自定义函数,以字符串返回数组
|
||||
*
|
||||
* @param callable $callback
|
||||
* @param mixed $initial
|
||||
* @param mixed $initial
|
||||
* @return mixed
|
||||
*/
|
||||
public function reduce(callable $callback, $initial = null)
|
||||
@@ -149,6 +149,132 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
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 $length
|
||||
* @param int $offset
|
||||
* @param int $length
|
||||
* @param bool $preserveKeys
|
||||
* @return static
|
||||
*/
|
||||
|
||||
@@ -98,10 +98,11 @@ class Cx extends Taglib
|
||||
} else {
|
||||
$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) {
|
||||
$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 {
|
||||
$parseStr .= ' $__LIST__ = ' . $name . ';';
|
||||
}
|
||||
@@ -144,6 +145,7 @@ class Cx extends Taglib
|
||||
$name = $tag['name'];
|
||||
$key = !empty($tag['key']) ? $tag['key'] : 'key';
|
||||
$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;
|
||||
$length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 'null';
|
||||
|
||||
@@ -157,16 +159,20 @@ class Cx extends Taglib
|
||||
} else {
|
||||
$name = $this->autoBuildVar($name);
|
||||
}
|
||||
$parseStr .= 'if(is_array(' . $name . ')): ';
|
||||
$parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection): ';
|
||||
// 设置了输出数组长度
|
||||
if (0 != $offset || 'null' != $length) {
|
||||
if (!isset($var)) {
|
||||
$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 {
|
||||
$var = &$name;
|
||||
}
|
||||
|
||||
$parseStr .= 'if( count(' . $var . ')==0 ) : echo "' . $empty . '" ;';
|
||||
$parseStr .= 'else: ';
|
||||
|
||||
// 设置了索引项
|
||||
if (isset($tag['index'])) {
|
||||
$index = $tag['index'];
|
||||
@@ -185,11 +191,7 @@ class Cx extends Taglib
|
||||
$parseStr .= '?>';
|
||||
// 循环体中的内容
|
||||
$parseStr .= $content;
|
||||
$parseStr .= '<?php endforeach; endif; ?>';
|
||||
// 设置了数组为空时的显示内容
|
||||
if (isset($tag['empty'])) {
|
||||
$parseStr .= '<?php if(empty(' . $var . ')): echo \'"' . $tag['empty'] . '\'; endif; ?>';
|
||||
}
|
||||
$parseStr .= '<?php endforeach; endif; else: echo "' . $empty . '" ;endif; ?>';
|
||||
|
||||
if (!empty($parseStr)) {
|
||||
return $parseStr;
|
||||
@@ -430,7 +432,7 @@ class Cx extends Taglib
|
||||
{
|
||||
$name = $tag['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;
|
||||
}
|
||||
|
||||
@@ -447,7 +449,7 @@ class Cx extends Taglib
|
||||
{
|
||||
$name = $tag['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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user