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

https://gitlab.com/techniconline/kmc · PHP · 254 lines · 117 code · 42 blank · 95 comment · 12 complexity · e7dc8a26f69ce53b75ad6e9061f6b5cd MD5 · raw file

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