Collection.php 8.7 KB

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