1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StephaneCoinon\SendGridActivity\Support; |
4
|
|
|
|
5
|
|
|
use StephaneCoinon\SendGridActivity\Integrations\Framework; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Cast an array into the framework native collection (or array for vanilla PHP). |
9
|
|
|
*/ |
10
|
|
|
class Collection |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Collection items. |
14
|
|
|
* |
15
|
|
|
* @var array|\Illuminate\Support\Collection |
16
|
|
|
*/ |
17
|
|
|
protected $items = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get a new Collection instance. |
21
|
|
|
* |
22
|
|
|
* @param array $items |
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $items) |
25
|
|
|
{ |
26
|
|
|
$this->items = static::collect($items); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Cast an array into the framework native collection. |
31
|
|
|
* |
32
|
|
|
* @param array $items |
33
|
|
|
* @return array|\Illuminate\Support\Collection |
34
|
|
|
*/ |
35
|
|
|
public static function collect(array $items) |
36
|
|
|
{ |
37
|
|
|
return Framework::isLaravel() ? collect($items) : $items; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a new Collection instance. |
42
|
|
|
* |
43
|
|
|
* @param array $items |
44
|
|
|
* @return self |
45
|
|
|
*/ |
46
|
|
|
public static function make(array $items): self |
47
|
|
|
{ |
48
|
|
|
return new static($items); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Run a map over each of the items. |
53
|
|
|
* |
54
|
|
|
* @param callable $callback |
55
|
|
|
* @return array|\Illuminate\Support\Collection |
56
|
|
|
*/ |
57
|
|
|
public function map(callable $callback) |
58
|
|
|
{ |
59
|
|
|
if (Framework::isLaravel()) { |
60
|
|
|
return $this->items->map($callback); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return array_map($callback, $this->items); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Undocumented function |
68
|
|
|
* |
69
|
|
|
* @return array|\Illuminate\Support\Collection |
70
|
|
|
*/ |
71
|
|
|
public function mapInto(string $model) |
72
|
|
|
{ |
73
|
|
|
if (Framework::isLaravel()) { |
74
|
|
|
return $this->items->mapInto($model); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return array_map(function ($item) use ($model) { |
78
|
|
|
return new $model($item); |
79
|
|
|
}, $this->items); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|