PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/daniruizcamacho/pfcascensores
PHP | 705 lines | 298 code | 83 blank | 324 comment | 10 complexity | a37d55f7437d19c0699c16af695dace7 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. * Put an item in the collection by key.
  283. *
  284. * @param mixed $key
  285. * @param mixed $value
  286. * @return void
  287. */
  288. public function put($key, $value)
  289. {
  290. $this->items[$key] = $value;
  291. }
  292. /**
  293. * Reduce the collection to a single value.
  294. *
  295. * @param callable $callback
  296. * @param mixed $initial
  297. * @return mixed
  298. */
  299. public function reduce($callback, $initial = null)
  300. {
  301. return array_reduce($this->items, $callback, $initial);
  302. }
  303. /**
  304. * Get one or more items randomly from the collection.
  305. *
  306. * @param int $amount
  307. * @return mixed
  308. */
  309. public function random($amount = 1)
  310. {
  311. $keys = array_rand($this->items, $amount);
  312. return is_array($keys) ? array_intersect_key($this->items, array_flip($keys)) : $this->items[$keys];
  313. }
  314. /**
  315. * Reverse items order.
  316. *
  317. * @return \Illuminate\Support\Collection
  318. */
  319. public function reverse()
  320. {
  321. return new static(array_reverse($this->items));
  322. }
  323. /**
  324. * Get and remove the first item from the collection.
  325. *
  326. * @return mixed|null
  327. */
  328. public function shift()
  329. {
  330. return array_shift($this->items);
  331. }
  332. /**
  333. * Slice the underlying collection array.
  334. *
  335. * @param int $offset
  336. * @param int $length
  337. * @param bool $preserveKeys
  338. * @return \Illuminate\Support\Collection
  339. */
  340. public function slice($offset, $length = null, $preserveKeys = false)
  341. {
  342. return new static(array_slice($this->items, $offset, $length, $preserveKeys));
  343. }
  344. /**
  345. * Chunk the underlying collection array.
  346. *
  347. * @param int $size
  348. * @param bool $preserveKeys
  349. * @return \Illuminate\Support\Collection
  350. */
  351. public function chunk($size, $preserveKeys = false)
  352. {
  353. $chunks = new static;
  354. foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk)
  355. {
  356. $chunks->push(new static($chunk));
  357. }
  358. return $chunks;
  359. }
  360. /**
  361. * Sort through each item with a callback.
  362. *
  363. * @param Closure $callback
  364. * @return \Illuminate\Support\Collection
  365. */
  366. public function sort(Closure $callback)
  367. {
  368. uasort($this->items, $callback);
  369. return $this;
  370. }
  371. /**
  372. * Sort the collection using the given Closure.
  373. *
  374. * @param \Closure|string $callback
  375. * @param int $options
  376. * @param bool $descending
  377. * @return \Illuminate\Support\Collection
  378. */
  379. public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
  380. {
  381. $results = array();
  382. if (is_string($callback)) $callback =
  383. $this->valueRetriever($callback);
  384. // First we will loop through the items and get the comparator from a callback
  385. // function which we were given. Then, we will sort the returned values and
  386. // and grab the corresponding values for the sorted keys from this array.
  387. foreach ($this->items as $key => $value)
  388. {
  389. $results[$key] = $callback($value);
  390. }
  391. $descending ? arsort($results, $options)
  392. : asort($results, $options);
  393. // Once we have sorted all of the keys in the array, we will loop through them
  394. // and grab the corresponding model so we can set the underlying items list
  395. // to the sorted version. Then we'll just return the collection instance.
  396. foreach (array_keys($results) as $key)
  397. {
  398. $results[$key] = $this->items[$key];
  399. }
  400. $this->items = $results;
  401. return $this;
  402. }
  403. /**
  404. * Sort the collection in descending order using the given Closure.
  405. *
  406. * @param \Closure|string $callback
  407. * @param int $options
  408. * @return \Illuminate\Support\Collection
  409. */
  410. public function sortByDesc($callback, $options = SORT_REGULAR)
  411. {
  412. return $this->sortBy($callback, $options, true);
  413. }
  414. /**
  415. * Splice portion of the underlying collection array.
  416. *
  417. * @param int $offset
  418. * @param int $length
  419. * @param mixed $replacement
  420. * @return \Illuminate\Support\Collection
  421. */
  422. public function splice($offset, $length = 0, $replacement = array())
  423. {
  424. return new static(array_splice($this->items, $offset, $length, $replacement));
  425. }
  426. /**
  427. * Get the sum of the given values.
  428. *
  429. * @param \Closure $callback
  430. * @param string $callback
  431. * @return mixed
  432. */
  433. public function sum($callback)
  434. {
  435. if (is_string($callback))
  436. {
  437. $callback = $this->valueRetriever($callback);
  438. }
  439. return $this->reduce(function($result, $item) use ($callback)
  440. {
  441. return $result += $callback($item);
  442. }, 0);
  443. }
  444. /**
  445. * Take the first or last {$limit} items.
  446. *
  447. * @param int $limit
  448. * @return \Illuminate\Support\Collection
  449. */
  450. public function take($limit = null)
  451. {
  452. if ($limit < 0) return $this->slice($limit, abs($limit));
  453. return $this->slice(0, $limit);
  454. }
  455. /**
  456. * Transform each item in the collection using a callback.
  457. *
  458. * @param Closure $callback
  459. * @return \Illuminate\Support\Collection
  460. */
  461. public function transform(Closure $callback)
  462. {
  463. $this->items = array_map($callback, $this->items);
  464. return $this;
  465. }
  466. /**
  467. * Return only unique items from the collection array.
  468. *
  469. * @return \Illuminate\Support\Collection
  470. */
  471. public function unique()
  472. {
  473. return new static(array_unique($this->items));
  474. }
  475. /**
  476. * Reset the keys on the underlying array.
  477. *
  478. * @return \Illuminate\Support\Collection
  479. */
  480. public function values()
  481. {
  482. $this->items = array_values($this->items);
  483. return $this;
  484. }
  485. /**
  486. * Get a value retrieving callback.
  487. *
  488. * @param string $value
  489. * @return \Closure
  490. */
  491. protected function valueRetriever($value)
  492. {
  493. return function($item) use ($value)
  494. {
  495. return is_object($item) ? $item->{$value} : array_get($item, $value);
  496. };
  497. }
  498. /**
  499. * Get the collection of items as a plain array.
  500. *
  501. * @return array
  502. */
  503. public function toArray()
  504. {
  505. return array_map(function($value)
  506. {
  507. return $value instanceof ArrayableInterface ? $value->toArray() : $value;
  508. }, $this->items);
  509. }
  510. /**
  511. * Get the collection of items as JSON.
  512. *
  513. * @param int $options
  514. * @return string
  515. */
  516. public function toJson($options = 0)
  517. {
  518. return json_encode($this->toArray(), $options);
  519. }
  520. /**
  521. * Get an iterator for the items.
  522. *
  523. * @return ArrayIterator
  524. */
  525. public function getIterator()
  526. {
  527. return new ArrayIterator($this->items);
  528. }
  529. /**
  530. * Get a CachingIterator instance.
  531. *
  532. * @return \CachingIterator
  533. */
  534. public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
  535. {
  536. return new CachingIterator($this->getIterator(), $flags);
  537. }
  538. /**
  539. * Count the number of items in the collection.
  540. *
  541. * @return int
  542. */
  543. public function count()
  544. {
  545. return count($this->items);
  546. }
  547. /**
  548. * Determine if an item exists at an offset.
  549. *
  550. * @param mixed $key
  551. * @return bool
  552. */
  553. public function offsetExists($key)
  554. {
  555. return array_key_exists($key, $this->items);
  556. }
  557. /**
  558. * Get an item at a given offset.
  559. *
  560. * @param mixed $key
  561. * @return mixed
  562. */
  563. public function offsetGet($key)
  564. {
  565. return $this->items[$key];
  566. }
  567. /**
  568. * Set the item at a given offset.
  569. *
  570. * @param mixed $key
  571. * @param mixed $value
  572. * @return void
  573. */
  574. public function offsetSet($key, $value)
  575. {
  576. if (is_null($key))
  577. {
  578. $this->items[] = $value;
  579. }
  580. else
  581. {
  582. $this->items[$key] = $value;
  583. }
  584. }
  585. /**
  586. * Unset the item at a given offset.
  587. *
  588. * @param string $key
  589. * @return void
  590. */
  591. public function offsetUnset($key)
  592. {
  593. unset($this->items[$key]);
  594. }
  595. /**
  596. * Convert the collection to its string representation.
  597. *
  598. * @return string
  599. */
  600. public function __toString()
  601. {
  602. return $this->toJson();
  603. }
  604. /**
  605. * Results array of items from Collection or ArrayableInterface.
  606. *
  607. * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
  608. * @return array
  609. */
  610. private function getArrayableItems($items)
  611. {
  612. if ($items instanceof Collection)
  613. {
  614. $items = $items->all();
  615. }
  616. elseif ($items instanceof ArrayableInterface)
  617. {
  618. $items = $items->toArray();
  619. }
  620. return $items;
  621. }
  622. }