Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/fluent-smtp/includes/Support/Collection.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace FluentMail\Includes\Support;
4 +
5 + use Closure;
6 + use Countable;
7 + use ArrayAccess;
8 + use ArrayIterator;
9 + use CachingIterator;
10 + use JsonSerializable;
11 + use IteratorAggregate;
12 + use FluentMail\Includes\Support\Arr;
13 + use FluentMail\Includes\Support\Contracts\JsonableInterface;
14 + use FluentMail\Includes\Support\Contracts\ArrayableInterface;
15 +
16 + class Collection implements ArrayAccess, ArrayableInterface, Countable, IteratorAggregate, JsonableInterface, JsonSerializable {
17 +
18 + /**
19 + * The items contained in the collection.
20 + *
21 + * @var array
22 + */
23 + protected $items = array();
24 +
25 + /**
26 + * Create a new collection.
27 + *
28 + * @param array $items
29 + * @return void
30 + */
31 + public function __construct(array $items = array())
32 + {
33 + $this->items = $items;
34 + }
35 +
36 + /**
37 + * Create a new collection instance if the value isn't one already.
38 + *
39 + * @param mixed $items
40 + * @return static
41 + */
42 + public static function make($items)
43 + {
44 + if (is_null($items)) return new static;
45 +
46 + if ($items instanceof Collection) return $items;
47 +
48 + return new static(is_array($items) ? $items : array($items));
49 + }
50 +
51 + /**
52 + * Get all of the items in the collection.
53 + *
54 + * @return array
55 + */
56 + public function all()
57 + {
58 + return $this->items;
59 + }
60 +
61 + /**
62 + * Collapse the collection items into a single array.
63 + *
64 + * @return static
65 + */
66 + public function collapse()
67 + {
68 + $results = array();
69 +
70 + foreach ($this->items as $values)
71 + {
72 + if ($values instanceof Collection) $values = $values->all();
73 +
74 + $results = array_merge($results, $values);
75 + }
76 +
77 + return new static($results);
78 + }
79 +
80 + /**
81 + * Determine if an item exists in the collection.
82 + *
83 + * @param mixed $value
84 + * @return bool
85 + */
86 + public function contains($value)
87 + {
88 + if ($value instanceof Closure)
89 + {
90 + return ! is_null($this->first($value));
91 + }
92 +
93 + return in_array($value, $this->items);
94 + }
95 +
96 + /**
97 + * Diff the collection with the given items.
98 + *
99 + * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
100 + * @return static
101 + */
102 + public function diff($items)
103 + {
104 + return new static(array_diff($this->items, $this->getArrayableItems($items)));
105 + }
106 +
107 + /**
108 + * Execute a callback over each item.
109 + *
110 + * @param \Closure $callback
111 + * @return $this
112 + */
113 + public function each(Closure $callback)
114 + {
115 + array_map($callback, $this->items);
116 +
117 + return $this;
118 + }
119 +
120 + /**
121 + * Fetch a nested element of the collection.
122 + *
123 + * @param string $key
124 + * @return static
125 + */
126 + public function fetch($key)
127 + {
128 + return new static(Arr::fetch($this->items, $key));
129 + }
130 +
131 + /**
132 + * Run a filter over each of the items.
133 + *
134 + * @param \Closure $callback
135 + * @return static
136 + */
137 + public function filter(Closure $callback)
138 + {
139 + return new static(array_filter($this->items, $callback));
140 + }
141 +
142 + /**
143 + * Get the first item from the collection.
144 + *
145 + * @param \Closure $callback
146 + * @param mixed $default
147 + * @return mixed|null
148 + */
149 + public function first(?Closure $callback = null, $default = null)
150 + {
151 + if (is_null($callback))
152 + {
153 + return count($this->items) > 0 ? reset($this->items) : null;
154 + }
155 +
156 + return Arr::first($this->items, $callback, $default);
157 + }
158 +
159 + /**
160 + * Get a flattened array of the items in the collection.
161 + *
162 + * @return static
163 + */
164 + public function flatten()
165 + {
166 + return new static(Arr::flatten($this->items));
167 + }
168 +
169 + /**
170 + * Flip the items in the collection.
171 + *
172 + * @return static
173 + */
174 + public function flip()
175 + {
176 + return new static(array_flip($this->items));
177 + }
178 +
179 + /**
180 + * Remove an item from the collection by key.
181 + *
182 + * @param mixed $key
183 + * @return void
184 + */
185 + public function forget($key)
186 + {
187 + unset($this->items[$key]);
188 + }
189 +
190 + /**
191 + * Get an item from the collection by key.
192 + *
193 + * @param mixed $key
194 + * @param mixed $default
195 + * @return mixed
196 + */
197 + public function get($key, $default = null)
198 + {
199 + if ($this->offsetExists($key))
200 + {
201 + return $this->items[$key];
202 + }
203 +
204 + return value($default);
205 + }
206 +
207 + /**
208 + * Group an associative array by a field or Closure value.
209 + *
210 + * @param callable|string $groupBy
211 + * @return static
212 + */
213 + public function groupBy($groupBy)
214 + {
215 + $results = array();
216 +
217 + foreach ($this->items as $key => $value)
218 + {
219 + $results[$this->getGroupByKey($groupBy, $key, $value)][] = $value;
220 + }
221 +
222 + return new static($results);
223 + }
224 +
225 + /**
226 + * Get the "group by" key value.
227 + *
228 + * @param callable|string $groupBy
229 + * @param string $key
230 + * @param mixed $value
231 + * @return string
232 + */
233 + protected function getGroupByKey($groupBy, $key, $value)
234 + {
235 + if ( ! is_string($groupBy) && is_callable($groupBy))
236 + {
237 + return $groupBy($value, $key);
238 + }
239 +
240 + return Arr::getItem($value, $groupBy);
241 + }
242 +
243 + /**
244 + * Key an associative array by a field.
245 + *
246 + * @param string $keyBy
247 + * @return static
248 + */
249 + public function keyBy($keyBy)
250 + {
251 + $results = [];
252 +
253 + foreach ($this->items as $item)
254 + {
255 + $key = Arr::getItem($item, $keyBy);
256 +
257 + $results[$key] = $item;
258 + }
259 +
260 + return new static($results);
261 + }
262 +
263 + /**
264 + * Determine if an item exists in the collection by key.
265 + *
266 + * @param mixed $key
267 + * @return bool
268 + */
269 + public function has($key)
270 + {
271 + return $this->offsetExists($key);
272 + }
273 +
274 + /**
275 + * Concatenate values of a given key as a string.
276 + *
277 + * @param string $value
278 + * @param string $glue
279 + * @return string
280 + */
281 + public function implode($value, $glue = null)
282 + {
283 + return implode($glue, $this->lists($value));
284 + }
285 +
286 + /**
287 + * Intersect the collection with the given items.
288 + *
289 + * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
290 + * @return static
291 + */
292 + public function intersect($items)
293 + {
294 + return new static(array_intersect($this->items, $this->getArrayableItems($items)));
295 + }
296 +
297 + /**
298 + * Determine if the collection is empty or not.
299 + *
300 + * @return bool
301 + */
302 + public function isEmpty()
303 + {
304 + return empty($this->items);
305 + }
306 +
307 + /**
308 + * Get the keys of the collection items.
309 + *
310 + * @return array
311 + */
312 + public function keys()
313 + {
314 + return array_keys($this->items);
315 + }
316 +
317 + /**
318 + * Get the last item from the collection.
319 + *
320 + * @return mixed|null
321 + */
322 + public function last()
323 + {
324 + return count($this->items) > 0 ? end($this->items) : null;
325 + }
326 +
327 + /**
328 + * Get an array with the values of a given key.
329 + *
330 + * @param string $value
331 + * @param string $key
332 + * @return array
333 + */
334 + public function lists($value, $key = null)
335 + {
336 + return Arr::pluck($this->items, $value, $key);
337 + }
338 +
339 + /**
340 + * Run a map over each of the items.
341 + *
342 + * @param \Closure $callback
343 + * @return static
344 + */
345 + public function map(Closure $callback)
346 + {
347 + return new static(array_map($callback, $this->items, array_keys($this->items)));
348 + }
349 +
350 + /**
351 + * Merge the collection with the given items.
352 + *
353 + * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
354 + * @return static
355 + */
356 + public function merge($items)
357 + {
358 + return new static(array_merge($this->items, $this->getArrayableItems($items)));
359 + }
360 +
361 + /**
362 + * Get and remove the last item from the collection.
363 + *
364 + * @return mixed|null
365 + */
366 + public function pop()
367 + {
368 + return array_pop($this->items);
369 + }
370 +
371 + /**
372 + * Push an item onto the beginning of the collection.
373 + *
374 + * @param mixed $value
375 + * @return void
376 + */
377 + public function prepend($value)
378 + {
379 + array_unshift($this->items, $value);
380 + }
381 +
382 + /**
383 + * Push an item onto the end of the collection.
384 + *
385 + * @param mixed $value
386 + * @return void
387 + */
388 + public function push($value)
389 + {
390 + $this->items[] = $value;
391 + }
392 +
393 + /**
394 + * Pulls an item from the collection.
395 + *
396 + * @param mixed $key
397 + * @param mixed $default
398 + * @return mixed
399 + */
400 + public function pull($key, $default = null)
401 + {
402 + return Arr::pull($this->items, $key, $default);
403 + }
404 +
405 + /**
406 + * Put an item in the collection by key.
407 + *
408 + * @param mixed $key
409 + * @param mixed $value
410 + * @return void
411 + */
412 + public function put($key, $value)
413 + {
414 + $this->items[$key] = $value;
415 + }
416 +
417 + /**
418 + * Get one or more items randomly from the collection.
419 + *
420 + * @param int $amount
421 + * @return mixed
422 + */
423 + public function random($amount = 1)
424 + {
425 + if ($this->isEmpty()) return null;
426 +
427 + $keys = array_rand($this->items, $amount);
428 +
429 + return is_array($keys) ? array_intersect_key($this->items, array_flip($keys)) : $this->items[$keys];
430 + }
431 +
432 + /**
433 + * Reduce the collection to a single value.
434 + *
435 + * @param callable $callback
436 + * @param mixed $initial
437 + * @return mixed
438 + */
439 + public function reduce(callable $callback, $initial = null)
440 + {
441 + return array_reduce($this->items, $callback, $initial);
442 + }
443 +
444 + /**
445 + * Create a collection of all elements that do not pass a given truth test.
446 + *
447 + * @param \Closure|mixed $callback
448 + * @return static
449 + */
450 + public function reject($callback)
451 + {
452 + if ($callback instanceof Closure)
453 + {
454 + return $this->filter(function($item) use ($callback)
455 + {
456 + return ! $callback($item);
457 + });
458 + }
459 +
460 + return $this->filter(function($item) use ($callback)
461 + {
462 + return $item != $callback;
463 + });
464 + }
465 +
466 + /**
467 + * Reverse items order.
468 + *
469 + * @return static
470 + */
471 + public function reverse()
472 + {
473 + return new static(array_reverse($this->items));
474 + }
475 +
476 + /**
477 + * Search the collection for a given value and return the corresponding key if successful.
478 + *
479 + * @param mixed $value
480 + * @param bool $strict
481 + * @return mixed
482 + */
483 + public function search($value, $strict = false)
484 + {
485 + return array_search($value, $this->items, $strict);
486 + }
487 +
488 + /**
489 + * Get and remove the first item from the collection.
490 + *
491 + * @return mixed|null
492 + */
493 + public function shift()
494 + {
495 + return array_shift($this->items);
496 + }
497 +
498 + /**
499 + * Shuffle the items in the collection.
500 + *
501 + * @return $this
502 + */
503 + public function shuffle()
504 + {
505 + shuffle($this->items);
506 +
507 + return $this;
508 + }
509 +
510 + /**
511 + * Slice the underlying collection array.
512 + *
513 + * @param int $offset
514 + * @param int $length
515 + * @param bool $preserveKeys
516 + * @return static
517 + */
518 + public function slice($offset, $length = null, $preserveKeys = false)
519 + {
520 + return new static(array_slice($this->items, $offset, $length, $preserveKeys));
521 + }
522 +
523 + /**
524 + * Chunk the underlying collection array.
525 + *
526 + * @param int $size
527 + * @param bool $preserveKeys
528 + * @return static
529 + */
530 + public function chunk($size, $preserveKeys = false)
531 + {
532 + $chunks = new static;
533 +
534 + foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk)
535 + {
536 + $chunks->push(new static($chunk));
537 + }
538 +
539 + return $chunks;
540 + }
541 +
542 + /**
543 + * Sort through each item with a callback.
544 + *
545 + * @param \Closure $callback
546 + * @return $this
547 + */
548 + public function sort(Closure $callback)
549 + {
550 + uasort($this->items, $callback);
551 +
552 + return $this;
553 + }
554 +
555 + /**
556 + * Sort the collection using the given Closure.
557 + *
558 + * @param \Closure|string $callback
559 + * @param int $options
560 + * @param bool $descending
561 + * @return $this
562 + */
563 + public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
564 + {
565 + $results = array();
566 +
567 + if (is_string($callback)) $callback =
568 + $this->valueRetriever($callback);
569 +
570 + // First we will loop through the items and get the comparator from a callback
571 + // function which we were given. Then, we will sort the returned values and
572 + // and grab the corresponding values for the sorted keys from this array.
573 + foreach ($this->items as $key => $value)
574 + {
575 + $results[$key] = $callback($value);
576 + }
577 +
578 + $descending ? arsort($results, $options)
579 + : asort($results, $options);
580 +
581 + // Once we have sorted all of the keys in the array, we will loop through them
582 + // and grab the corresponding model so we can set the underlying items list
583 + // to the sorted version. Then we'll just return the collection instance.
584 + foreach (array_keys($results) as $key)
585 + {
586 + $results[$key] = $this->items[$key];
587 + }
588 +
589 + $this->items = $results;
590 +
591 + return $this;
592 + }
593 +
594 + /**
595 + * Sort the collection in descending order using the given Closure.
596 + *
597 + * @param \Closure|string $callback
598 + * @param int $options
599 + * @return $this
600 + */
601 + public function sortByDesc($callback, $options = SORT_REGULAR)
602 + {
603 + return $this->sortBy($callback, $options, true);
604 + }
605 +
606 + /**
607 + * Splice portion of the underlying collection array.
608 + *
609 + * @param int $offset
610 + * @param int $length
611 + * @param mixed $replacement
612 + * @return static
613 + */
614 + public function splice($offset, $length = 0, $replacement = array())
615 + {
616 + return new static(array_splice($this->items, $offset, $length, $replacement));
617 + }
618 +
619 + /**
620 + * Get the sum of the given values.
621 + *
622 + * @param \Closure $callback
623 + * @return mixed
624 + */
625 + public function sum($callback = null)
626 + {
627 + if (is_null($callback))
628 + {
629 + return array_sum($this->items);
630 + }
631 +
632 + if (is_string($callback))
633 + {
634 + $callback = $this->valueRetriever($callback);
635 + }
636 +
637 + return $this->reduce(function($result, $item) use ($callback)
638 + {
639 + return $result += $callback($item);
640 +
641 + }, 0);
642 + }
643 +
644 + /**
645 + * Take the first or last {$limit} items.
646 + *
647 + * @param int $limit
648 + * @return static
649 + */
650 + public function take($limit = null)
651 + {
652 + if ($limit < 0) return $this->slice($limit, abs($limit));
653 +
654 + return $this->slice(0, $limit);
655 + }
656 +
657 + /**
658 + * Transform each item in the collection using a callback.
659 + *
660 + * @param \Closure $callback
661 + * @return $this
662 + */
663 + public function transform(Closure $callback)
664 + {
665 + $this->items = array_map($callback, $this->items);
666 +
667 + return $this;
668 + }
669 +
670 + /**
671 + * Return only unique items from the collection array.
672 + *
673 + * @return static
674 + */
675 + public function unique()
676 + {
677 + return new static(array_unique($this->items));
678 + }
679 +
680 + /**
681 + * Reset the keys on the underlying array.
682 + *
683 + * @return static
684 + */
685 + public function values()
686 + {
687 + $this->items = array_values($this->items);
688 +
689 + return $this;
690 + }
691 +
692 + /**
693 + * Get a value retrieving callback.
694 + *
695 + * @param string $value
696 + * @return \Closure
697 + */
698 + protected function valueRetriever($value)
699 + {
700 + return function($item) use ($value)
701 + {
702 + return Arr::getItem($item, $value);
703 + };
704 + }
705 +
706 + /**
707 + * Get the collection of items as a plain array.
708 + *
709 + * @return array
710 + */
711 + public function toArray()
712 + {
713 + return array_map(function($value) {
714 + return $value instanceof ArrayableInterface ? $value->toArray() : $value;
715 + }, $this->items);
716 + }
717 +
718 + /**
719 + * Convert the object into something JSON serializable.
720 + *
721 + * @return array
722 + */
723 + public function jsonSerialize()
724 + {
725 + return $this->toArray();
726 + }
727 +
728 + /**
729 + * Get the collection of items as JSON.
730 + *
731 + * @param int $options
732 + * @return string
733 + */
734 + public function toJson($options = 0)
735 + {
736 + return json_encode($this->toArray(), $options);
737 + }
738 +
739 + /**
740 + * Get an iterator for the items.
741 + *
742 + * @return \ArrayIterator
743 + */
744 + public function getIterator()
745 + {
746 + return new ArrayIterator($this->items);
747 + }
748 +
749 + /**
750 + * Get a CachingIterator instance.
751 + *
752 + * @param int $flags
753 + * @return \CachingIterator
754 + */
755 + public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
756 + {
757 + return new CachingIterator($this->getIterator(), $flags);
758 + }
759 +
760 + /**
761 + * Count the number of items in the collection.
762 + *
763 + * @return int
764 + */
765 + public function count()
766 + {
767 + return count($this->items);
768 + }
769 +
770 + /**
771 + * Determine if an item exists at an offset.
772 + *
773 + * @param mixed $key
774 + * @return bool
775 + */
776 + public function offsetExists($key)
777 + {
778 + return array_key_exists($key, $this->items);
779 + }
780 +
781 + /**
782 + * Get an item at a given offset.
783 + *
784 + * @param mixed $key
785 + * @return mixed
786 + */
787 + public function offsetGet($key)
788 + {
789 + return $this->items[$key];
790 + }
791 +
792 + /**
793 + * Set the item at a given offset.
794 + *
795 + * @param mixed $key
796 + * @param mixed $value
797 + * @return void
798 + */
799 + public function offsetSet($key, $value)
800 + {
801 + if (is_null($key))
802 + {
803 + $this->items[] = $value;
804 + }
805 + else
806 + {
807 + $this->items[$key] = $value;
808 + }
809 + }
810 +
811 + /**
812 + * Unset the item at a given offset.
813 + *
814 + * @param string $key
815 + * @return void
816 + */
817 + public function offsetUnset($key)
818 + {
819 + unset($this->items[$key]);
820 + }
821 +
822 + /**
823 + * Convert the collection to its string representation.
824 + *
825 + * @return string
826 + */
827 + public function __toString()
828 + {
829 + return $this->toJson();
830 + }
831 +
832 + /**
833 + * Results array of items from Collection or ArrayableInterface.
834 + *
835 + * @param \Illuminate\Support\Collection|\Illuminate\Support\Contracts\ArrayableInterface|array $items
836 + * @return array
837 + */
838 + protected function getArrayableItems($items)
839 + {
840 + if ($items instanceof Collection)
841 + {
842 + $items = $items->all();
843 + }
844 + elseif ($items instanceof ArrayableInterface)
845 + {
846 + $items = $items->toArray();
847 + }
848 +
849 + return $items;
850 + }
851 + }
852 +