PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Support/Collection.php

https://gitlab.com/ealexis.t/trends
PHP | 1170 lines | 518 code | 147 blank | 505 comment | 39 complexity | 586fa74564bd02767cbcfee93760b461 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Support;
  3. use Countable;
  4. use ArrayAccess;
  5. use ArrayIterator;
  6. use CachingIterator;
  7. use JsonSerializable;
  8. use IteratorAggregate;
  9. use InvalidArgumentException;
  10. use Illuminate\Support\Traits\Macroable;
  11. use Illuminate\Contracts\Support\Jsonable;
  12. use Illuminate\Contracts\Support\Arrayable;
  13. class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable
  14. {
  15. use Macroable;
  16. /**
  17. * The items contained in the collection.
  18. *
  19. * @var array
  20. */
  21. protected $items = [];
  22. /**
  23. * Create a new collection.
  24. *
  25. * @param mixed $items
  26. * @return void
  27. */
  28. public function __construct($items = [])
  29. {
  30. $this->items = $this->getArrayableItems($items);
  31. }
  32. /**
  33. * Create a new collection instance if the value isn't one already.
  34. *
  35. * @param mixed $items
  36. * @return static
  37. */
  38. public static function make($items = [])
  39. {
  40. return new static($items);
  41. }
  42. /**
  43. * Get all of the items in the collection.
  44. *
  45. * @return array
  46. */
  47. public function all()
  48. {
  49. return $this->items;
  50. }
  51. /**
  52. * Get the average value of a given key.
  53. *
  54. * @param string|null $key
  55. * @return mixed
  56. */
  57. public function avg($key = null)
  58. {
  59. if ($count = $this->count()) {
  60. return $this->sum($key) / $count;
  61. }
  62. }
  63. /**
  64. * Alias for the "avg" method.
  65. *
  66. * @param string|null $key
  67. * @return mixed
  68. */
  69. public function average($key = null)
  70. {
  71. return $this->avg($key);
  72. }
  73. /**
  74. * Collapse the collection of items into a single array.
  75. *
  76. * @return static
  77. */
  78. public function collapse()
  79. {
  80. return new static(Arr::collapse($this->items));
  81. }
  82. /**
  83. * Determine if an item exists in the collection.
  84. *
  85. * @param mixed $key
  86. * @param mixed $value
  87. * @return bool
  88. */
  89. public function contains($key, $value = null)
  90. {
  91. if (func_num_args() == 2) {
  92. return $this->contains(function ($k, $item) use ($key, $value) {
  93. return data_get($item, $key) == $value;
  94. });
  95. }
  96. if ($this->useAsCallable($key)) {
  97. return ! is_null($this->first($key));
  98. }
  99. return in_array($key, $this->items);
  100. }
  101. /**
  102. * Get the items in the collection that are not present in the given items.
  103. *
  104. * @param mixed $items
  105. * @return static
  106. */
  107. public function diff($items)
  108. {
  109. return new static(array_diff($this->items, $this->getArrayableItems($items)));
  110. }
  111. /**
  112. * Get the items in the collection whose keys are not present in the given items.
  113. *
  114. * @param mixed $items
  115. * @return static
  116. */
  117. public function diffKeys($items)
  118. {
  119. return new static(array_diff_key($this->items, $this->getArrayableItems($items)));
  120. }
  121. /**
  122. * Execute a callback over each item.
  123. *
  124. * @param callable $callback
  125. * @return $this
  126. */
  127. public function each(callable $callback)
  128. {
  129. foreach ($this->items as $key => $item) {
  130. if ($callback($item, $key) === false) {
  131. break;
  132. }
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Create a new collection consisting of every n-th element.
  138. *
  139. * @param int $step
  140. * @param int $offset
  141. * @return static
  142. */
  143. public function every($step, $offset = 0)
  144. {
  145. $new = [];
  146. $position = 0;
  147. foreach ($this->items as $item) {
  148. if ($position % $step === $offset) {
  149. $new[] = $item;
  150. }
  151. $position++;
  152. }
  153. return new static($new);
  154. }
  155. /**
  156. * Get all items except for those with the specified keys.
  157. *
  158. * @param mixed $keys
  159. * @return static
  160. */
  161. public function except($keys)
  162. {
  163. $keys = is_array($keys) ? $keys : func_get_args();
  164. return new static(Arr::except($this->items, $keys));
  165. }
  166. /**
  167. * Run a filter over each of the items.
  168. *
  169. * @param callable|null $callback
  170. * @return static
  171. */
  172. public function filter(callable $callback = null)
  173. {
  174. if ($callback) {
  175. $return = [];
  176. foreach ($this->items as $key => $value) {
  177. if ($callback($value, $key)) {
  178. $return[$key] = $value;
  179. }
  180. }
  181. return new static($return);
  182. }
  183. return new static(array_filter($this->items));
  184. }
  185. /**
  186. * Filter items by the given key value pair.
  187. *
  188. * @param string $key
  189. * @param mixed $value
  190. * @param bool $strict
  191. * @return static
  192. */
  193. public function where($key, $value, $strict = true)
  194. {
  195. return $this->filter(function ($item) use ($key, $value, $strict) {
  196. return $strict ? data_get($item, $key) === $value
  197. : data_get($item, $key) == $value;
  198. });
  199. }
  200. /**
  201. * Filter items by the given key value pair using loose comparison.
  202. *
  203. * @param string $key
  204. * @param mixed $value
  205. * @return static
  206. */
  207. public function whereLoose($key, $value)
  208. {
  209. return $this->where($key, $value, false);
  210. }
  211. /**
  212. * Filter items by the given key value pair.
  213. *
  214. * @param string $key
  215. * @param array $values
  216. * @param bool $strict
  217. * @return static
  218. */
  219. public function whereIn($key, array $values, $strict = true)
  220. {
  221. return $this->filter(function ($item) use ($key, $values, $strict) {
  222. return in_array(data_get($item, $key), $values, $strict);
  223. });
  224. }
  225. /**
  226. * Filter items by the given key value pair using loose comparison.
  227. *
  228. * @param string $key
  229. * @param array $values
  230. * @return static
  231. */
  232. public function whereInLoose($key, array $values)
  233. {
  234. return $this->whereIn($key, $values, false);
  235. }
  236. /**
  237. * Get the first item from the collection.
  238. *
  239. * @param callable|null $callback
  240. * @param mixed $default
  241. * @return mixed
  242. */
  243. public function first(callable $callback = null, $default = null)
  244. {
  245. return Arr::first($this->items, $callback, $default);
  246. }
  247. /**
  248. * Get a flattened array of the items in the collection.
  249. *
  250. * @param int $depth
  251. * @return static
  252. */
  253. public function flatten($depth = INF)
  254. {
  255. return new static(Arr::flatten($this->items, $depth));
  256. }
  257. /**
  258. * Flip the items in the collection.
  259. *
  260. * @return static
  261. */
  262. public function flip()
  263. {
  264. return new static(array_flip($this->items));
  265. }
  266. /**
  267. * Remove an item from the collection by key.
  268. *
  269. * @param string|array $keys
  270. * @return $this
  271. */
  272. public function forget($keys)
  273. {
  274. foreach ((array) $keys as $key) {
  275. $this->offsetUnset($key);
  276. }
  277. return $this;
  278. }
  279. /**
  280. * Get an item from the collection by key.
  281. *
  282. * @param mixed $key
  283. * @param mixed $default
  284. * @return mixed
  285. */
  286. public function get($key, $default = null)
  287. {
  288. if ($this->offsetExists($key)) {
  289. return $this->items[$key];
  290. }
  291. return value($default);
  292. }
  293. /**
  294. * Group an associative array by a field or using a callback.
  295. *
  296. * @param callable|string $groupBy
  297. * @param bool $preserveKeys
  298. * @return static
  299. */
  300. public function groupBy($groupBy, $preserveKeys = false)
  301. {
  302. $groupBy = $this->valueRetriever($groupBy);
  303. $results = [];
  304. foreach ($this->items as $key => $value) {
  305. $groupKeys = $groupBy($value, $key);
  306. if (! is_array($groupKeys)) {
  307. $groupKeys = [$groupKeys];
  308. }
  309. foreach ($groupKeys as $groupKey) {
  310. if (! array_key_exists($groupKey, $results)) {
  311. $results[$groupKey] = new static;
  312. }
  313. $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value);
  314. }
  315. }
  316. return new static($results);
  317. }
  318. /**
  319. * Key an associative array by a field or using a callback.
  320. *
  321. * @param callable|string $keyBy
  322. * @return static
  323. */
  324. public function keyBy($keyBy)
  325. {
  326. $keyBy = $this->valueRetriever($keyBy);
  327. $results = [];
  328. foreach ($this->items as $item) {
  329. $results[$keyBy($item)] = $item;
  330. }
  331. return new static($results);
  332. }
  333. /**
  334. * Determine if an item exists in the collection by key.
  335. *
  336. * @param mixed $key
  337. * @return bool
  338. */
  339. public function has($key)
  340. {
  341. return $this->offsetExists($key);
  342. }
  343. /**
  344. * Concatenate values of a given key as a string.
  345. *
  346. * @param string $value
  347. * @param string $glue
  348. * @return string
  349. */
  350. public function implode($value, $glue = null)
  351. {
  352. $first = $this->first();
  353. if (is_array($first) || is_object($first)) {
  354. return implode($glue, $this->pluck($value)->all());
  355. }
  356. return implode($value, $this->items);
  357. }
  358. /**
  359. * Intersect the collection with the given items.
  360. *
  361. * @param mixed $items
  362. * @return static
  363. */
  364. public function intersect($items)
  365. {
  366. return new static(array_intersect($this->items, $this->getArrayableItems($items)));
  367. }
  368. /**
  369. * Determine if the collection is empty or not.
  370. *
  371. * @return bool
  372. */
  373. public function isEmpty()
  374. {
  375. return empty($this->items);
  376. }
  377. /**
  378. * Determine if the given value is callable, but not a string.
  379. *
  380. * @param mixed $value
  381. * @return bool
  382. */
  383. protected function useAsCallable($value)
  384. {
  385. return ! is_string($value) && is_callable($value);
  386. }
  387. /**
  388. * Get the keys of the collection items.
  389. *
  390. * @return static
  391. */
  392. public function keys()
  393. {
  394. return new static(array_keys($this->items));
  395. }
  396. /**
  397. * Get the last item from the collection.
  398. *
  399. * @param callable|null $callback
  400. * @param mixed $default
  401. * @return mixed
  402. */
  403. public function last(callable $callback = null, $default = null)
  404. {
  405. return Arr::last($this->items, $callback, $default);
  406. }
  407. /**
  408. * Get the values of a given key.
  409. *
  410. * @param string $value
  411. * @param string|null $key
  412. * @return static
  413. */
  414. public function pluck($value, $key = null)
  415. {
  416. return new static(Arr::pluck($this->items, $value, $key));
  417. }
  418. /**
  419. * Alias for the "pluck" method.
  420. *
  421. * @param string $value
  422. * @param string|null $key
  423. * @return static
  424. *
  425. * @deprecated since version 5.2. Use the "pluck" method directly.
  426. */
  427. public function lists($value, $key = null)
  428. {
  429. return $this->pluck($value, $key);
  430. }
  431. /**
  432. * Run a map over each of the items.
  433. *
  434. * @param callable $callback
  435. * @return static
  436. */
  437. public function map(callable $callback)
  438. {
  439. $keys = array_keys($this->items);
  440. $items = array_map($callback, $this->items, $keys);
  441. return new static(array_combine($keys, $items));
  442. }
  443. /**
  444. * Map a collection and flatten the result by a single level.
  445. *
  446. * @param callable $callback
  447. * @return static
  448. */
  449. public function flatMap(callable $callback)
  450. {
  451. return $this->map($callback)->collapse();
  452. }
  453. /**
  454. * Get the max value of a given key.
  455. *
  456. * @param string|null $key
  457. * @return mixed
  458. */
  459. public function max($key = null)
  460. {
  461. return $this->reduce(function ($result, $item) use ($key) {
  462. $value = data_get($item, $key);
  463. return is_null($result) || $value > $result ? $value : $result;
  464. });
  465. }
  466. /**
  467. * Merge the collection with the given items.
  468. *
  469. * @param mixed $items
  470. * @return static
  471. */
  472. public function merge($items)
  473. {
  474. return new static(array_merge($this->items, $this->getArrayableItems($items)));
  475. }
  476. /**
  477. * Create a collection by using this collection for keys and another for its values.
  478. *
  479. * @param mixed $values
  480. * @return static
  481. */
  482. public function combine($values)
  483. {
  484. return new static(array_combine($this->all(), $this->getArrayableItems($values)));
  485. }
  486. /**
  487. * Union the collection with the given items.
  488. *
  489. * @param mixed $items
  490. * @return static
  491. */
  492. public function union($items)
  493. {
  494. return new static($this->items + $this->getArrayableItems($items));
  495. }
  496. /**
  497. * Get the min value of a given key.
  498. *
  499. * @param string|null $key
  500. * @return mixed
  501. */
  502. public function min($key = null)
  503. {
  504. return $this->reduce(function ($result, $item) use ($key) {
  505. $value = data_get($item, $key);
  506. return is_null($result) || $value < $result ? $value : $result;
  507. });
  508. }
  509. /**
  510. * Get the items with the specified keys.
  511. *
  512. * @param mixed $keys
  513. * @return static
  514. */
  515. public function only($keys)
  516. {
  517. $keys = is_array($keys) ? $keys : func_get_args();
  518. return new static(Arr::only($this->items, $keys));
  519. }
  520. /**
  521. * "Paginate" the collection by slicing it into a smaller collection.
  522. *
  523. * @param int $page
  524. * @param int $perPage
  525. * @return static
  526. */
  527. public function forPage($page, $perPage)
  528. {
  529. return $this->slice(($page - 1) * $perPage, $perPage);
  530. }
  531. /**
  532. * Get and remove the last item from the collection.
  533. *
  534. * @return mixed
  535. */
  536. public function pop()
  537. {
  538. return array_pop($this->items);
  539. }
  540. /**
  541. * Push an item onto the beginning of the collection.
  542. *
  543. * @param mixed $value
  544. * @param mixed $key
  545. * @return $this
  546. */
  547. public function prepend($value, $key = null)
  548. {
  549. $this->items = Arr::prepend($this->items, $value, $key);
  550. return $this;
  551. }
  552. /**
  553. * Push an item onto the end of the collection.
  554. *
  555. * @param mixed $value
  556. * @return $this
  557. */
  558. public function push($value)
  559. {
  560. $this->offsetSet(null, $value);
  561. return $this;
  562. }
  563. /**
  564. * Get and remove an item from the collection.
  565. *
  566. * @param mixed $key
  567. * @param mixed $default
  568. * @return mixed
  569. */
  570. public function pull($key, $default = null)
  571. {
  572. return Arr::pull($this->items, $key, $default);
  573. }
  574. /**
  575. * Put an item in the collection by key.
  576. *
  577. * @param mixed $key
  578. * @param mixed $value
  579. * @return $this
  580. */
  581. public function put($key, $value)
  582. {
  583. $this->offsetSet($key, $value);
  584. return $this;
  585. }
  586. /**
  587. * Get one or more items randomly from the collection.
  588. *
  589. * @param int $amount
  590. * @return mixed
  591. *
  592. * @throws \InvalidArgumentException
  593. */
  594. public function random($amount = 1)
  595. {
  596. if ($amount > ($count = $this->count())) {
  597. throw new InvalidArgumentException("You requested {$amount} items, but there are only {$count} items in the collection");
  598. }
  599. $keys = array_rand($this->items, $amount);
  600. if ($amount == 1) {
  601. return $this->items[$keys];
  602. }
  603. return new static(array_intersect_key($this->items, array_flip($keys)));
  604. }
  605. /**
  606. * Reduce the collection to a single value.
  607. *
  608. * @param callable $callback
  609. * @param mixed $initial
  610. * @return mixed
  611. */
  612. public function reduce(callable $callback, $initial = null)
  613. {
  614. return array_reduce($this->items, $callback, $initial);
  615. }
  616. /**
  617. * Create a collection of all elements that do not pass a given truth test.
  618. *
  619. * @param callable|mixed $callback
  620. * @return static
  621. */
  622. public function reject($callback)
  623. {
  624. if ($this->useAsCallable($callback)) {
  625. return $this->filter(function ($value, $key) use ($callback) {
  626. return ! $callback($value, $key);
  627. });
  628. }
  629. return $this->filter(function ($item) use ($callback) {
  630. return $item != $callback;
  631. });
  632. }
  633. /**
  634. * Reverse items order.
  635. *
  636. * @return static
  637. */
  638. public function reverse()
  639. {
  640. return new static(array_reverse($this->items, true));
  641. }
  642. /**
  643. * Search the collection for a given value and return the corresponding key if successful.
  644. *
  645. * @param mixed $value
  646. * @param bool $strict
  647. * @return mixed
  648. */
  649. public function search($value, $strict = false)
  650. {
  651. if (! $this->useAsCallable($value)) {
  652. return array_search($value, $this->items, $strict);
  653. }
  654. foreach ($this->items as $key => $item) {
  655. if (call_user_func($value, $item, $key)) {
  656. return $key;
  657. }
  658. }
  659. return false;
  660. }
  661. /**
  662. * Get and remove the first item from the collection.
  663. *
  664. * @return mixed
  665. */
  666. public function shift()
  667. {
  668. return array_shift($this->items);
  669. }
  670. /**
  671. * Shuffle the items in the collection.
  672. *
  673. * @return static
  674. */
  675. public function shuffle()
  676. {
  677. $items = $this->items;
  678. shuffle($items);
  679. return new static($items);
  680. }
  681. /**
  682. * Slice the underlying collection array.
  683. *
  684. * @param int $offset
  685. * @param int $length
  686. * @return static
  687. */
  688. public function slice($offset, $length = null)
  689. {
  690. return new static(array_slice($this->items, $offset, $length, true));
  691. }
  692. /**
  693. * Chunk the underlying collection array.
  694. *
  695. * @param int $size
  696. * @return static
  697. */
  698. public function chunk($size)
  699. {
  700. $chunks = [];
  701. foreach (array_chunk($this->items, $size, true) as $chunk) {
  702. $chunks[] = new static($chunk);
  703. }
  704. return new static($chunks);
  705. }
  706. /**
  707. * Sort through each item with a callback.
  708. *
  709. * @param callable|null $callback
  710. * @return static
  711. */
  712. public function sort(callable $callback = null)
  713. {
  714. $items = $this->items;
  715. $callback ? uasort($items, $callback) : uasort($items, function ($a, $b) {
  716. if ($a == $b) {
  717. return 0;
  718. }
  719. return ($a < $b) ? -1 : 1;
  720. });
  721. return new static($items);
  722. }
  723. /**
  724. * Sort the collection using the given callback.
  725. *
  726. * @param callable|string $callback
  727. * @param int $options
  728. * @param bool $descending
  729. * @return static
  730. */
  731. public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
  732. {
  733. $results = [];
  734. $callback = $this->valueRetriever($callback);
  735. // First we will loop through the items and get the comparator from a callback
  736. // function which we were given. Then, we will sort the returned values and
  737. // and grab the corresponding values for the sorted keys from this array.
  738. foreach ($this->items as $key => $value) {
  739. $results[$key] = $callback($value, $key);
  740. }
  741. $descending ? arsort($results, $options)
  742. : asort($results, $options);
  743. // Once we have sorted all of the keys in the array, we will loop through them
  744. // and grab the corresponding model so we can set the underlying items list
  745. // to the sorted version. Then we'll just return the collection instance.
  746. foreach (array_keys($results) as $key) {
  747. $results[$key] = $this->items[$key];
  748. }
  749. return new static($results);
  750. }
  751. /**
  752. * Sort the collection in descending order using the given callback.
  753. *
  754. * @param callable|string $callback
  755. * @param int $options
  756. * @return static
  757. */
  758. public function sortByDesc($callback, $options = SORT_REGULAR)
  759. {
  760. return $this->sortBy($callback, $options, true);
  761. }
  762. /**
  763. * Splice a portion of the underlying collection array.
  764. *
  765. * @param int $offset
  766. * @param int|null $length
  767. * @param mixed $replacement
  768. * @return static
  769. */
  770. public function splice($offset, $length = null, $replacement = [])
  771. {
  772. if (func_num_args() == 1) {
  773. return new static(array_splice($this->items, $offset));
  774. }
  775. return new static(array_splice($this->items, $offset, $length, $replacement));
  776. }
  777. /**
  778. * Get the sum of the given values.
  779. *
  780. * @param callable|string|null $callback
  781. * @return mixed
  782. */
  783. public function sum($callback = null)
  784. {
  785. if (is_null($callback)) {
  786. return array_sum($this->items);
  787. }
  788. $callback = $this->valueRetriever($callback);
  789. return $this->reduce(function ($result, $item) use ($callback) {
  790. return $result += $callback($item);
  791. }, 0);
  792. }
  793. /**
  794. * Take the first or last {$limit} items.
  795. *
  796. * @param int $limit
  797. * @return static
  798. */
  799. public function take($limit)
  800. {
  801. if ($limit < 0) {
  802. return $this->slice($limit, abs($limit));
  803. }
  804. return $this->slice(0, $limit);
  805. }
  806. /**
  807. * Transform each item in the collection using a callback.
  808. *
  809. * @param callable $callback
  810. * @return $this
  811. */
  812. public function transform(callable $callback)
  813. {
  814. $this->items = $this->map($callback)->all();
  815. return $this;
  816. }
  817. /**
  818. * Return only unique items from the collection array.
  819. *
  820. * @param string|callable|null $key
  821. * @return static
  822. */
  823. public function unique($key = null)
  824. {
  825. if (is_null($key)) {
  826. return new static(array_unique($this->items, SORT_REGULAR));
  827. }
  828. $key = $this->valueRetriever($key);
  829. $exists = [];
  830. return $this->reject(function ($item) use ($key, &$exists) {
  831. if (in_array($id = $key($item), $exists)) {
  832. return true;
  833. }
  834. $exists[] = $id;
  835. });
  836. }
  837. /**
  838. * Reset the keys on the underlying array.
  839. *
  840. * @return static
  841. */
  842. public function values()
  843. {
  844. return new static(array_values($this->items));
  845. }
  846. /**
  847. * Get a value retrieving callback.
  848. *
  849. * @param string $value
  850. * @return callable
  851. */
  852. protected function valueRetriever($value)
  853. {
  854. if ($this->useAsCallable($value)) {
  855. return $value;
  856. }
  857. return function ($item) use ($value) {
  858. return data_get($item, $value);
  859. };
  860. }
  861. /**
  862. * Zip the collection together with one or more arrays.
  863. *
  864. * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
  865. * => [[1, 4], [2, 5], [3, 6]]
  866. *
  867. * @param mixed ...$items
  868. * @return static
  869. */
  870. public function zip($items)
  871. {
  872. $arrayableItems = array_map(function ($items) {
  873. return $this->getArrayableItems($items);
  874. }, func_get_args());
  875. $params = array_merge([function () {
  876. return new static(func_get_args());
  877. }, $this->items], $arrayableItems);
  878. return new static(call_user_func_array('array_map', $params));
  879. }
  880. /**
  881. * Get the collection of items as a plain array.
  882. *
  883. * @return array
  884. */
  885. public function toArray()
  886. {
  887. return array_map(function ($value) {
  888. return $value instanceof Arrayable ? $value->toArray() : $value;
  889. }, $this->items);
  890. }
  891. /**
  892. * Convert the object into something JSON serializable.
  893. *
  894. * @return array
  895. */
  896. public function jsonSerialize()
  897. {
  898. return array_map(function ($value) {
  899. if ($value instanceof JsonSerializable) {
  900. return $value->jsonSerialize();
  901. } elseif ($value instanceof Jsonable) {
  902. return json_decode($value->toJson(), true);
  903. } elseif ($value instanceof Arrayable) {
  904. return $value->toArray();
  905. } else {
  906. return $value;
  907. }
  908. }, $this->items);
  909. }
  910. /**
  911. * Get the collection of items as JSON.
  912. *
  913. * @param int $options
  914. * @return string
  915. */
  916. public function toJson($options = 0)
  917. {
  918. return json_encode($this->jsonSerialize(), $options);
  919. }
  920. /**
  921. * Get an iterator for the items.
  922. *
  923. * @return \ArrayIterator
  924. */
  925. public function getIterator()
  926. {
  927. return new ArrayIterator($this->items);
  928. }
  929. /**
  930. * Get a CachingIterator instance.
  931. *
  932. * @param int $flags
  933. * @return \CachingIterator
  934. */
  935. public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
  936. {
  937. return new CachingIterator($this->getIterator(), $flags);
  938. }
  939. /**
  940. * Count the number of items in the collection.
  941. *
  942. * @return int
  943. */
  944. public function count()
  945. {
  946. return count($this->items);
  947. }
  948. /**
  949. * Determine if an item exists at an offset.
  950. *
  951. * @param mixed $key
  952. * @return bool
  953. */
  954. public function offsetExists($key)
  955. {
  956. return array_key_exists($key, $this->items);
  957. }
  958. /**
  959. * Get an item at a given offset.
  960. *
  961. * @param mixed $key
  962. * @return mixed
  963. */
  964. public function offsetGet($key)
  965. {
  966. return $this->items[$key];
  967. }
  968. /**
  969. * Set the item at a given offset.
  970. *
  971. * @param mixed $key
  972. * @param mixed $value
  973. * @return void
  974. */
  975. public function offsetSet($key, $value)
  976. {
  977. if (is_null($key)) {
  978. $this->items[] = $value;
  979. } else {
  980. $this->items[$key] = $value;
  981. }
  982. }
  983. /**
  984. * Unset the item at a given offset.
  985. *
  986. * @param string $key
  987. * @return void
  988. */
  989. public function offsetUnset($key)
  990. {
  991. unset($this->items[$key]);
  992. }
  993. /**
  994. * Convert the collection to its string representation.
  995. *
  996. * @return string
  997. */
  998. public function __toString()
  999. {
  1000. return $this->toJson();
  1001. }
  1002. /**
  1003. * Results array of items from Collection or Arrayable.
  1004. *
  1005. * @param mixed $items
  1006. * @return array
  1007. */
  1008. protected function getArrayableItems($items)
  1009. {
  1010. if (is_array($items)) {
  1011. return $items;
  1012. } elseif ($items instanceof self) {
  1013. return $items->all();
  1014. } elseif ($items instanceof Arrayable) {
  1015. return $items->toArray();
  1016. } elseif ($items instanceof Jsonable) {
  1017. return json_decode($items->toJson(), true);
  1018. } elseif ($items instanceof JsonSerializable) {
  1019. return $items->jsonSerialize();
  1020. }
  1021. return (array) $items;
  1022. }
  1023. }