您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天TP5-20161223|thinkphp5-Collection.php

2016-11-15 08:34 351 查看
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------

namespace think;

use ArrayAccess;
use ArrayIterator;
use Countable;
use IteratorAggregate;
use JsonSerializable;
// 收集系列
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
{// 哥哥,你这个是要重写 系统函数,牛叉
// 这个静态方法 用的好,真心的好,牛叉,
protected $items = [];// 条目记录

public function __construct($items = [])
{
$this->items = $this->convertToArray($items);// 转换数组格式 存入 数组 条目 仓库
}

public static function make($items = [])// 这个里面的 make 不错,有点 跟linux 的节奏
{
return new static($items);// 返回 自身 实例化, 应该是不会被 继承者子类 误判,很牛叉 静态方法 才可以
}

/**
* 是否为空
* @return bool
*/
public function isEmpty()
{
return empty($this->items);// 是否为空,典型判断的封装函数
}

public function toArray()
{
return array_map(function ($value) {
return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value;
}, $this->items);
}// 太经典的 写法了, 遍历数组, 转换 内部数据类型到数组,符合标准的数组

public function all()
{
return $this->items;
}// 返回全部数据

/**
* 合并数组
*
* @param  mixed $items
* @return static
*/
public function merge($items)
{
return new static(array_merge($this->items, $this->convertToArray($items)));
}// 合并数组,并返回 静态对象 的方式 注册 数据到 静态对象 很神奇的用法

/**
* 比较数组,返回差集
*
* @param  mixed $items
* @return static
*/
public function diff($items)
{
return new static(array_diff($this->items, $this->convertToArray($items)));
}// 存放 差集

/**
* 交换数组中的键和值
*
* @return static
*/
public function flip()
{
return new static(array_flip($this->items));
}// 存放 交换的 key 跟 value

/**
* 比较数组,返回交集
*
* @param  mixed $items
* @return static
*/
public function intersect($items)
{
return new static(array_intersect($this->items, $this->convertToArray($items)));
}// 交集

/**
* 返回数组中所有的键名
*
* @return static
*/
public function keys()
{
return new static(array_keys($this->items));
}// 存取key

/**
* 删除数组的最后一个元素(出栈)
*
* @return mixed
*/
public function pop()
{
return array_pop($this->items);
}// 删除 最后元素 出栈

/**
* 通过使用用户自定义函数,以字符串返回数组
*
* @param  callable $callback
* @param  mixed    $initial
* @return mixed
*/
public function reduce(callable $callback, $initial = null)
{
return array_reduce($this->items, $callback, $initial);
}// 自定义函数 调用,处理 数据

/**
* 以相反的顺序返回数组。
*
* @return static
*/
public function reverse()
{
return new static(array_reverse($this->items));
}// 反向 排序

/**
* 删除数组中首个元素,并返回被删除元素的值
*
* @return mixed
*/
public function shift()
{
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 (null !== $index_key && array_key_exists($index_key, $row)) {
$keySet = true;
$key    = (string)$row[$index_key];
}
if (null === $column_key) {
$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);
}// 排序数组

/**
* 将数组打乱
*
* @return static
*/
public function shuffle()
{
$items = $this->items;

shuffle($items);

return new static($items);
}// 打乱数组

/**
* 截取数组
*
* @param  int  $offset
* @param  int  $length
* @param  bool $preserveKeys
* @return static
*/
public function slice($offset, $length = null, $preserveKeys = false)
{
return new static(array_slice($this->items, $offset, $length, $preserveKeys));
}// 截取数组

// ArrayAccess
public function offsetExists($offset)
{
return array_key_exists($offset, $this->items);
}// 验证存在

public function offsetGet($offset)
{
return $this->items[$offset];
}// 偏移量

public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->items[] = $value;
} else {
$this->items[$offset] = $value;
}
}// 设置便宜

public function offsetUnset($offset)
{
unset($this->items[$offset]);
}// 删除偏移

//Countable
public function count()
{
return count($this->items);
}// 统计

//IteratorAggregate
public function getIterator()
{
return new ArrayIterator($this->items);
}// 获取信息

//JsonSerializable
public function jsonSerialize()
{
return $this->toArray();
}// Json 序列化

/**
* 转换当前数据集为JSON字符串
* @access public
* @param integer $options json参数
* @return string
*/
public function toJson($options = JSON_UNESCAPED_UNICODE)
{
return json_encode($this->toArray(), $options);
}// 转化称为 json

public function __toString()
{
return $this->toJson();
}// 转成 字符串

/**
* 转换成数组
*
* @param  mixed $items
* @return array
*/
protected function convertToArray($items)
{
if ($items instanceof self) {
return $items->all();
}
return (array)$items;
}// 转化成数组
}
// 1 牛叉哄哄的继承了系统接口
// 2 出色的利用的 static 的 特性,及 new static 的方法的区别,进行的完美的诠释。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp php