Collection.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 void
  216. */
  217. public function push($value, string $key = null): void
  218. {
  219. if (is_null($key)) {
  220. $this->items[] = $value;
  221. } else {
  222. $this->items[$key] = $value;
  223. }
  224. }
  225. /**
  226. * 把一个数组分割为新的数组块.
  227. *
  228. * @access public
  229. * @param int $size 块大小
  230. * @param bool $preserveKeys
  231. * @return static
  232. */
  233. public function chunk(int $size, bool $preserveKeys = false)
  234. {
  235. $chunks = [];
  236. foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
  237. $chunks[] = new static($chunk);
  238. }
  239. return new static($chunks);
  240. }
  241. /**
  242. * 在数组开头插入一个元素
  243. * @access public
  244. * @param mixed $value 元素
  245. * @param string $key KEY
  246. * @return void
  247. */
  248. public function unshift($value, string $key = null): void
  249. {
  250. if (is_null($key)) {
  251. array_unshift($this->items, $value);
  252. } else {
  253. $this->items = [$key => $value] + $this->items;
  254. }
  255. }
  256. /**
  257. * 给每个元素执行个回调
  258. *
  259. * @access public
  260. * @param callable $callback 回调
  261. * @return $this
  262. */
  263. public function each(callable $callback)
  264. {
  265. foreach ($this->items as $key => $item) {
  266. $result = $callback($item, $key);
  267. if (false === $result) {
  268. break;
  269. } elseif (!is_object($item)) {
  270. $this->items[$key] = $result;
  271. }
  272. }
  273. return $this;
  274. }
  275. /**
  276. * 用回调函数处理数组中的元素
  277. * @access public
  278. * @param callable|null $callback 回调
  279. * @return static
  280. */
  281. public function map(callable $callback)
  282. {
  283. return new static(array_map($callback, $this->items));
  284. }
  285. /**
  286. * 用回调函数过滤数组中的元素
  287. * @access public
  288. * @param callable|null $callback 回调
  289. * @return static
  290. */
  291. public function filter(callable $callback = null)
  292. {
  293. if ($callback) {
  294. return new static(array_filter($this->items, $callback));
  295. }
  296. return new static(array_filter($this->items));
  297. }
  298. /**
  299. * 根据字段条件过滤数组中的元素
  300. * @access public
  301. * @param string $field 字段名
  302. * @param mixed $operator 操作符
  303. * @param mixed $value 数据
  304. * @return static
  305. */
  306. public function where(string $field, $operator, $value = null)
  307. {
  308. if (is_null($value)) {
  309. $value = $operator;
  310. $operator = '=';
  311. }
  312. return $this->filter(function ($data) use ($field, $operator, $value) {
  313. if (strpos($field, '.')) {
  314. list($field, $relation) = explode('.', $field);
  315. $result = $data[$field][$relation] ?? null;
  316. } else {
  317. $result = $data[$field] ?? null;
  318. }
  319. switch (strtolower($operator)) {
  320. case '===':
  321. return $result === $value;
  322. case '!==':
  323. return $result !== $value;
  324. case '!=':
  325. case '<>':
  326. return $result != $value;
  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 'like':
  336. return is_string($result) && false !== strpos($result, $value);
  337. case 'not like':
  338. return is_string($result) && false === strpos($result, $value);
  339. case 'in':
  340. return is_scalar($result) && in_array($result, $value, true);
  341. case 'not in':
  342. return is_scalar($result) && !in_array($result, $value, true);
  343. case 'between':
  344. list($min, $max) = is_string($value) ? explode(',', $value) : $value;
  345. return is_scalar($result) && $result >= $min && $result <= $max;
  346. case 'not between':
  347. list($min, $max) = is_string($value) ? explode(',', $value) : $value;
  348. return is_scalar($result) && $result > $max || $result < $min;
  349. case '==':
  350. case '=':
  351. default:
  352. return $result == $value;
  353. }
  354. });
  355. }
  356. /**
  357. * LIKE过滤
  358. * @access public
  359. * @param string $field 字段名
  360. * @param string $value 数据
  361. * @return static
  362. */
  363. public function whereLike(string $field, string $value)
  364. {
  365. return $this->where($field, 'like', $value);
  366. }
  367. /**
  368. * NOT LIKE过滤
  369. * @access public
  370. * @param string $field 字段名
  371. * @param string $value 数据
  372. * @return static
  373. */
  374. public function whereNotLike(string $field, string $value)
  375. {
  376. return $this->where($field, 'not like', $value);
  377. }
  378. /**
  379. * IN过滤
  380. * @access public
  381. * @param string $field 字段名
  382. * @param array $value 数据
  383. * @return static
  384. */
  385. public function whereIn(string $field, array $value)
  386. {
  387. return $this->where($field, 'in', $value);
  388. }
  389. /**
  390. * NOT IN过滤
  391. * @access public
  392. * @param string $field 字段名
  393. * @param array $value 数据
  394. * @return static
  395. */
  396. public function whereNotIn(string $field, array $value)
  397. {
  398. return $this->where($field, 'not in', $value);
  399. }
  400. /**
  401. * BETWEEN 过滤
  402. * @access public
  403. * @param string $field 字段名
  404. * @param mixed $value 数据
  405. * @return static
  406. */
  407. public function whereBetween(string $field, $value)
  408. {
  409. return $this->where($field, 'between', $value);
  410. }
  411. /**
  412. * NOT BETWEEN 过滤
  413. * @access public
  414. * @param string $field 字段名
  415. * @param mixed $value 数据
  416. * @return static
  417. */
  418. public function whereNotBetween(string $field, $value)
  419. {
  420. return $this->where($field, 'not between', $value);
  421. }
  422. /**
  423. * 返回数据中指定的一列
  424. * @access public
  425. * @param string $columnKey 键名
  426. * @param string $indexKey 作为索引值的列
  427. * @return array
  428. */
  429. public function column(string $columnKey, string $indexKey = null)
  430. {
  431. return array_column($this->items, $columnKey, $indexKey);
  432. }
  433. /**
  434. * 对数组排序
  435. *
  436. * @access public
  437. * @param callable|null $callback 回调
  438. * @return static
  439. */
  440. public function sort(callable $callback = null)
  441. {
  442. $items = $this->items;
  443. $callback = $callback ?: function ($a, $b) {
  444. return $a == $b ? 0 : (($a < $b) ? -1 : 1);
  445. };
  446. uasort($items, $callback);
  447. return new static($items);
  448. }
  449. /**
  450. * 指定字段排序
  451. * @access public
  452. * @param string $field 排序字段
  453. * @param string $order 排序
  454. * @return $this
  455. */
  456. public function order(string $field, string $order = null)
  457. {
  458. return $this->sort(function ($a, $b) use ($field, $order) {
  459. $fieldA = $a[$field] ?? null;
  460. $fieldB = $b[$field] ?? null;
  461. return 'desc' == strtolower($order) ? strcmp($fieldB, $fieldA) : strcmp($fieldA, $fieldB);
  462. });
  463. }
  464. /**
  465. * 将数组打乱
  466. *
  467. * @access public
  468. * @return static
  469. */
  470. public function shuffle()
  471. {
  472. $items = $this->items;
  473. shuffle($items);
  474. return new static($items);
  475. }
  476. /**
  477. * 获取最后一个单元数据
  478. *
  479. * @access public
  480. * @param callable|null $callback
  481. * @param null $default
  482. * @return mixed
  483. */
  484. public function first(callable $callback = null, $default = null)
  485. {
  486. return Arr::first($this->items, $callback, $default);
  487. }
  488. /**
  489. * 获取第一个单元数据
  490. *
  491. * @access public
  492. * @param callable|null $callback
  493. * @param null $default
  494. * @return mixed
  495. */
  496. public function last(callable $callback = null, $default = null)
  497. {
  498. return Arr::last($this->items, $callback, $default);
  499. }
  500. /**
  501. * 截取数组
  502. *
  503. * @access public
  504. * @param int $offset 起始位置
  505. * @param int $length 截取长度
  506. * @param bool $preserveKeys preserveKeys
  507. * @return static
  508. */
  509. public function slice(int $offset, int $length = null, bool $preserveKeys = false)
  510. {
  511. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  512. }
  513. // ArrayAccess
  514. public function offsetExists($offset)
  515. {
  516. return array_key_exists($offset, $this->items);
  517. }
  518. public function offsetGet($offset)
  519. {
  520. return $this->items[$offset];
  521. }
  522. public function offsetSet($offset, $value)
  523. {
  524. if (is_null($offset)) {
  525. $this->items[] = $value;
  526. } else {
  527. $this->items[$offset] = $value;
  528. }
  529. }
  530. public function offsetUnset($offset)
  531. {
  532. unset($this->items[$offset]);
  533. }
  534. //Countable
  535. public function count()
  536. {
  537. return count($this->items);
  538. }
  539. //IteratorAggregate
  540. public function getIterator()
  541. {
  542. return new ArrayIterator($this->items);
  543. }
  544. //JsonSerializable
  545. public function jsonSerialize()
  546. {
  547. return $this->toArray();
  548. }
  549. /**
  550. * 转换当前数据集为JSON字符串
  551. * @access public
  552. * @param integer $options json参数
  553. * @return string
  554. */
  555. public function toJson(int $options = JSON_UNESCAPED_UNICODE): string
  556. {
  557. return json_encode($this->toArray(), $options);
  558. }
  559. public function __toString()
  560. {
  561. return $this->toJson();
  562. }
  563. /**
  564. * 转换成数组
  565. *
  566. * @access public
  567. * @param mixed $items 数据
  568. * @return array
  569. */
  570. protected function convertToArray($items): array
  571. {
  572. if ($items instanceof self) {
  573. return $items->all();
  574. }
  575. return (array) $items;
  576. }
  577. }