Collection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: zhangyajun <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think;
  12. use ArrayAccess;
  13. use ArrayIterator;
  14. use Countable;
  15. use IteratorAggregate;
  16. use JsonSerializable;
  17. class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
  18. {
  19. /**
  20. * @var array 数据
  21. */
  22. protected $items = [];
  23. /**
  24. * Collection constructor.
  25. * @access public
  26. * @param array $items 数据
  27. */
  28. public function __construct($items = [])
  29. {
  30. $this->items = $this->convertToArray($items);
  31. }
  32. /**
  33. * 创建 Collection 实例
  34. * @access public
  35. * @param array $items 数据
  36. * @return static
  37. */
  38. public static function make($items = [])
  39. {
  40. return new static($items);
  41. }
  42. /**
  43. * 判断数据是否为空
  44. * @access public
  45. * @return bool
  46. */
  47. public function isEmpty()
  48. {
  49. return empty($this->items);
  50. }
  51. /**
  52. * 将数据转成数组
  53. * @access public
  54. * @return array
  55. */
  56. public function toArray()
  57. {
  58. return array_map(function ($value) {
  59. return ($value instanceof Model || $value instanceof self) ?
  60. $value->toArray() :
  61. $value;
  62. }, $this->items);
  63. }
  64. /**
  65. * 获取全部的数据
  66. * @access public
  67. * @return array
  68. */
  69. public function all()
  70. {
  71. return $this->items;
  72. }
  73. /**
  74. * 交换数组中的键和值
  75. * @access public
  76. * @return static
  77. */
  78. public function flip()
  79. {
  80. return new static(array_flip($this->items));
  81. }
  82. /**
  83. * 返回数组中所有的键名组成的新 Collection 实例
  84. * @access public
  85. * @return static
  86. */
  87. public function keys()
  88. {
  89. return new static(array_keys($this->items));
  90. }
  91. /**
  92. * 合并数组并返回一个新的 Collection 实例
  93. * @access public
  94. * @param mixed $items 新的数据
  95. * @return static
  96. */
  97. public function merge($items)
  98. {
  99. return new static(array_merge($this->items, $this->convertToArray($items)));
  100. }
  101. /**
  102. * 比较数组,返回差集生成的新 Collection 实例
  103. * @access public
  104. * @param mixed $items 做比较的数据
  105. * @return static
  106. */
  107. public function diff($items)
  108. {
  109. return new static(array_diff($this->items, $this->convertToArray($items)));
  110. }
  111. /**
  112. * 比较数组,返回交集组成的 Collection 新实例
  113. * @access public
  114. * @param mixed $items 比较数据
  115. * @return static
  116. */
  117. public function intersect($items)
  118. {
  119. return new static(array_intersect($this->items, $this->convertToArray($items)));
  120. }
  121. /**
  122. * 返回并删除数据中的的最后一个元素(出栈)
  123. * @access public
  124. * @return mixed
  125. */
  126. public function pop()
  127. {
  128. return array_pop($this->items);
  129. }
  130. /**
  131. * 返回并删除数据中首个元素
  132. * @access public
  133. * @return mixed
  134. */
  135. public function shift()
  136. {
  137. return array_shift($this->items);
  138. }
  139. /**
  140. * 在数组开头插入一个元素
  141. * @access public
  142. * @param mixed $value 值
  143. * @param mixed $key 键名
  144. * @return void
  145. */
  146. public function unshift($value, $key = null)
  147. {
  148. if (is_null($key)) {
  149. array_unshift($this->items, $value);
  150. } else {
  151. $this->items = [$key => $value] + $this->items;
  152. }
  153. }
  154. /**
  155. * 在数组结尾插入一个元素
  156. * @access public
  157. * @param mixed $value 值
  158. * @param mixed $key 键名
  159. * @return void
  160. */
  161. public function push($value, $key = null)
  162. {
  163. if (is_null($key)) {
  164. $this->items[] = $value;
  165. } else {
  166. $this->items[$key] = $value;
  167. }
  168. }
  169. /**
  170. * 通过使用用户自定义函数,以字符串返回数组
  171. * @access public
  172. * @param callable $callback 回调函数
  173. * @param mixed $initial 初始值
  174. * @return mixed
  175. */
  176. public function reduce(callable $callback, $initial = null)
  177. {
  178. return array_reduce($this->items, $callback, $initial);
  179. }
  180. /**
  181. * 以相反的顺序创建一个新的 Collection 实例
  182. * @access public
  183. * @return static
  184. */
  185. public function reverse()
  186. {
  187. return new static(array_reverse($this->items));
  188. }
  189. /**
  190. * 把数据分割为新的数组块
  191. * @access public
  192. * @param int $size 分隔长度
  193. * @param bool $preserveKeys 是否保持原数据索引
  194. * @return static
  195. */
  196. public function chunk($size, $preserveKeys = false)
  197. {
  198. $chunks = [];
  199. foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
  200. $chunks[] = new static($chunk);
  201. }
  202. return new static($chunks);
  203. }
  204. /**
  205. * 给数据中的每个元素执行回调
  206. * @access public
  207. * @param callable $callback 回调函数
  208. * @return $this
  209. */
  210. public function each(callable $callback)
  211. {
  212. foreach ($this->items as $key => $item) {
  213. $result = $callback($item, $key);
  214. if (false === $result) {
  215. break;
  216. }
  217. if (!is_object($item)) {
  218. $this->items[$key] = $result;
  219. }
  220. }
  221. return $this;
  222. }
  223. /**
  224. * 用回调函数过滤数据中的元素
  225. * @access public
  226. * @param callable|null $callback 回调函数
  227. * @return static
  228. */
  229. public function filter(callable $callback = null)
  230. {
  231. return new static(array_filter($this->items, $callback ?: null));
  232. }
  233. /**
  234. * 返回数据中指定的一列
  235. * @access public
  236. * @param mixed $columnKey 键名
  237. * @param null $indexKey 作为索引值的列
  238. * @return array
  239. */
  240. public function column($columnKey, $indexKey = null)
  241. {
  242. if (function_exists('array_column')) {
  243. return array_column($this->items, $columnKey, $indexKey);
  244. }
  245. $result = [];
  246. foreach ($this->items as $row) {
  247. $key = $value = null;
  248. $keySet = $valueSet = false;
  249. if (null !== $indexKey && array_key_exists($indexKey, $row)) {
  250. $key = (string) $row[$indexKey];
  251. $keySet = true;
  252. }
  253. if (null === $columnKey) {
  254. $valueSet = true;
  255. $value = $row;
  256. } elseif (is_array($row) && array_key_exists($columnKey, $row)) {
  257. $valueSet = true;
  258. $value = $row[$columnKey];
  259. }
  260. if ($valueSet) {
  261. if ($keySet) {
  262. $result[$key] = $value;
  263. } else {
  264. $result[] = $value;
  265. }
  266. }
  267. }
  268. return $result;
  269. }
  270. /**
  271. * 对数据排序,并返回排序后的数据组成的新 Collection 实例
  272. * @access public
  273. * @param callable|null $callback 回调函数
  274. * @return static
  275. */
  276. public function sort(callable $callback = null)
  277. {
  278. $items = $this->items;
  279. $callback = $callback ?: function ($a, $b) {
  280. return $a == $b ? 0 : (($a < $b) ? -1 : 1);
  281. };
  282. uasort($items, $callback);
  283. return new static($items);
  284. }
  285. /**
  286. * 将数据打乱后组成新的 Collection 实例
  287. * @access public
  288. * @return static
  289. */
  290. public function shuffle()
  291. {
  292. $items = $this->items;
  293. shuffle($items);
  294. return new static($items);
  295. }
  296. /**
  297. * 截取数据并返回新的 Collection 实例
  298. * @access public
  299. * @param int $offset 起始位置
  300. * @param int $length 截取长度
  301. * @param bool $preserveKeys 是否保持原先的键名
  302. * @return static
  303. */
  304. public function slice($offset, $length = null, $preserveKeys = false)
  305. {
  306. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  307. }
  308. /**
  309. * 指定的键是否存在
  310. * @access public
  311. * @param mixed $offset 键名
  312. * @return bool
  313. */
  314. public function offsetExists($offset)
  315. {
  316. return array_key_exists($offset, $this->items);
  317. }
  318. /**
  319. * 获取指定键对应的值
  320. * @access public
  321. * @param mixed $offset 键名
  322. * @return mixed
  323. */
  324. public function offsetGet($offset)
  325. {
  326. return $this->items[$offset];
  327. }
  328. /**
  329. * 设置键值
  330. * @access public
  331. * @param mixed $offset 键名
  332. * @param mixed $value 值
  333. * @return void
  334. */
  335. public function offsetSet($offset, $value)
  336. {
  337. if (is_null($offset)) {
  338. $this->items[] = $value;
  339. } else {
  340. $this->items[$offset] = $value;
  341. }
  342. }
  343. /**
  344. * 删除指定键值
  345. * @access public
  346. * @param mixed $offset 键名
  347. * @return void
  348. */
  349. public function offsetUnset($offset)
  350. {
  351. unset($this->items[$offset]);
  352. }
  353. /**
  354. * 统计数据的个数
  355. * @access public
  356. * @return int
  357. */
  358. public function count()
  359. {
  360. return count($this->items);
  361. }
  362. /**
  363. * 获取数据的迭代器
  364. * @access public
  365. * @return ArrayIterator
  366. */
  367. public function getIterator()
  368. {
  369. return new ArrayIterator($this->items);
  370. }
  371. /**
  372. * 将数据反序列化成数组
  373. * @access public
  374. * @return array
  375. */
  376. public function jsonSerialize()
  377. {
  378. return $this->toArray();
  379. }
  380. /**
  381. * 转换当前数据集为 JSON 字符串
  382. * @access public
  383. * @param integer $options json 参数
  384. * @return string
  385. */
  386. public function toJson($options = JSON_UNESCAPED_UNICODE)
  387. {
  388. return json_encode($this->toArray(), $options);
  389. }
  390. /**
  391. * 将数据转换成字符串
  392. * @access public
  393. * @return string
  394. */
  395. public function __toString()
  396. {
  397. return $this->toJson();
  398. }
  399. /**
  400. * 将数据转换成数组
  401. * @access protected
  402. * @param mixed $items 数据
  403. * @return array
  404. */
  405. protected function convertToArray($items)
  406. {
  407. return $items instanceof self ? $items->all() : (array) $items;
  408. }
  409. }