-
Notifications
You must be signed in to change notification settings - Fork 17
/
Collection.php
235 lines (213 loc) · 5.24 KB
/
Collection.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php namespace JobApis\Jobs\Client;
use Countable;
/**
* Class for storing a collection of items. Basically this adds functionality
* and security to an array.
*/
class Collection implements Countable
{
/**
* Items
*
* @var array
*/
protected $items = [];
/**
* Errors
*
* @var array
*/
protected $errors = [];
/**
* Add item to collection, at specific key if given
*
* @param mixed $item
* @param integer|string $key Optional
*
* @return Collection
*/
public function add($item, $key = null)
{
if ($key == null) {
$this->items[] = $item;
} else {
if (isset($this->items[$key])) {
return $this->addError("Invalid key $key.");
} else {
$this->items[$key] = $item;
}
}
return $this;
}
/**
* Append a collection to this collection.
*
* @param Collection $collection
*
* @return $this
*/
public function addCollection(Collection $collection)
{
// If there are jobs, add them to the collection
if ($collection->count()) {
foreach ($collection->all() as $job) {
$this->add($job);
}
}
// If there are errors, add them to the collection
if ($collection->getErrors()) {
foreach ($collection->getErrors() as $error) {
$this->addError($error);
}
}
return $this;
}
/**
* Add error to collection
*
* @param string $message
*
* @return Collection
*/
public function addError($message)
{
if (isset($message)) {
$this->errors[] = $message;
} else {
$this->errors[] = "Invalid error mesage.";
}
return $this;
}
/**
* Get all items from collection
*
* @return array
*/
public function all()
{
return $this->items;
}
/**
* Get count of items in collection
*
* @return integer
*/
public function count()
{
return count($this->items);
}
/**
* Delete item from collection at specific key
*
* @param integer|string $key
*
* @return Collection
*/
public function delete($key)
{
if (isset($this->items[$key])) {
unset($this->items[$key]);
} else {
return $this->addError("Invalid key $key.");
}
return $this;
}
/**
* Filter items by a field having a specific value.
*
* @param string $field
* @param string $value
* @param string $operator
*
* @return $this
*/
public function filter($field, $value, $operator = '=')
{
$this->items = array_filter(
$this->items,
function ($item) use ($field, $value, $operator) {
if (!isset($item->{$field})) {
throw new \Exception("Property not defined.");
}
if ($operator == '>') {
return $item->{$field} > $value;
} elseif ($operator == '<') {
return $item->{$field} < $value;
} elseif ($operator == '=') {
return $item->{$field} == $value;
}
return false;
}
);
return $this;
}
/**
* Get item from collection at specific key
*
* @param integer|string $key
*
* @return mixed
*/
public function get($key)
{
if (isset($this->items[$key])) {
return $this->items[$key];
} else {
$this->addError("Invalid key $key.");
}
return null;
}
/**
* Get all errors from Collection
*
* @return array
*/
public function getErrors()
{
return $this->errors;
}
/**
* Order items by a field value.
*
* @param string $orderBy
* @param string $order
*
* @return $this
*/
public function orderBy($orderBy, $order = 'desc')
{
usort(
$this->items,
function ($item1, $item2) use ($orderBy, $order) {
if (!isset($item1->{$orderBy}) || !isset($item2->{$orderBy})) {
throw new \Exception("Property not defined.");
}
// If the two items are equal, return 0
if ($item1->{$orderBy} == $item2->{$orderBy}) {
return 0;
}
// If ascending, test whether Item 1 is less than Item 2
if ($order === 'asc') {
return $item1->{$orderBy} < $item2->{$orderBy} ? -1 : 1;
}
// Else assuming descending.
return $item1->{$orderBy} > $item2->{$orderBy} ? -1 : 1;
}
);
return $this;
}
/**
* Truncate the items to a maximum number of results.
*
* @param null $max
*
* @return $this
*/
public function truncate($max = null)
{
if ($max) {
$this->items = array_slice($this->items, 0, $max);
}
return $this;
}
}