Collection.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2019 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. declare (strict_types = 1);
  12. namespace think;
  13. use ArrayAccess;
  14. use ArrayIterator;
  15. use Countable;
  16. use IteratorAggregate;
  17. use JsonSerializable;
  18. use think\contract\Arrayable;
  19. use think\contract\Jsonable;
  20. use think\helper\Arr;
  21. /**
  22. * 数据集管理类
  23. */
  24. class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Arrayable, Jsonable
  25. {
  26. /**
  27. * 数据集数据
  28. * @var array
  29. */
  30. protected $items = [];
  31. public function __construct($items = [])
  32. {
  33. $this->items = $this->convertToArray($items);
  34. }
  35. public static function make($items = [])
  36. {
  37. return new static($items);
  38. }
  39. /**
  40. * 是否为空
  41. * @access public
  42. * @return bool
  43. */
  44. public function isEmpty(): bool
  45. {
  46. return empty($this->items);
  47. }
  48. public function toArray(): array
  49. {
  50. return array_map(function ($value) {
  51. return $value instanceof Arrayable ? $value->toArray() : $value;
  52. }, $this->items);
  53. }
  54. public function all(): array
  55. {
  56. return $this->items;
  57. }
  58. /**
  59. * 合并数组
  60. *
  61. * @access public
  62. * @param mixed $items 数据
  63. * @return static
  64. */
  65. public function merge($items)
  66. {
  67. return new static(array_merge($this->items, $this->convertToArray($items)));
  68. }
  69. /**
  70. * 按指定键整理数据
  71. *
  72. * @access public
  73. * @param mixed $items 数据
  74. * @param string $indexKey 键名
  75. * @return array
  76. */
  77. public function dictionary($items = null, string &$indexKey = null)
  78. {
  79. if ($items instanceof self) {
  80. $items = $items->all();
  81. }
  82. $items = is_null($items) ? $this->items : $items;
  83. if ($items && empty($indexKey)) {
  84. $indexKey = is_array($items[0]) ? 'id' : $items[0]->getPk();
  85. }
  86. if (isset($indexKey) && is_string($indexKey)) {
  87. return array_column($items, null, $indexKey);
  88. }
  89. return $items;
  90. }
  91. /**
  92. * 比较数组,返回差集
  93. *
  94. * @access public
  95. * @param mixed $items 数据
  96. * @param string $indexKey 指定比较的键名
  97. * @return static
  98. */
  99. public function diff($items, string $indexKey = null)
  100. {
  101. if ($this->isEmpty() || is_scalar($this->items[0])) {
  102. return new static(array_diff($this->items, $this->convertToArray($items)));
  103. }
  104. $diff = [];
  105. $dictionary = $this->dictionary($items, $indexKey);
  106. if (is_string($indexKey)) {
  107. foreach ($this->items as $item) {
  108. if (!isset($dictionary[$item[$indexKey]])) {
  109. $diff[] = $item;
  110. }
  111. }
  112. }
  113. return new static($diff);
  114. }
  115. /**
  116. * 比较数组,返回交集
  117. *
  118. * @access public
  119. * @param mixed $items 数据
  120. * @param string $indexKey 指定比较的键名
  121. * @return static
  122. */
  123. public function intersect($items, string $indexKey = null)
  124. {
  125. if ($this->isEmpty() || is_scalar($this->items[0])) {
  126. return new static(array_diff($this->items, $this->convertToArray($items)));
  127. }
  128. $intersect = [];
  129. $dictionary = $this->dictionary($items, $indexKey);
  130. if (is_string($indexKey)) {
  131. foreach ($this->items as $item) {
  132. if (isset($dictionary[$item[$indexKey]])) {
  133. $intersect[] = $item;
  134. }
  135. }
  136. }
  137. return new static($intersect);
  138. }
  139. /**
  140. * 交换数组中的键和值
  141. *
  142. * @access public
  143. * @return static
  144. */
  145. public function flip()
  146. {
  147. return new static(array_flip($this->items));
  148. }
  149. /**
  150. * 返回数组中所有的键名
  151. *
  152. * @access public
  153. * @return static
  154. */
  155. public function keys()
  156. {
  157. return new static(array_keys($this->items));
  158. }
  159. /**
  160. * 返回数组中所有的值组成的新 Collection 实例
  161. * @access public
  162. * @return static
  163. */
  164. public function values()
  165. {
  166. return new static(array_values($this->items));
  167. }
  168. /**
  169. * 删除数组的最后一个元素(出栈)
  170. *
  171. * @access public
  172. * @return mixed
  173. */
  174. public function pop()
  175. {
  176. return array_pop($this->items);
  177. }
  178. /**
  179. * 通过使用用户自定义函数,以字符串返回数组
  180. *
  181. * @access public
  182. * @param callable $callback 调用方法
  183. * @param mixed $initial
  184. * @return mixed
  185. */
  186. public function reduce(callable $callback, $initial = null)
  187. {
  188. return array_reduce($this->items, $callback, $initial);
  189. }
  190. /**
  191. * 以相反的顺序返回数组。
  192. *
  193. * @access public
  194. * @return static
  195. */
  196. public function reverse()
  197. {
  198. return new static(array_reverse($this->items));
  199. }
  200. /**
  201. * 删除数组中首个元素,并返回被删除元素的值
  202. *
  203. * @access public
  204. * @return mixed
  205. */
  206. public function shift()
  207. {
  208. return array_shift($this->items);
  209. }
  210. /**
  211. * 在数组结尾插入一个元素
  212. * @access public
  213. * @param mixed $value 元素
  214. * @param string $key KEY
  215. * @return $this
  216. */
  217. public function push($value, string $key = null)
  218. {
  219. if (is_null($key)) {
  220. $this->items[] = $value;
  221. } else {
  222. $this->items[$key] = $value;
  223. }
  224. return $this;
  225. }
  226. /**
  227. * 把一个数组分割为新的数组块.
  228. *
  229. * @access public
  230. * @param int $size 块大小
  231. * @param bool $preserveKeys
  232. * @return static
  233. */
  234. public function chunk(int $size, bool $preserveKeys = false)
  235. {
  236. $chunks = [];
  237. foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
  238. $chunks[] = new static($chunk);
  239. }
  240. return new static($chunks);
  241. }
  242. /**
  243. * 在数组开头插入一个元素
  244. * @access public
  245. * @param mixed $value 元素
  246. * @param string $key KEY
  247. * @return $this
  248. */
  249. public function unshift($value, string $key = null)
  250. {
  251. if (is_null($key)) {
  252. array_unshift($this->items, $value);
  253. } else {
  254. $this->items = [$key => $value] + $this->items;
  255. }
  256. return $this;
  257. }
  258. /**
  259. * 给每个元素执行个回调
  260. *
  261. * @access public
  262. * @param callable $callback 回调
  263. * @return $this
  264. */
  265. public function each(callable $callback)
  266. {
  267. foreach ($this->items as $key => $item) {
  268. $result = $callback($item, $key);
  269. if (false === $result) {
  270. break;
  271. } elseif (!is_object($item)) {
  272. $this->items[$key] = $result;
  273. }
  274. }
  275. return $this;
  276. }
  277. /**
  278. * 用回调函数处理数组中的元素
  279. * @access public
  280. * @param callable|null $callback 回调
  281. * @return static
  282. */
  283. public function map(callable $callback)
  284. {
  285. return new static(array_map($callback, $this->items));
  286. }
  287. /**
  288. * 用回调函数过滤数组中的元素
  289. * @access public
  290. * @param callable|null $callback 回调
  291. * @return static
  292. */
  293. public function filter(callable $callback = null)
  294. {
  295. if ($callback) {
  296. return new static(array_filter($this->items, $callback));
  297. }
  298. return new static(array_filter($this->items));
  299. }
  300. /**
  301. * 根据字段条件过滤数组中的元素
  302. * @access public
  303. * @param string $field 字段名
  304. * @param mixed $operator 操作符
  305. * @param mixed $value 数据
  306. * @return static
  307. */
  308. public function where(string $field, $operator, $value = null)
  309. {
  310. if (is_null($value)) {
  311. $value = $operator;
  312. $operator = '=';
  313. }
  314. return $this->filter(function ($data) use ($field, $operator, $value) {
  315. if (strpos($field, '.')) {
  316. [$field, $relation] = explode('.', $field);
  317. $result = $data[$field][$relation] ?? null;
  318. } else {
  319. $result = $data[$field] ?? null;
  320. }
  321. switch (strtolower($operator)) {
  322. case '===':
  323. return $result === $value;
  324. case '!==':
  325. return $result !== $value;
  326. case '!=':
  327. case '<>':
  328. return $result != $value;
  329. case '>':
  330. return $result > $value;
  331. case '>=':
  332. return $result >= $value;
  333. case '<':
  334. return $result < $value;
  335. case '<=':
  336. return $result <= $value;
  337. case 'like':
  338. return is_string($result) && false !== strpos($result, $value);
  339. case 'not like':
  340. return is_string($result) && false === strpos($result, $value);
  341. case 'in':
  342. return is_scalar($result) && in_array($result, $value, true);
  343. case 'not in':
  344. return is_scalar($result) && !in_array($result, $value, true);
  345. case 'between':
  346. [$min, $max] = is_string($value) ? explode(',', $value) : $value;
  347. return is_scalar($result) && $result >= $min && $result <= $max;
  348. case 'not between':
  349. [$min, $max] = is_string($value) ? explode(',', $value) : $value;
  350. return is_scalar($result) && $result > $max || $result < $min;
  351. case '==':
  352. case '=':
  353. default:
  354. return $result == $value;
  355. }
  356. });
  357. }
  358. /**
  359. * LIKE过滤
  360. * @access public
  361. * @param string $field 字段名
  362. * @param string $value 数据
  363. * @return static
  364. */
  365. public function whereLike(string $field, string $value)
  366. {
  367. return $this->where($field, 'like', $value);
  368. }
  369. /**
  370. * NOT LIKE过滤
  371. * @access public
  372. * @param string $field 字段名
  373. * @param string $value 数据
  374. * @return static
  375. */
  376. public function whereNotLike(string $field, string $value)
  377. {
  378. return $this->where($field, 'not like', $value);
  379. }
  380. /**
  381. * IN过滤
  382. * @access public
  383. * @param string $field 字段名
  384. * @param array $value 数据
  385. * @return static
  386. */
  387. public function whereIn(string $field, array $value)
  388. {
  389. return $this->where($field, 'in', $value);
  390. }
  391. /**
  392. * NOT IN过滤
  393. * @access public
  394. * @param string $field 字段名
  395. * @param array $value 数据
  396. * @return static
  397. */
  398. public function whereNotIn(string $field, array $value)
  399. {
  400. return $this->where($field, 'not in', $value);
  401. }
  402. /**
  403. * BETWEEN 过滤
  404. * @access public
  405. * @param string $field 字段名
  406. * @param mixed $value 数据
  407. * @return static
  408. */
  409. public function whereBetween(string $field, $value)
  410. {
  411. return $this->where($field, 'between', $value);
  412. }
  413. /**
  414. * NOT BETWEEN 过滤
  415. * @access public
  416. * @param string $field 字段名
  417. * @param mixed $value 数据
  418. * @return static
  419. */
  420. public function whereNotBetween(string $field, $value)
  421. {
  422. return $this->where($field, 'not between', $value);
  423. }
  424. /**
  425. * 返回数据中指定的一列
  426. * @access public
  427. * @param string|null $columnKey 键名
  428. * @param string|null $indexKey 作为索引值的列
  429. * @return array
  430. */
  431. public function column( ? string $columnKey, string $indexKey = null)
  432. {
  433. return array_column($this->items, $columnKey, $indexKey);
  434. }
  435. /**
  436. * 对数组排序
  437. *
  438. * @access public
  439. * @param callable|null $callback 回调
  440. * @return static
  441. */
  442. public function sort(callable $callback = null)
  443. {
  444. $items = $this->items;
  445. $callback = $callback ?: function ($a, $b) {
  446. return $a == $b ? 0 : (($a < $b) ? -1 : 1);
  447. };
  448. uasort($items, $callback);
  449. return new static($items);
  450. }
  451. /**
  452. * 指定字段排序
  453. * @access public
  454. * @param string $field 排序字段
  455. * @param string $order 排序
  456. * @return $this
  457. */
  458. public function order(string $field, string $order = 'asc')
  459. {
  460. return $this->sort(function ($a, $b) use ($field, $order) {
  461. $fieldA = $a[$field] ?? null;
  462. $fieldB = $b[$field] ?? null;
  463. return 'desc' == strtolower($order) ? intval($fieldB > $fieldA) : intval($fieldA > $fieldB);
  464. });
  465. }
  466. /**
  467. * 将数组打乱
  468. *
  469. * @access public
  470. * @return static
  471. */
  472. public function shuffle()
  473. {
  474. $items = $this->items;
  475. shuffle($items);
  476. return new static($items);
  477. }
  478. /**
  479. * 获取第一个单元数据
  480. *
  481. * @access public
  482. * @param callable|null $callback
  483. * @param null $default
  484. * @return mixed
  485. */
  486. public function first(callable $callback = null, $default = null)
  487. {
  488. return Arr::first($this->items, $callback, $default);
  489. }
  490. /**
  491. * 获取最后一个单元数据
  492. *
  493. * @access public
  494. * @param callable|null $callback
  495. * @param null $default
  496. * @return mixed
  497. */
  498. public function last(callable $callback = null, $default = null)
  499. {
  500. return Arr::last($this->items, $callback, $default);
  501. }
  502. /**
  503. * 截取数组
  504. *
  505. * @access public
  506. * @param int $offset 起始位置
  507. * @param int $length 截取长度
  508. * @param bool $preserveKeys preserveKeys
  509. * @return static
  510. */
  511. public function slice(int $offset, int $length = null, bool $preserveKeys = false)
  512. {
  513. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  514. }
  515. // ArrayAccess
  516. public function offsetExists($offset)
  517. {
  518. return array_key_exists($offset, $this->items);
  519. }
  520. public function offsetGet($offset)
  521. {
  522. return $this->items[$offset];
  523. }
  524. public function offsetSet($offset, $value)
  525. {
  526. if (is_null($offset)) {
  527. $this->items[] = $value;
  528. } else {
  529. $this->items[$offset] = $value;
  530. }
  531. }
  532. public function offsetUnset($offset)
  533. {
  534. unset($this->items[$offset]);
  535. }
  536. //Countable
  537. public function count()
  538. {
  539. return count($this->items);
  540. }
  541. //IteratorAggregate
  542. public function getIterator()
  543. {
  544. return new ArrayIterator($this->items);
  545. }
  546. //JsonSerializable
  547. public function jsonSerialize()
  548. {
  549. return $this->toArray();
  550. }
  551. /**
  552. * 转换当前数据集为JSON字符串
  553. * @access public
  554. * @param integer $options json参数
  555. * @return string
  556. */
  557. public function toJson(int $options = JSON_UNESCAPED_UNICODE) : string
  558. {
  559. return json_encode($this->toArray(), $options);
  560. }
  561. public function __toString()
  562. {
  563. return $this->toJson();
  564. }
  565. /**
  566. * 转换成数组
  567. *
  568. * @access public
  569. * @param mixed $items 数据
  570. * @return array
  571. */
  572. protected function convertToArray($items): array
  573. {
  574. if ($items instanceof self) {
  575. return $items->all();
  576. }
  577. return (array) $items;
  578. }
  579. }