-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathCollection.php
More file actions
143 lines (126 loc) · 2.65 KB
/
Collection.php
File metadata and controls
143 lines (126 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace MailerLiteApi\Common;
use ArrayAccess;
use ArrayIterator;
use Countable;
use IteratorAggregate;
class Collection implements ArrayAccess, IteratorAggregate, Countable
{
/**
* The items contained in the collection.
*
* @var array
*/
protected $items = [];
/**
* Create a new collection.
*
* @param array $items
*/
public function __construct(array $items = [])
{
$this->items = $items;
}
/**
* Get an iterator for the items.
*
*/
public function getIterator() : ArrayIterator
{
return new ArrayIterator($this->items);
}
/**
* Create a new collection instance if the value isn't one already.
*
* @param mixed $items
*
* @return static
*/
public static function make($items)
{
if (is_null($items)) {
return new static;
}
if ($items instanceof Collection) {
return $items;
}
return new static(is_array($items) ? $items : [$items]);
}
/**
* Get the first item from the collection.
*
* @return mixed|null
*/
public function first()
{
return count($this->items) > 0 ? reset($this->items) : null;
}
/**
* Get the collection of items as a plain array.
*
* @return array
*/
public function toArray()
{
return array_map(
function ($value) {
return $value instanceof Entity ? $value->toArray() : $value;
}, $this->items
);
}
/**
* Count the number of items in the collection.
*
*/
public function count() : int
{
return count($this->items);
}
/**
* Determine if an item exists at an offset.
*
* @param mixed $key
*
*/
public function offsetExists($key) : bool
{
return array_key_exists($key, $this->items);
}
/**
* Get an item at a given offset.
*
* @param mixed $key
*
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($key)
{
return $this->items[$key];
}
/**
* Set the item at a given offset.
*
* @param mixed $key
* @param mixed $value
*
*/
public function offsetSet($key, $value) : void
{
if (is_null($key)) {
$this->items[] = $value;
} else {
$this->items[$key] = $value;
}
}
/**
* Unset the item at a given offset.
*
* @param string $key
*
*/
public function offsetUnset($key) : void
{
unset($this->items[$key]);
}
}