Collection.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. $result = $callback($item, $key);
  171. if (false === $result) {
  172. break;
  173. } elseif (!is_object($item)) {
  174. $this->items[$key] = $result;
  175. }
  176. }
  177. return $this;
  178. }
  179. /**
  180. * 用回调函数过滤数组中的元素
  181. * @param callable|null $callback
  182. * @return static
  183. */
  184. public function filter(callable $callback = null)
  185. {
  186. if ($callback) {
  187. return new static(array_filter($this->items, $callback));
  188. }
  189. return new static(array_filter($this->items));
  190. }
  191. /**
  192. * 返回数组中指定的一列
  193. * @param $column_key
  194. * @param null $index_key
  195. * @return array
  196. */
  197. public function column($column_key, $index_key = null)
  198. {
  199. if (function_exists('array_column')) {
  200. return array_column($this->items, $column_key, $index_key);
  201. }
  202. $result = [];
  203. foreach ($this->items as $row) {
  204. $key = $value = null;
  205. $keySet = $valueSet = false;
  206. if (null !== $index_key && array_key_exists($index_key, $row)) {
  207. $keySet = true;
  208. $key = (string) $row[$index_key];
  209. }
  210. if (null === $column_key) {
  211. $valueSet = true;
  212. $value = $row;
  213. } elseif (is_array($row) && array_key_exists($column_key, $row)) {
  214. $valueSet = true;
  215. $value = $row[$column_key];
  216. }
  217. if ($valueSet) {
  218. if ($keySet) {
  219. $result[$key] = $value;
  220. } else {
  221. $result[] = $value;
  222. }
  223. }
  224. }
  225. return $result;
  226. }
  227. /**
  228. * 对数组排序
  229. *
  230. * @param callable|null $callback
  231. * @return static
  232. */
  233. public function sort(callable $callback = null)
  234. {
  235. $items = $this->items;
  236. $callback ? uasort($items, $callback) : uasort($items, function ($a, $b) {
  237. if ($a == $b) {
  238. return 0;
  239. }
  240. return ($a < $b) ? -1 : 1;
  241. });
  242. return new static($items);
  243. }
  244. /**
  245. * 将数组打乱
  246. *
  247. * @return static
  248. */
  249. public function shuffle()
  250. {
  251. $items = $this->items;
  252. shuffle($items);
  253. return new static($items);
  254. }
  255. /**
  256. * 截取数组
  257. *
  258. * @param int $offset
  259. * @param int $length
  260. * @param bool $preserveKeys
  261. * @return static
  262. */
  263. public function slice($offset, $length = null, $preserveKeys = false)
  264. {
  265. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  266. }
  267. // ArrayAccess
  268. public function offsetExists($offset)
  269. {
  270. return array_key_exists($offset, $this->items);
  271. }
  272. public function offsetGet($offset)
  273. {
  274. return $this->items[$offset];
  275. }
  276. public function offsetSet($offset, $value)
  277. {
  278. if (is_null($offset)) {
  279. $this->items[] = $value;
  280. } else {
  281. $this->items[$offset] = $value;
  282. }
  283. }
  284. public function offsetUnset($offset)
  285. {
  286. unset($this->items[$offset]);
  287. }
  288. //Countable
  289. public function count()
  290. {
  291. return count($this->items);
  292. }
  293. //IteratorAggregate
  294. public function getIterator()
  295. {
  296. return new ArrayIterator($this->items);
  297. }
  298. //JsonSerializable
  299. public function jsonSerialize()
  300. {
  301. return $this->toArray();
  302. }
  303. /**
  304. * 转换当前数据集为JSON字符串
  305. * @access public
  306. * @param integer $options json参数
  307. * @return string
  308. */
  309. public function toJson($options = JSON_UNESCAPED_UNICODE)
  310. {
  311. return json_encode($this->toArray(), $options);
  312. }
  313. public function __toString()
  314. {
  315. return $this->toJson();
  316. }
  317. /**
  318. * 转换成数组
  319. *
  320. * @param mixed $items
  321. * @return array
  322. */
  323. protected function convertToArray($items)
  324. {
  325. if ($items instanceof self) {
  326. return $items->all();
  327. }
  328. return (array) $items;
  329. }
  330. }