1
|
|
|
<?php namespace JobApis\Jobs\Client; |
2
|
|
|
|
3
|
|
|
use Countable; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class for storing a collection of items. Basically this adds functionality |
7
|
|
|
* and security to an array. |
8
|
|
|
*/ |
9
|
|
|
class Collection implements Countable |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Items |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $items = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Errors |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $errors = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Add item to collection, at specific key if given |
27
|
|
|
* |
28
|
|
|
* @param mixed $item |
29
|
|
|
* @param integer|string $key Optional |
30
|
|
|
* |
31
|
|
|
* @return Collection |
32
|
|
|
*/ |
33
|
28 |
|
public function add($item, $key = null) |
34
|
|
|
{ |
35
|
28 |
|
if ($key == null) { |
36
|
22 |
|
$this->items[] = $item; |
37
|
22 |
|
} else { |
38
|
6 |
|
if (isset($this->items[$key])) { |
39
|
2 |
|
return $this->addError("Invalid key $key."); |
40
|
|
|
} else { |
41
|
6 |
|
$this->items[$key] = $item; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
28 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Append a collection to this collection. |
50
|
|
|
* |
51
|
|
|
* @param Collection $collection |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
4 |
|
public function addCollection(Collection $collection) |
56
|
|
|
{ |
57
|
|
|
// If there are jobs, add them to the collection |
58
|
4 |
|
if ($collection->count()) { |
59
|
2 |
|
foreach ($collection->all() as $job) { |
60
|
2 |
|
$this->add($job); |
61
|
2 |
|
} |
62
|
2 |
|
} |
63
|
|
|
// If there are errors, add them to the collection |
64
|
4 |
|
if ($collection->getErrors()) { |
65
|
4 |
|
foreach ($collection->getErrors() as $error) { |
66
|
4 |
|
$this->addError($error); |
67
|
4 |
|
} |
68
|
4 |
|
} |
69
|
|
|
|
70
|
4 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add error to collection |
75
|
|
|
* |
76
|
|
|
* @param string $message |
77
|
|
|
* |
78
|
|
|
* @return Collection |
79
|
|
|
*/ |
80
|
14 |
|
public function addError($message) |
81
|
|
|
{ |
82
|
14 |
|
if (isset($message)) { |
83
|
12 |
|
$this->errors[] = $message; |
84
|
12 |
|
} else { |
85
|
2 |
|
$this->errors[] = "Invalid error mesage."; |
86
|
|
|
} |
87
|
|
|
|
88
|
14 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get all items from collection |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
8 |
|
public function all() |
97
|
|
|
{ |
98
|
8 |
|
return $this->items; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get count of items in collection |
103
|
|
|
* |
104
|
|
|
* @return integer |
105
|
|
|
*/ |
106
|
16 |
|
public function count() |
107
|
|
|
{ |
108
|
16 |
|
return count($this->items); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Delete item from collection at specific key |
113
|
|
|
* |
114
|
|
|
* @param integer|string $key |
115
|
|
|
* |
116
|
|
|
* @return Collection |
117
|
|
|
*/ |
118
|
4 |
|
public function delete($key) |
119
|
|
|
{ |
120
|
4 |
|
if (isset($this->items[$key])) { |
121
|
2 |
|
unset($this->items[$key]); |
122
|
2 |
|
} else { |
123
|
2 |
|
return $this->addError("Invalid key $key."); |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Filter items by a field having a specific value. |
131
|
|
|
* |
132
|
|
|
* @param string $field |
133
|
|
|
* @param string $value |
134
|
|
|
* @param string $operator |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
4 |
|
public function filter($field, $value, $operator = '=') |
139
|
|
|
{ |
140
|
4 |
|
$this->items = array_filter( |
141
|
4 |
|
$this->items, |
142
|
|
|
function ($item) use ($field, $value, $operator) { |
143
|
4 |
|
if (!isset($item->{$field})) { |
144
|
2 |
|
throw new \Exception("Property not defined."); |
145
|
|
|
} |
146
|
2 |
|
if ($operator == '>') { |
147
|
|
|
return $item->{$field} > $value; |
148
|
2 |
|
} elseif ($operator == '<') { |
149
|
|
|
return $item->{$field} < $value; |
150
|
2 |
|
} elseif ($operator == '=') { |
151
|
2 |
|
return $item->{$field} == $value; |
152
|
|
|
} |
153
|
|
|
return false; |
154
|
|
|
} |
155
|
4 |
|
); |
156
|
|
|
|
157
|
2 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get item from collection at specific key |
162
|
|
|
* |
163
|
|
|
* @param integer|string $key |
164
|
|
|
* |
165
|
|
|
* @return mixed |
166
|
|
|
*/ |
167
|
8 |
|
public function get($key) |
168
|
|
|
{ |
169
|
8 |
|
if (isset($this->items[$key])) { |
170
|
4 |
|
return $this->items[$key]; |
171
|
|
|
} else { |
172
|
4 |
|
$this->addError("Invalid key $key."); |
173
|
|
|
} |
174
|
|
|
|
175
|
4 |
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get all errors from Collection |
180
|
|
|
* |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
14 |
|
public function getErrors() |
184
|
|
|
{ |
185
|
14 |
|
return $this->errors; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Order items by a field value. |
190
|
|
|
* |
191
|
|
|
* @param string $orderBy |
192
|
|
|
* @param string $order |
193
|
|
|
* |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
6 |
|
public function orderBy($orderBy, $order = 'desc') |
197
|
|
|
{ |
198
|
6 |
|
usort( |
199
|
6 |
|
$this->items, |
200
|
6 |
|
function ($item1, $item2) use ($orderBy, $order) { |
201
|
6 |
|
if (!isset($item1->{$orderBy}) || !isset($item2->{$orderBy})) { |
202
|
2 |
|
throw new \Exception("Property not defined."); |
203
|
|
|
} |
204
|
|
|
// If the two items are equal, return 0 |
205
|
4 |
|
if ($item1->{$orderBy} == $item2->{$orderBy}) { |
206
|
|
|
return 0; |
207
|
|
|
} |
208
|
|
|
// If ascending, test whether Item 1 is less than Item 2 |
209
|
4 |
|
if ($order === 'asc') { |
210
|
2 |
|
return $item1->{$orderBy} < $item2->{$orderBy} ? -1 : 1; |
211
|
|
|
} |
212
|
|
|
// Else assuming descending. |
213
|
2 |
|
return $item1->{$orderBy} > $item2->{$orderBy} ? -1 : 1; |
214
|
|
|
} |
215
|
6 |
|
); |
216
|
|
|
|
217
|
4 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Truncate the items to a maximum number of results. |
222
|
|
|
* |
223
|
|
|
* @param null $max |
224
|
|
|
* |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
2 |
|
public function truncate($max = null) |
228
|
|
|
{ |
229
|
2 |
|
if ($max) { |
230
|
2 |
|
$this->items = array_slice($this->items, 0, $max); |
231
|
2 |
|
} |
232
|
|
|
|
233
|
2 |
|
return $this; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|