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

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