Collection.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2017 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. protected $items = [];
  20. public function __construct($items = [])
  21. {
  22. $this->items = $this->convertToArray($items);
  23. }
  24. public static function make($items = [])
  25. {
  26. return new static($items);
  27. }
  28. /**
  29. * 是否为空
  30. * @return bool
  31. */
  32. public function isEmpty()
  33. {
  34. return empty($this->items);
  35. }
  36. public function toArray()
  37. {
  38. return array_map(function ($value) {
  39. return ($value instanceof Model || $value instanceof self) ? $value->toArray() : $value;
  40. }, $this->items);
  41. }
  42. public function all()
  43. {
  44. return $this->items;
  45. }
  46. /**
  47. * 合并数组
  48. *
  49. * @param mixed $items
  50. * @return static
  51. */
  52. public function merge($items)
  53. {
  54. return new static(array_merge($this->items, $this->convertToArray($items)));
  55. }
  56. /**
  57. * 比较数组,返回差集
  58. *
  59. * @param mixed $items
  60. * @return static
  61. */
  62. public function diff($items)
  63. {
  64. return new static(array_diff($this->items, $this->convertToArray($items)));
  65. }
  66. /**
  67. * 交换数组中的键和值
  68. *
  69. * @return static
  70. */
  71. public function flip()
  72. {
  73. return new static(array_flip($this->items));
  74. }
  75. /**
  76. * 比较数组,返回交集
  77. *
  78. * @param mixed $items
  79. * @return static
  80. */
  81. public function intersect($items)
  82. {
  83. return new static(array_intersect($this->items, $this->convertToArray($items)));
  84. }
  85. /**
  86. * 返回数组中所有的键名
  87. *
  88. * @return static
  89. */
  90. public function keys()
  91. {
  92. return new static(array_keys($this->items));
  93. }
  94. /**
  95. * 删除数组的最后一个元素(出栈)
  96. *
  97. * @return mixed
  98. */
  99. public function pop()
  100. {
  101. return array_pop($this->items);
  102. }
  103. /**
  104. * 通过使用用户自定义函数,以字符串返回数组
  105. *
  106. * @param callable $callback
  107. * @param mixed $initial
  108. * @return mixed
  109. */
  110. public function reduce(callable $callback, $initial = null)
  111. {
  112. return array_reduce($this->items, $callback, $initial);
  113. }
  114. /**
  115. * 以相反的顺序返回数组。
  116. *
  117. * @return static
  118. */
  119. public function reverse()
  120. {
  121. return new static(array_reverse($this->items));
  122. }
  123. /**
  124. * 删除数组中首个元素,并返回被删除元素的值
  125. *
  126. * @return mixed
  127. */
  128. public function shift()
  129. {
  130. return array_shift($this->items);
  131. }
  132. /**
  133. * 把一个数组分割为新的数组块.
  134. *
  135. * @param int $size
  136. * @param bool $preserveKeys
  137. * @return static
  138. */
  139. public function chunk($size, $preserveKeys = false)
  140. {
  141. $chunks = [];
  142. foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
  143. $chunks[] = new static($chunk);
  144. }
  145. return new static($chunks);
  146. }
  147. /**
  148. * 在数组开头插入一个元素
  149. * @param mixed $value
  150. * @param null $key
  151. * @return int
  152. */
  153. public function unshift($value, $key = null)
  154. {
  155. if (is_null($key)) {
  156. array_unshift($this->items, $value);
  157. } else {
  158. $this->items = [$key => $value] + $this->items;
  159. }
  160. }
  161. /**
  162. * 给每个元素执行个回调
  163. *
  164. * @param callable $callback
  165. * @return $this
  166. */
  167. public function each(callable $callback)
  168. {
  169. foreach ($this->items as $key => $item) {
  170. if ($callback($item, $key) === false) {
  171. break;
  172. }
  173. }
  174. return $this;
  175. }
  176. /**
  177. * 用回调函数过滤数组中的元素
  178. * @param callable|null $callback
  179. * @return static
  180. */
  181. public function filter(callable $callback = null)
  182. {
  183. if ($callback) {
  184. return new static(array_filter($this->items, $callback));
  185. }
  186. return new static(array_filter($this->items));
  187. }
  188. /**
  189. * 返回数组中指定的一列
  190. * @param $column_key
  191. * @param null $index_key
  192. * @return array
  193. */
  194. public function column($column_key, $index_key = null)
  195. {
  196. if (function_exists('array_column')) {
  197. return array_column($this->items, $column_key, $index_key);
  198. }
  199. $result = [];
  200. foreach ($this->items as $row) {
  201. $key = $value = null;
  202. $keySet = $valueSet = false;
  203. if (null !== $index_key && array_key_exists($index_key, $row)) {
  204. $keySet = true;
  205. $key = (string) $row[$index_key];
  206. }
  207. if (null === $column_key) {
  208. $valueSet = true;
  209. $value = $row;
  210. } elseif (is_array($row) && array_key_exists($column_key, $row)) {
  211. $valueSet = true;
  212. $value = $row[$column_key];
  213. }
  214. if ($valueSet) {
  215. if ($keySet) {
  216. $result[$key] = $value;
  217. } else {
  218. $result[] = $value;
  219. }
  220. }
  221. }
  222. return $result;
  223. }
  224. /**
  225. * 对数组排序
  226. *
  227. * @param callable|null $callback
  228. * @return static
  229. */
  230. public function sort(callable $callback = null)
  231. {
  232. $items = $this->items;
  233. $callback ? uasort($items, $callback) : uasort($items, function ($a, $b) {
  234. if ($a == $b) {
  235. return 0;
  236. }
  237. return ($a < $b) ? -1 : 1;
  238. });
  239. return new static($items);
  240. }
  241. /**
  242. * 将数组打乱
  243. *
  244. * @return static
  245. */
  246. public function shuffle()
  247. {
  248. $items = $this->items;
  249. shuffle($items);
  250. return new static($items);
  251. }
  252. /**
  253. * 截取数组
  254. *
  255. * @param int $offset
  256. * @param int $length
  257. * @param bool $preserveKeys
  258. * @return static
  259. */
  260. public function slice($offset, $length = null, $preserveKeys = false)
  261. {
  262. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  263. }
  264. // ArrayAccess
  265. public function offsetExists($offset)
  266. {
  267. return array_key_exists($offset, $this->items);
  268. }
  269. public function offsetGet($offset)
  270. {
  271. return $this->items[$offset];
  272. }
  273. public function offsetSet($offset, $value)
  274. {
  275. if (is_null($offset)) {
  276. $this->items[] = $value;
  277. } else {
  278. $this->items[$offset] = $value;
  279. }
  280. }
  281. public function offsetUnset($offset)
  282. {
  283. unset($this->items[$offset]);
  284. }
  285. //Countable
  286. public function count()
  287. {
  288. return count($this->items);
  289. }
  290. //IteratorAggregate
  291. public function getIterator()
  292. {
  293. return new ArrayIterator($this->items);
  294. }
  295. //JsonSerializable
  296. public function jsonSerialize()
  297. {
  298. return $this->toArray();
  299. }
  300. /**
  301. * 转换当前数据集为JSON字符串
  302. * @access public
  303. * @param integer $options json参数
  304. * @return string
  305. */
  306. public function toJson($options = JSON_UNESCAPED_UNICODE)
  307. {
  308. return json_encode($this->toArray(), $options);
  309. }
  310. public function __toString()
  311. {
  312. return $this->toJson();
  313. }
  314. /**
  315. * 转换成数组
  316. *
  317. * @param mixed $items
  318. * @return array
  319. */
  320. protected function convertToArray($items)
  321. {
  322. if ($items instanceof self) {
  323. return $items->all();
  324. }
  325. return (array) $items;
  326. }
  327. }