PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ealexis.t/trends
PHP | 334 lines | 151 code | 48 blank | 135 comment | 11 complexity | f1e4736731fb5a9d41b1cbf6d00cecf5 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. * Get the array of primary keys.
  73. *
  74. * @return array
  75. */
  76. public function modelKeys()
  77. {
  78. return array_map(function ($m) {
  79. return $m->getKey();
  80. }, $this->items);
  81. }
  82. /**
  83. * Merge the collection with the given items.
  84. *
  85. * @param \ArrayAccess|array $items
  86. * @return static
  87. */
  88. public function merge($items)
  89. {
  90. $dictionary = $this->getDictionary();
  91. foreach ($items as $item) {
  92. $dictionary[$item->getKey()] = $item;
  93. }
  94. return new static(array_values($dictionary));
  95. }
  96. /**
  97. * Diff the collection with the given items.
  98. *
  99. * @param \ArrayAccess|array $items
  100. * @return static
  101. */
  102. public function diff($items)
  103. {
  104. $diff = new static;
  105. $dictionary = $this->getDictionary($items);
  106. foreach ($this->items as $item) {
  107. if (! isset($dictionary[$item->getKey()])) {
  108. $diff->add($item);
  109. }
  110. }
  111. return $diff;
  112. }
  113. /**
  114. * Intersect the collection with the given items.
  115. *
  116. * @param \ArrayAccess|array $items
  117. * @return static
  118. */
  119. public function intersect($items)
  120. {
  121. $intersect = new static;
  122. $dictionary = $this->getDictionary($items);
  123. foreach ($this->items as $item) {
  124. if (isset($dictionary[$item->getKey()])) {
  125. $intersect->add($item);
  126. }
  127. }
  128. return $intersect;
  129. }
  130. /**
  131. * Return only unique items from the collection.
  132. *
  133. * @param string|callable|null $key
  134. * @return static
  135. */
  136. public function unique($key = null)
  137. {
  138. if (! is_null($key)) {
  139. return parent::unique($key);
  140. }
  141. return new static(array_values($this->getDictionary()));
  142. }
  143. /**
  144. * Returns only the models from the collection with the specified keys.
  145. *
  146. * @param mixed $keys
  147. * @return static
  148. */
  149. public function only($keys)
  150. {
  151. $dictionary = Arr::only($this->getDictionary(), $keys);
  152. return new static(array_values($dictionary));
  153. }
  154. /**
  155. * Returns all models in the collection except the models with specified keys.
  156. *
  157. * @param mixed $keys
  158. * @return static
  159. */
  160. public function except($keys)
  161. {
  162. $dictionary = Arr::except($this->getDictionary(), $keys);
  163. return new static(array_values($dictionary));
  164. }
  165. /**
  166. * Make the given, typically visible, attributes hidden across the entire collection.
  167. *
  168. * @param array|string $attributes
  169. * @return $this
  170. */
  171. public function makeHidden($attributes)
  172. {
  173. return $this->each(function ($model) use ($attributes) {
  174. $model->addHidden($attributes);
  175. });
  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 makeVisible($attributes)
  184. {
  185. return $this->each(function ($model) use ($attributes) {
  186. $model->makeVisible($attributes);
  187. });
  188. }
  189. /**
  190. * Make the given, typically hidden, attributes visible across the entire collection.
  191. *
  192. * @param array|string $attributes
  193. * @return $this
  194. *
  195. * @deprecated since version 5.2. Use the "makeVisible" method directly.
  196. */
  197. public function withHidden($attributes)
  198. {
  199. return $this->makeVisible($attributes);
  200. }
  201. /**
  202. * Get a dictionary keyed by primary keys.
  203. *
  204. * @param \ArrayAccess|array|null $items
  205. * @return array
  206. */
  207. public function getDictionary($items = null)
  208. {
  209. $items = is_null($items) ? $this->items : $items;
  210. $dictionary = [];
  211. foreach ($items as $value) {
  212. $dictionary[$value->getKey()] = $value;
  213. }
  214. return $dictionary;
  215. }
  216. /**
  217. * The following methods are intercepted to always return base collections.
  218. */
  219. /**
  220. * Get an array with the values of a given key.
  221. *
  222. * @param string $value
  223. * @param string|null $key
  224. * @return \Illuminate\Support\Collection
  225. */
  226. public function pluck($value, $key = null)
  227. {
  228. return $this->toBase()->pluck($value, $key);
  229. }
  230. /**
  231. * Get the keys of the collection items.
  232. *
  233. * @return \Illuminate\Support\Collection
  234. */
  235. public function keys()
  236. {
  237. return $this->toBase()->keys();
  238. }
  239. /**
  240. * Zip the collection together with one or more arrays.
  241. *
  242. * @param mixed ...$items
  243. * @return \Illuminate\Support\Collection
  244. */
  245. public function zip($items)
  246. {
  247. return call_user_func_array([$this->toBase(), 'zip'], func_get_args());
  248. }
  249. /**
  250. * Collapse the collection of items into a single array.
  251. *
  252. * @return \Illuminate\Support\Collection
  253. */
  254. public function collapse()
  255. {
  256. return $this->toBase()->collapse();
  257. }
  258. /**
  259. * Get a flattened array of the items in the collection.
  260. *
  261. * @param int $depth
  262. * @return \Illuminate\Support\Collection
  263. */
  264. public function flatten($depth = INF)
  265. {
  266. return $this->toBase()->flatten($depth);
  267. }
  268. /**
  269. * Flip the items in the collection.
  270. *
  271. * @return \Illuminate\Support\Collection
  272. */
  273. public function flip()
  274. {
  275. return $this->toBase()->flip();
  276. }
  277. /**
  278. * Get a base Support collection instance from this collection.
  279. *
  280. * @return \Illuminate\Support\Collection
  281. */
  282. public function toBase()
  283. {
  284. return new BaseCollection($this->items);
  285. }
  286. }