PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/vendor/illuminate/support/Illuminate/Support/Collection.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 717 lines | 302 code | 84 blank | 331 comment | 10 complexity | 68bf45e45bbdabb4a09241cd19c4279e MD5 | raw file
  1. <?php namespace Illuminate\Support;
  2. use Closure;
  3. use Countable;
  4. use ArrayAccess;
  5. use ArrayIterator;
  6. use CachingIterator;
  7. use IteratorAggregate;
  8. use Illuminate\Support\Contracts\JsonableInterface;
  9. use Illuminate\Support\Contracts\ArrayableInterface;
  10. class Collection implements ArrayAccess, ArrayableInterface, Countable, IteratorAggregate, JsonableInterface {
  11. /**
  12. * The items contained in the collection.
  13. *
  14. * @var array
  15. */
  16. protected $items = array();
  17. /**
  18. * Create a new collection.
  19. *
  20. * @param array $items
  21. * @return void
  22. */
  23. public function __construct(array $items = array())
  24. {
  25. $this->items = $items;
  26. }
  27. /**
  28. * Create a new collection instance if the value isn't one already.
  29. *
  30. * @param mixed $items
  31. * @return \Illuminate\Support\Collection
  32. */
  33. public static function make($items)
  34. {
  35. if (is_null($items)) return new static;
  36. if ($items instanceof Collection) return $items;
  37. return new static(is_array($items) ? $items : array($items));
  38. }
  39. /**
  40. * Get all of the items in the collection.
  41. *
  42. * @return array
  43. */
  44. public function all()
  45. {
  46. return $this->items;
  47. }
  48. /**
  49. * Collapse the collection items into a single array.
  50. *
  51. * @return \Illuminate\Support\Collection
  52. */
  53. public function collapse()
  54. {
  55. $results = array();
  56. foreach ($this->items as $values)
  57. {
  58. $results = array_merge($results, $values);
  59. }
  60. return new static($results);
  61. }
  62. /**
  63. * Diff the collection with the given items.
  64. *
  65. * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
  66. * @return \Illuminate\Support\Collection
  67. */
  68. public function diff($items)
  69. {
  70. return new static(array_diff($this->items, $this->getArrayableItems($items)));
  71. }
  72. /**
  73. * Execute a callback over each item.
  74. *
  75. * @param Closure $callback
  76. * @return \Illuminate\Support\Collection
  77. */
  78. public function each(Closure $callback)
  79. {
  80. array_map($callback, $this->items);
  81. return $this;
  82. }
  83. /**
  84. * Fetch a nested element of the collection.
  85. *
  86. * @param string $key
  87. * @return \Illuminate\Support\Collection
  88. */
  89. public function fetch($key)
  90. {
  91. return new static(array_fetch($this->items, $key));
  92. }
  93. /**
  94. * Run a filter over each of the items.
  95. *
  96. * @param Closure $callback
  97. * @return \Illuminate\Support\Collection
  98. */
  99. public function filter(Closure $callback)
  100. {
  101. return new static(array_filter($this->items, $callback));
  102. }
  103. /**
  104. * Get the first item from the collection.
  105. *
  106. * @param \Closure $callback
  107. * @param mixed $default
  108. * @return mixed|null
  109. */
  110. public function first(Closure $callback = null, $default = null)
  111. {
  112. if (is_null($callback))
  113. {
  114. return count($this->items) > 0 ? reset($this->items) : null;
  115. }
  116. else
  117. {
  118. return array_first($this->items, $callback, $default);
  119. }
  120. }
  121. /**
  122. * Get a flattened array of the items in the collection.
  123. *
  124. * @return array
  125. */
  126. public function flatten()
  127. {
  128. return new static(array_flatten($this->items));
  129. }
  130. /**
  131. * Remove an item from the collection by key.
  132. *
  133. * @param mixed $key
  134. * @return void
  135. */
  136. public function forget($key)
  137. {
  138. unset($this->items[$key]);
  139. }
  140. /**
  141. * Get an item from the collection by key.
  142. *
  143. * @param mixed $key
  144. * @param mixed $default
  145. * @return mixed
  146. */
  147. public function get($key, $default = null)
  148. {
  149. if (array_key_exists($key, $this->items))
  150. {
  151. return $this->items[$key];
  152. }
  153. return value($default);
  154. }
  155. /**
  156. * Group an associative array by a field or Closure value.
  157. *
  158. * @param callable|string $groupBy
  159. * @return \Illuminate\Support\Collection
  160. */
  161. public function groupBy($groupBy)
  162. {
  163. $results = array();
  164. foreach ($this->items as $key => $value)
  165. {
  166. $key = is_callable($groupBy) ? $groupBy($value, $key) : data_get($value, $groupBy);
  167. $results[$key][] = $value;
  168. }
  169. return new static($results);
  170. }
  171. /**
  172. * Determine if an item exists in the collection by key.
  173. *
  174. * @param mixed $key
  175. * @return bool
  176. */
  177. public function has($key)
  178. {
  179. return array_key_exists($key, $this->items);
  180. }
  181. /**
  182. * Concatenate values of a given key as a string.
  183. *
  184. * @param string $value
  185. * @param string $glue
  186. * @return string
  187. */
  188. public function implode($value, $glue = null)
  189. {
  190. if (is_null($glue)) return implode($this->lists($value));
  191. return implode($glue, $this->lists($value));
  192. }
  193. /**
  194. * Intersect the collection with the given items.
  195. *
  196. * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
  197. * @return \Illuminate\Support\Collection
  198. */
  199. public function intersect($items)
  200. {
  201. return new static(array_intersect($this->items, $this->getArrayableItems($items)));
  202. }
  203. /**
  204. * Determine if the collection is empty or not.
  205. *
  206. * @return bool
  207. */
  208. public function isEmpty()
  209. {
  210. return empty($this->items);
  211. }
  212. /**
  213. * Get the last item from the collection.
  214. *
  215. * @return mixed|null
  216. */
  217. public function last()
  218. {
  219. return count($this->items) > 0 ? end($this->items) : null;
  220. }
  221. /**
  222. * Get an array with the values of a given key.
  223. *
  224. * @param string $value
  225. * @param string $key
  226. * @return array
  227. */
  228. public function lists($value, $key = null)
  229. {
  230. return array_pluck($this->items, $value, $key);
  231. }
  232. /**
  233. * Run a map over each of the items.
  234. *
  235. * @param Closure $callback
  236. * @return \Illuminate\Support\Collection
  237. */
  238. public function map(Closure $callback)
  239. {
  240. return new static(array_map($callback, $this->items, array_keys($this->items)));
  241. }
  242. /**
  243. * Merge the collection with the given items.
  244. *
  245. * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
  246. * @return \Illuminate\Support\Collection
  247. */
  248. public function merge($items)
  249. {
  250. return new static(array_merge($this->items, $this->getArrayableItems($items)));
  251. }
  252. /**
  253. * Get and remove the last item from the collection.
  254. *
  255. * @return mixed|null
  256. */
  257. public function pop()
  258. {
  259. return array_pop($this->items);
  260. }
  261. /**
  262. * Push an item onto the beginning of the collection.
  263. *
  264. * @param mixed $value
  265. * @return void
  266. */
  267. public function prepend($value)
  268. {
  269. array_unshift($this->items, $value);
  270. }
  271. /**
  272. * Push an item onto the end of the collection.
  273. *
  274. * @param mixed $value
  275. * @return void
  276. */
  277. public function push($value)
  278. {
  279. $this->items[] = $value;
  280. }
  281. /**
  282. * Pulls an item from the collection.
  283. *
  284. * @param mixed $key
  285. * @param mixed $default
  286. * @return mixed
  287. */
  288. public function pull($key, $default = null)
  289. {
  290. return array_pull($this->items, $key, $default);
  291. }
  292. /**
  293. * Put an item in the collection by key.
  294. *
  295. * @param mixed $key
  296. * @param mixed $value
  297. * @return void
  298. */
  299. public function put($key, $value)
  300. {
  301. $this->items[$key] = $value;
  302. }
  303. /**
  304. * Reduce the collection to a single value.
  305. *
  306. * @param callable $callback
  307. * @param mixed $initial
  308. * @return mixed
  309. */
  310. public function reduce($callback, $initial = null)
  311. {
  312. return array_reduce($this->items, $callback, $initial);
  313. }
  314. /**
  315. * Get one or more items randomly from the collection.
  316. *
  317. * @param int $amount
  318. * @return mixed
  319. */
  320. public function random($amount = 1)
  321. {
  322. $keys = array_rand($this->items, $amount);
  323. return is_array($keys) ? array_intersect_key($this->items, array_flip($keys)) : $this->items[$keys];
  324. }
  325. /**
  326. * Reverse items order.
  327. *
  328. * @return \Illuminate\Support\Collection
  329. */
  330. public function reverse()
  331. {
  332. return new static(array_reverse($this->items));
  333. }
  334. /**
  335. * Get and remove the first item from the collection.
  336. *
  337. * @return mixed|null
  338. */
  339. public function shift()
  340. {
  341. return array_shift($this->items);
  342. }
  343. /**
  344. * Slice the underlying collection array.
  345. *
  346. * @param int $offset
  347. * @param int $length
  348. * @param bool $preserveKeys
  349. * @return \Illuminate\Support\Collection
  350. */
  351. public function slice($offset, $length = null, $preserveKeys = false)
  352. {
  353. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  354. }
  355. /**
  356. * Chunk the underlying collection array.
  357. *
  358. * @param int $size
  359. * @param bool $preserveKeys
  360. * @return \Illuminate\Support\Collection
  361. */
  362. public function chunk($size, $preserveKeys = false)
  363. {
  364. $chunks = new static;
  365. foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk)
  366. {
  367. $chunks->push(new static($chunk));
  368. }
  369. return $chunks;
  370. }
  371. /**
  372. * Sort through each item with a callback.
  373. *
  374. * @param Closure $callback
  375. * @return \Illuminate\Support\Collection
  376. */
  377. public function sort(Closure $callback)
  378. {
  379. uasort($this->items, $callback);
  380. return $this;
  381. }
  382. /**
  383. * Sort the collection using the given Closure.
  384. *
  385. * @param \Closure|string $callback
  386. * @param int $options
  387. * @param bool $descending
  388. * @return \Illuminate\Support\Collection
  389. */
  390. public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
  391. {
  392. $results = array();
  393. if (is_string($callback)) $callback =
  394. $this->valueRetriever($callback);
  395. // First we will loop through the items and get the comparator from a callback
  396. // function which we were given. Then, we will sort the returned values and
  397. // and grab the corresponding values for the sorted keys from this array.
  398. foreach ($this->items as $key => $value)
  399. {
  400. $results[$key] = $callback($value);
  401. }
  402. $descending ? arsort($results, $options)
  403. : asort($results, $options);
  404. // Once we have sorted all of the keys in the array, we will loop through them
  405. // and grab the corresponding model so we can set the underlying items list
  406. // to the sorted version. Then we'll just return the collection instance.
  407. foreach (array_keys($results) as $key)
  408. {
  409. $results[$key] = $this->items[$key];
  410. }
  411. $this->items = $results;
  412. return $this;
  413. }
  414. /**
  415. * Sort the collection in descending order using the given Closure.
  416. *
  417. * @param \Closure|string $callback
  418. * @param int $options
  419. * @return \Illuminate\Support\Collection
  420. */
  421. public function sortByDesc($callback, $options = SORT_REGULAR)
  422. {
  423. return $this->sortBy($callback, $options, true);
  424. }
  425. /**
  426. * Splice portion of the underlying collection array.
  427. *
  428. * @param int $offset
  429. * @param int $length
  430. * @param mixed $replacement
  431. * @return \Illuminate\Support\Collection
  432. */
  433. public function splice($offset, $length = 0, $replacement = array())
  434. {
  435. return new static(array_splice($this->items, $offset, $length, $replacement));
  436. }
  437. /**
  438. * Get the sum of the given values.
  439. *
  440. * @param \Closure $callback
  441. * @param string $callback
  442. * @return mixed
  443. */
  444. public function sum($callback)
  445. {
  446. if (is_string($callback))
  447. {
  448. $callback = $this->valueRetriever($callback);
  449. }
  450. return $this->reduce(function($result, $item) use ($callback)
  451. {
  452. return $result += $callback($item);
  453. }, 0);
  454. }
  455. /**
  456. * Take the first or last {$limit} items.
  457. *
  458. * @param int $limit
  459. * @return \Illuminate\Support\Collection
  460. */
  461. public function take($limit = null)
  462. {
  463. if ($limit < 0) return $this->slice($limit, abs($limit));
  464. return $this->slice(0, $limit);
  465. }
  466. /**
  467. * Transform each item in the collection using a callback.
  468. *
  469. * @param Closure $callback
  470. * @return \Illuminate\Support\Collection
  471. */
  472. public function transform(Closure $callback)
  473. {
  474. $this->items = array_map($callback, $this->items);
  475. return $this;
  476. }
  477. /**
  478. * Return only unique items from the collection array.
  479. *
  480. * @return \Illuminate\Support\Collection
  481. */
  482. public function unique()
  483. {
  484. return new static(array_unique($this->items));
  485. }
  486. /**
  487. * Reset the keys on the underlying array.
  488. *
  489. * @return \Illuminate\Support\Collection
  490. */
  491. public function values()
  492. {
  493. $this->items = array_values($this->items);
  494. return $this;
  495. }
  496. /**
  497. * Get a value retrieving callback.
  498. *
  499. * @param string $value
  500. * @return \Closure
  501. */
  502. protected function valueRetriever($value)
  503. {
  504. return function($item) use ($value)
  505. {
  506. return data_get($item, $value);
  507. };
  508. }
  509. /**
  510. * Get the collection of items as a plain array.
  511. *
  512. * @return array
  513. */
  514. public function toArray()
  515. {
  516. return array_map(function($value)
  517. {
  518. return $value instanceof ArrayableInterface ? $value->toArray() : $value;
  519. }, $this->items);
  520. }
  521. /**
  522. * Get the collection of items as JSON.
  523. *
  524. * @param int $options
  525. * @return string
  526. */
  527. public function toJson($options = 0)
  528. {
  529. return json_encode($this->toArray(), $options);
  530. }
  531. /**
  532. * Get an iterator for the items.
  533. *
  534. * @return ArrayIterator
  535. */
  536. public function getIterator()
  537. {
  538. return new ArrayIterator($this->items);
  539. }
  540. /**
  541. * Get a CachingIterator instance.
  542. *
  543. * @return \CachingIterator
  544. */
  545. public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
  546. {
  547. return new CachingIterator($this->getIterator(), $flags);
  548. }
  549. /**
  550. * Count the number of items in the collection.
  551. *
  552. * @return int
  553. */
  554. public function count()
  555. {
  556. return count($this->items);
  557. }
  558. /**
  559. * Determine if an item exists at an offset.
  560. *
  561. * @param mixed $key
  562. * @return bool
  563. */
  564. public function offsetExists($key)
  565. {
  566. return array_key_exists($key, $this->items);
  567. }
  568. /**
  569. * Get an item at a given offset.
  570. *
  571. * @param mixed $key
  572. * @return mixed
  573. */
  574. public function offsetGet($key)
  575. {
  576. return $this->items[$key];
  577. }
  578. /**
  579. * Set the item at a given offset.
  580. *
  581. * @param mixed $key
  582. * @param mixed $value
  583. * @return void
  584. */
  585. public function offsetSet($key, $value)
  586. {
  587. if (is_null($key))
  588. {
  589. $this->items[] = $value;
  590. }
  591. else
  592. {
  593. $this->items[$key] = $value;
  594. }
  595. }
  596. /**
  597. * Unset the item at a given offset.
  598. *
  599. * @param string $key
  600. * @return void
  601. */
  602. public function offsetUnset($key)
  603. {
  604. unset($this->items[$key]);
  605. }
  606. /**
  607. * Convert the collection to its string representation.
  608. *
  609. * @return string
  610. */
  611. public function __toString()
  612. {
  613. return $this->toJson();
  614. }
  615. /**
  616. * Results array of items from Collection or ArrayableInterface.
  617. *
  618. * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
  619. * @return array
  620. */
  621. private function getArrayableItems($items)
  622. {
  623. if ($items instanceof Collection)
  624. {
  625. $items = $items->all();
  626. }
  627. elseif ($items instanceof ArrayableInterface)
  628. {
  629. $items = $items->toArray();
  630. }
  631. return $items;
  632. }
  633. }