/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 255 lines · 122 code · 41 blank · 92 comment · 11 complexity · 805764002775553b58a5d37b6f4541c7 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Collection as BaseCollection;
  5. class Collection extends BaseCollection
  6. {
  7. /**
  8. * Find a model in the collection by key.
  9. *
  10. * @param mixed $key
  11. * @param mixed $default
  12. * @return \Illuminate\Database\Eloquent\Model
  13. */
  14. public function find($key, $default = null)
  15. {
  16. if ($key instanceof Model) {
  17. $key = $key->getKey();
  18. }
  19. return Arr::first($this->items, function ($itemKey, $model) use ($key) {
  20. return $model->getKey() == $key;
  21. }, $default);
  22. }
  23. /**
  24. * Load a set of relationships onto the collection.
  25. *
  26. * @param mixed $relations
  27. * @return $this
  28. */
  29. public function load($relations)
  30. {
  31. if (count($this->items) > 0) {
  32. if (is_string($relations)) {
  33. $relations = func_get_args();
  34. }
  35. $query = $this->first()->newQuery()->with($relations);
  36. $this->items = $query->eagerLoadRelations($this->items);
  37. }
  38. return $this;
  39. }
  40. /**
  41. * Add an item to the collection.
  42. *
  43. * @param mixed $item
  44. * @return $this
  45. */
  46. public function add($item)
  47. {
  48. $this->items[] = $item;
  49. return $this;
  50. }
  51. /**
  52. * Determine if a key exists in the collection.
  53. *
  54. * @param mixed $key
  55. * @param mixed $value
  56. * @return bool
  57. */
  58. public function contains($key, $value = null)
  59. {
  60. if (func_num_args() == 2) {
  61. return parent::contains($key, $value);
  62. }
  63. if ($this->useAsCallable($key)) {
  64. return parent::contains($key);
  65. }
  66. $key = $key instanceof Model ? $key->getKey() : $key;
  67. return parent::contains(function ($k, $m) use ($key) {
  68. return $m->getKey() == $key;
  69. });
  70. }
  71. /**
  72. * Fetch a nested element of the collection.
  73. *
  74. * @param string $key
  75. * @return static
  76. *
  77. * @deprecated since version 5.1. Use pluck instead.
  78. */
  79. public function fetch($key)
  80. {
  81. return new static(Arr::fetch($this->toArray(), $key));
  82. }
  83. /**
  84. * Get the array of primary keys.
  85. *
  86. * @return array
  87. */
  88. public function modelKeys()
  89. {
  90. return array_map(function ($m) {
  91. return $m->getKey();
  92. }, $this->items);
  93. }
  94. /**
  95. * Merge the collection with the given items.
  96. *
  97. * @param \ArrayAccess|array $items
  98. * @return static
  99. */
  100. public function merge($items)
  101. {
  102. $dictionary = $this->getDictionary();
  103. foreach ($items as $item) {
  104. $dictionary[$item->getKey()] = $item;
  105. }
  106. return new static(array_values($dictionary));
  107. }
  108. /**
  109. * Diff the collection with the given items.
  110. *
  111. * @param \ArrayAccess|array $items
  112. * @return static
  113. */
  114. public function diff($items)
  115. {
  116. $diff = new static;
  117. $dictionary = $this->getDictionary($items);
  118. foreach ($this->items as $item) {
  119. if (! isset($dictionary[$item->getKey()])) {
  120. $diff->add($item);
  121. }
  122. }
  123. return $diff;
  124. }
  125. /**
  126. * Intersect the collection with the given items.
  127. *
  128. * @param \ArrayAccess|array $items
  129. * @return static
  130. */
  131. public function intersect($items)
  132. {
  133. $intersect = new static;
  134. $dictionary = $this->getDictionary($items);
  135. foreach ($this->items as $item) {
  136. if (isset($dictionary[$item->getKey()])) {
  137. $intersect->add($item);
  138. }
  139. }
  140. return $intersect;
  141. }
  142. /**
  143. * Return only unique items from the collection.
  144. *
  145. * @param string|callable|null $key
  146. * @return static
  147. */
  148. public function unique($key = null)
  149. {
  150. if (! is_null($key)) {
  151. return parent::unique($key);
  152. }
  153. return new static(array_values($this->getDictionary()));
  154. }
  155. /**
  156. * Returns only the models from the collection with the specified keys.
  157. *
  158. * @param mixed $keys
  159. * @return static
  160. */
  161. public function only($keys)
  162. {
  163. $dictionary = Arr::only($this->getDictionary(), $keys);
  164. return new static(array_values($dictionary));
  165. }
  166. /**
  167. * Returns all models in the collection except the models with specified keys.
  168. *
  169. * @param mixed $keys
  170. * @return static
  171. */
  172. public function except($keys)
  173. {
  174. $dictionary = Arr::except($this->getDictionary(), $keys);
  175. return new static(array_values($dictionary));
  176. }
  177. /**
  178. * Make the given, typically hidden, attributes visible across the entire collection.
  179. *
  180. * @param array|string $attributes
  181. * @return $this
  182. */
  183. public function withHidden($attributes)
  184. {
  185. $this->each(function ($model) use ($attributes) {
  186. $model->withHidden($attributes);
  187. });
  188. return $this;
  189. }
  190. /**
  191. * Get a dictionary keyed by primary keys.
  192. *
  193. * @param \ArrayAccess|array $items
  194. * @return array
  195. */
  196. public function getDictionary($items = null)
  197. {
  198. $items = is_null($items) ? $this->items : $items;
  199. $dictionary = [];
  200. foreach ($items as $value) {
  201. $dictionary[$value->getKey()] = $value;
  202. }
  203. return $dictionary;
  204. }
  205. /**
  206. * Get a base Support collection instance from this collection.
  207. *
  208. * @return \Illuminate\Support\Collection
  209. */
  210. public function toBase()
  211. {
  212. return new BaseCollection($this->items);
  213. }
  214. }