PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/milton2913/myBlog
PHP | 364 lines | 172 code | 53 blank | 139 comment | 14 complexity | ae1c093954f9db2040834d220eacd332 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use LogicException;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Contracts\Queue\QueueableCollection;
  6. use Illuminate\Support\Collection as BaseCollection;
  7. class Collection extends BaseCollection implements QueueableCollection
  8. {
  9. /**
  10. * Find a model in the collection by key.
  11. *
  12. * @param mixed $key
  13. * @param mixed $default
  14. * @return \Illuminate\Database\Eloquent\Model
  15. */
  16. public function find($key, $default = null)
  17. {
  18. if ($key instanceof Model) {
  19. $key = $key->getKey();
  20. }
  21. return Arr::first($this->items, function ($model) use ($key) {
  22. return $model->getKey() == $key;
  23. }, $default);
  24. }
  25. /**
  26. * Load a set of relationships onto the collection.
  27. *
  28. * @param mixed $relations
  29. * @return $this
  30. */
  31. public function load($relations)
  32. {
  33. if (count($this->items) > 0) {
  34. if (is_string($relations)) {
  35. $relations = func_get_args();
  36. }
  37. $query = $this->first()->newQuery()->with($relations);
  38. $this->items = $query->eagerLoadRelations($this->items);
  39. }
  40. return $this;
  41. }
  42. /**
  43. * Add an item to the collection.
  44. *
  45. * @param mixed $item
  46. * @return $this
  47. */
  48. public function add($item)
  49. {
  50. $this->items[] = $item;
  51. return $this;
  52. }
  53. /**
  54. * Determine if a key exists in the collection.
  55. *
  56. * @param mixed $key
  57. * @param mixed $value
  58. * @return bool
  59. */
  60. public function contains($key, $value = null)
  61. {
  62. if (func_num_args() == 2) {
  63. return parent::contains($key, $value);
  64. }
  65. if ($this->useAsCallable($key)) {
  66. return parent::contains($key);
  67. }
  68. $key = $key instanceof Model ? $key->getKey() : $key;
  69. return parent::contains(function ($model) use ($key) {
  70. return $model->getKey() == $key;
  71. });
  72. }
  73. /**
  74. * Get the array of primary keys.
  75. *
  76. * @return array
  77. */
  78. public function modelKeys()
  79. {
  80. return array_map(function ($model) {
  81. return $model->getKey();
  82. }, $this->items);
  83. }
  84. /**
  85. * Merge the collection with the given items.
  86. *
  87. * @param \ArrayAccess|array $items
  88. * @return static
  89. */
  90. public function merge($items)
  91. {
  92. $dictionary = $this->getDictionary();
  93. foreach ($items as $item) {
  94. $dictionary[$item->getKey()] = $item;
  95. }
  96. return new static(array_values($dictionary));
  97. }
  98. /**
  99. * Run a map over each of the items.
  100. *
  101. * @param callable $callback
  102. * @return \Illuminate\Support\Collection
  103. */
  104. public function map(callable $callback)
  105. {
  106. $result = parent::map($callback);
  107. return $result->contains(function ($item) {
  108. return ! $item instanceof Model;
  109. }) ? $result->toBase() : $result;
  110. }
  111. /**
  112. * Diff the collection with the given items.
  113. *
  114. * @param \ArrayAccess|array $items
  115. * @return static
  116. */
  117. public function diff($items)
  118. {
  119. $diff = new static;
  120. $dictionary = $this->getDictionary($items);
  121. foreach ($this->items as $item) {
  122. if (! isset($dictionary[$item->getKey()])) {
  123. $diff->add($item);
  124. }
  125. }
  126. return $diff;
  127. }
  128. /**
  129. * Intersect the collection with the given items.
  130. *
  131. * @param \ArrayAccess|array $items
  132. * @return static
  133. */
  134. public function intersect($items)
  135. {
  136. $intersect = new static;
  137. $dictionary = $this->getDictionary($items);
  138. foreach ($this->items as $item) {
  139. if (isset($dictionary[$item->getKey()])) {
  140. $intersect->add($item);
  141. }
  142. }
  143. return $intersect;
  144. }
  145. /**
  146. * Return only unique items from the collection.
  147. *
  148. * @param string|callable|null $key
  149. * @param bool $strict
  150. * @return static|\Illuminate\Support\Collection
  151. */
  152. public function unique($key = null, $strict = false)
  153. {
  154. if (! is_null($key)) {
  155. return parent::unique($key, $strict);
  156. }
  157. return new static(array_values($this->getDictionary()));
  158. }
  159. /**
  160. * Returns only the models from the collection with the specified keys.
  161. *
  162. * @param mixed $keys
  163. * @return static
  164. */
  165. public function only($keys)
  166. {
  167. if (is_null($keys)) {
  168. return new static($this->items);
  169. }
  170. $dictionary = Arr::only($this->getDictionary(), $keys);
  171. return new static(array_values($dictionary));
  172. }
  173. /**
  174. * Returns all models in the collection except the models with specified keys.
  175. *
  176. * @param mixed $keys
  177. * @return static
  178. */
  179. public function except($keys)
  180. {
  181. $dictionary = Arr::except($this->getDictionary(), $keys);
  182. return new static(array_values($dictionary));
  183. }
  184. /**
  185. * Make the given, typically visible, attributes hidden across the entire collection.
  186. *
  187. * @param array|string $attributes
  188. * @return $this
  189. */
  190. public function makeHidden($attributes)
  191. {
  192. return $this->each(function ($model) use ($attributes) {
  193. $model->addHidden($attributes);
  194. });
  195. }
  196. /**
  197. * Make the given, typically hidden, attributes visible across the entire collection.
  198. *
  199. * @param array|string $attributes
  200. * @return $this
  201. */
  202. public function makeVisible($attributes)
  203. {
  204. return $this->each(function ($model) use ($attributes) {
  205. $model->makeVisible($attributes);
  206. });
  207. }
  208. /**
  209. * Get a dictionary keyed by primary keys.
  210. *
  211. * @param \ArrayAccess|array|null $items
  212. * @return array
  213. */
  214. public function getDictionary($items = null)
  215. {
  216. $items = is_null($items) ? $this->items : $items;
  217. $dictionary = [];
  218. foreach ($items as $value) {
  219. $dictionary[$value->getKey()] = $value;
  220. }
  221. return $dictionary;
  222. }
  223. /**
  224. * The following methods are intercepted to always return base collections.
  225. */
  226. /**
  227. * Get an array with the values of a given key.
  228. *
  229. * @param string $value
  230. * @param string|null $key
  231. * @return \Illuminate\Support\Collection
  232. */
  233. public function pluck($value, $key = null)
  234. {
  235. return $this->toBase()->pluck($value, $key);
  236. }
  237. /**
  238. * Get the keys of the collection items.
  239. *
  240. * @return \Illuminate\Support\Collection
  241. */
  242. public function keys()
  243. {
  244. return $this->toBase()->keys();
  245. }
  246. /**
  247. * Zip the collection together with one or more arrays.
  248. *
  249. * @param mixed ...$items
  250. * @return \Illuminate\Support\Collection
  251. */
  252. public function zip($items)
  253. {
  254. return call_user_func_array([$this->toBase(), 'zip'], func_get_args());
  255. }
  256. /**
  257. * Collapse the collection of items into a single array.
  258. *
  259. * @return \Illuminate\Support\Collection
  260. */
  261. public function collapse()
  262. {
  263. return $this->toBase()->collapse();
  264. }
  265. /**
  266. * Get a flattened array of the items in the collection.
  267. *
  268. * @param int $depth
  269. * @return \Illuminate\Support\Collection
  270. */
  271. public function flatten($depth = INF)
  272. {
  273. return $this->toBase()->flatten($depth);
  274. }
  275. /**
  276. * Flip the items in the collection.
  277. *
  278. * @return \Illuminate\Support\Collection
  279. */
  280. public function flip()
  281. {
  282. return $this->toBase()->flip();
  283. }
  284. /**
  285. * Get the type of the entities being queued.
  286. *
  287. * @return string|null
  288. */
  289. public function getQueueableClass()
  290. {
  291. if ($this->count() === 0) {
  292. return;
  293. }
  294. $class = get_class($this->first());
  295. $this->each(function ($model) use ($class) {
  296. if (get_class($model) !== $class) {
  297. throw new LogicException('Queueing collections with multiple model types is not supported.');
  298. }
  299. });
  300. return $class;
  301. }
  302. /**
  303. * Get the identifiers for all of the entities.
  304. *
  305. * @return array
  306. */
  307. public function getQueueableIds()
  308. {
  309. return $this->modelKeys();
  310. }
  311. }