Collection.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. * @return static
  95. */
  96. public function values()
  97. {
  98. return new static(array_values($this->items));
  99. }
  100. /**
  101. * 合并数组并返回一个新的 Collection 实例
  102. * @access public
  103. * @param mixed $items 新的数据
  104. * @return static
  105. */
  106. public function merge($items)
  107. {
  108. return new static(array_merge($this->items, $this->convertToArray($items)));
  109. }
  110. /**
  111. * 比较数组,返回差集生成的新 Collection 实例
  112. * @access public
  113. * @param mixed $items 做比较的数据
  114. * @return static
  115. */
  116. public function diff($items)
  117. {
  118. return new static(array_diff($this->items, $this->convertToArray($items)));
  119. }
  120. /**
  121. * 比较数组,返回交集组成的 Collection 新实例
  122. * @access public
  123. * @param mixed $items 比较数据
  124. * @return static
  125. */
  126. public function intersect($items)
  127. {
  128. return new static(array_intersect($this->items, $this->convertToArray($items)));
  129. }
  130. /**
  131. * 返回并删除数据中的的最后一个元素(出栈)
  132. * @access public
  133. * @return mixed
  134. */
  135. public function pop()
  136. {
  137. return array_pop($this->items);
  138. }
  139. /**
  140. * 返回并删除数据中首个元素
  141. * @access public
  142. * @return mixed
  143. */
  144. public function shift()
  145. {
  146. return array_shift($this->items);
  147. }
  148. /**
  149. * 在数组开头插入一个元素
  150. * @access public
  151. * @param mixed $value 值
  152. * @param mixed $key 键名
  153. * @return void
  154. */
  155. public function unshift($value, $key = null)
  156. {
  157. if (is_null($key)) {
  158. array_unshift($this->items, $value);
  159. } else {
  160. $this->items = [$key => $value] + $this->items;
  161. }
  162. }
  163. /**
  164. * 在数组结尾插入一个元素
  165. * @access public
  166. * @param mixed $value 值
  167. * @param mixed $key 键名
  168. * @return void
  169. */
  170. public function push($value, $key = null)
  171. {
  172. if (is_null($key)) {
  173. $this->items[] = $value;
  174. } else {
  175. $this->items[$key] = $value;
  176. }
  177. }
  178. /**
  179. * 通过使用用户自定义函数,以字符串返回数组
  180. * @access public
  181. * @param callable $callback 回调函数
  182. * @param mixed $initial 初始值
  183. * @return mixed
  184. */
  185. public function reduce(callable $callback, $initial = null)
  186. {
  187. return array_reduce($this->items, $callback, $initial);
  188. }
  189. /**
  190. * 以相反的顺序创建一个新的 Collection 实例
  191. * @access public
  192. * @return static
  193. */
  194. public function reverse()
  195. {
  196. return new static(array_reverse($this->items));
  197. }
  198. /**
  199. * 把数据分割为新的数组块
  200. * @access public
  201. * @param int $size 分隔长度
  202. * @param bool $preserveKeys 是否保持原数据索引
  203. * @return static
  204. */
  205. public function chunk($size, $preserveKeys = false)
  206. {
  207. $chunks = [];
  208. foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
  209. $chunks[] = new static($chunk);
  210. }
  211. return new static($chunks);
  212. }
  213. /**
  214. * 给数据中的每个元素执行回调
  215. * @access public
  216. * @param callable $callback 回调函数
  217. * @return $this
  218. */
  219. public function each(callable $callback)
  220. {
  221. foreach ($this->items as $key => $item) {
  222. $result = $callback($item, $key);
  223. if (false === $result) {
  224. break;
  225. }
  226. if (!is_object($item)) {
  227. $this->items[$key] = $result;
  228. }
  229. }
  230. return $this;
  231. }
  232. /**
  233. * 用回调函数过滤数据中的元素
  234. * @access public
  235. * @param callable|null $callback 回调函数
  236. * @return static
  237. */
  238. public function filter(callable $callback = null)
  239. {
  240. return new static(array_filter($this->items, $callback ?: null));
  241. }
  242. /**
  243. * 返回数据中指定的一列
  244. * @access public
  245. * @param mixed $columnKey 键名
  246. * @param null $indexKey 作为索引值的列
  247. * @return array
  248. */
  249. public function column($columnKey, $indexKey = null)
  250. {
  251. if (function_exists('array_column')) {
  252. return array_column($this->items, $columnKey, $indexKey);
  253. }
  254. $result = [];
  255. foreach ($this->items as $row) {
  256. $key = $value = null;
  257. $keySet = $valueSet = false;
  258. if (null !== $indexKey && array_key_exists($indexKey, $row)) {
  259. $key = (string) $row[$indexKey];
  260. $keySet = true;
  261. }
  262. if (null === $columnKey) {
  263. $valueSet = true;
  264. $value = $row;
  265. } elseif (is_array($row) && array_key_exists($columnKey, $row)) {
  266. $valueSet = true;
  267. $value = $row[$columnKey];
  268. }
  269. if ($valueSet) {
  270. if ($keySet) {
  271. $result[$key] = $value;
  272. } else {
  273. $result[] = $value;
  274. }
  275. }
  276. }
  277. return $result;
  278. }
  279. /**
  280. * 对数据排序,并返回排序后的数据组成的新 Collection 实例
  281. * @access public
  282. * @param callable|null $callback 回调函数
  283. * @return static
  284. */
  285. public function sort(callable $callback = null)
  286. {
  287. $items = $this->items;
  288. $callback = $callback ?: function ($a, $b) {
  289. return $a == $b ? 0 : (($a < $b) ? -1 : 1);
  290. };
  291. uasort($items, $callback);
  292. return new static($items);
  293. }
  294. /**
  295. * 将数据打乱后组成新的 Collection 实例
  296. * @access public
  297. * @return static
  298. */
  299. public function shuffle()
  300. {
  301. $items = $this->items;
  302. shuffle($items);
  303. return new static($items);
  304. }
  305. /**
  306. * 截取数据并返回新的 Collection 实例
  307. * @access public
  308. * @param int $offset 起始位置
  309. * @param int $length 截取长度
  310. * @param bool $preserveKeys 是否保持原先的键名
  311. * @return static
  312. */
  313. public function slice($offset, $length = null, $preserveKeys = false)
  314. {
  315. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  316. }
  317. /**
  318. * 指定的键是否存在
  319. * @access public
  320. * @param mixed $offset 键名
  321. * @return bool
  322. */
  323. #[\ReturnTypeWillChange]
  324. public function offsetExists($offset)
  325. {
  326. return array_key_exists($offset, $this->items);
  327. }
  328. /**
  329. * 获取指定键对应的值
  330. * @access public
  331. * @param mixed $offset 键名
  332. * @return mixed
  333. */
  334. #[\ReturnTypeWillChange]
  335. public function offsetGet($offset)
  336. {
  337. return $this->items[$offset];
  338. }
  339. /**
  340. * 设置键值
  341. * @access public
  342. * @param mixed $offset 键名
  343. * @param mixed $value 值
  344. * @return void
  345. */
  346. #[\ReturnTypeWillChange]
  347. public function offsetSet($offset, $value)
  348. {
  349. if (is_null($offset)) {
  350. $this->items[] = $value;
  351. } else {
  352. $this->items[$offset] = $value;
  353. }
  354. }
  355. /**
  356. * 删除指定键值
  357. * @access public
  358. * @param mixed $offset 键名
  359. * @return void
  360. */
  361. #[\ReturnTypeWillChange]
  362. public function offsetUnset($offset)
  363. {
  364. unset($this->items[$offset]);
  365. }
  366. /**
  367. * 统计数据的个数
  368. * @access public
  369. * @return int
  370. */
  371. #[\ReturnTypeWillChange]
  372. public function count()
  373. {
  374. return count($this->items);
  375. }
  376. /**
  377. * 获取数据的迭代器
  378. * @access public
  379. * @return ArrayIterator
  380. */
  381. #[\ReturnTypeWillChange]
  382. public function getIterator()
  383. {
  384. return new ArrayIterator($this->items);
  385. }
  386. /**
  387. * 将数据反序列化成数组
  388. * @access public
  389. * @return array
  390. */
  391. #[\ReturnTypeWillChange]
  392. public function jsonSerialize()
  393. {
  394. return $this->toArray();
  395. }
  396. /**
  397. * 转换当前数据集为 JSON 字符串
  398. * @access public
  399. * @param integer $options json 参数
  400. * @return string
  401. */
  402. public function toJson($options = JSON_UNESCAPED_UNICODE)
  403. {
  404. return json_encode($this->toArray(), $options);
  405. }
  406. /**
  407. * 将数据转换成字符串
  408. * @access public
  409. * @return string
  410. */
  411. public function __toString()
  412. {
  413. return $this->toJson();
  414. }
  415. /**
  416. * 将数据转换成数组
  417. * @access protected
  418. * @param mixed $items 数据
  419. * @return array
  420. */
  421. protected function convertToArray($items)
  422. {
  423. return $items instanceof self ? $items->all() : (array) $items;
  424. }
  425. }