|
1
|
|
|
<?php namespace JobApis\Jobs\Client; |
|
2
|
|
|
|
|
3
|
|
|
class MultiCollection extends Collection |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* Append a collection to this collection. |
|
7
|
|
|
* |
|
8
|
|
|
* @param Collection $collection |
|
9
|
|
|
* |
|
10
|
|
|
* @return $this |
|
11
|
|
|
*/ |
|
12
|
4 |
|
public function append(Collection $collection) |
|
13
|
|
|
{ |
|
14
|
|
|
// If there are jobs, add them to the collection |
|
15
|
4 |
|
if ($collection->count()) { |
|
16
|
2 |
|
foreach ($collection->all() as $job) { |
|
17
|
2 |
|
$this->add($job); |
|
18
|
2 |
|
} |
|
19
|
2 |
|
} |
|
20
|
|
|
// If there are errors, add them to the collection |
|
21
|
4 |
|
if ($collection->getErrors()) { |
|
22
|
4 |
|
foreach ($collection->getErrors() as $error) { |
|
23
|
4 |
|
$this->addError($error); |
|
24
|
4 |
|
} |
|
25
|
4 |
|
} |
|
26
|
|
|
|
|
27
|
4 |
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Filter items by a field having a specific value. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $field |
|
34
|
|
|
* @param string $value |
|
35
|
|
|
* @param string $operator |
|
36
|
|
|
* |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public function filter($field, $value, $operator = '=') |
|
40
|
|
|
{ |
|
41
|
4 |
|
$this->items = array_filter( |
|
42
|
4 |
|
$this->items, |
|
43
|
|
|
function ($item) use ($field, $value, $operator) { |
|
44
|
4 |
|
if (!isset($item->{$field})) { |
|
45
|
2 |
|
throw new \Exception("Property not defined."); |
|
46
|
|
|
} |
|
47
|
2 |
|
if ($operator == '>') { |
|
48
|
|
|
return $item->{$field} > $value; |
|
49
|
2 |
|
} elseif ($operator == '<') { |
|
50
|
|
|
return $item->{$field} < $value; |
|
51
|
2 |
|
} elseif ($operator == '=') { |
|
52
|
2 |
|
return $item->{$field} == $value; |
|
53
|
|
|
} |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
4 |
|
); |
|
57
|
|
|
|
|
58
|
2 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Order items by a field value. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $orderBy |
|
65
|
|
|
* @param string $order |
|
66
|
|
|
* |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
6 |
|
public function orderBy($orderBy, $order = 'desc') |
|
70
|
|
|
{ |
|
71
|
6 |
|
usort( |
|
72
|
6 |
|
$this->items, |
|
73
|
6 |
|
function ($item1, $item2) use ($orderBy, $order) { |
|
74
|
6 |
|
if (!isset($item1->{$orderBy}) || !isset($item2->{$orderBy})) { |
|
75
|
2 |
|
throw new \Exception("Property not defined."); |
|
76
|
|
|
} |
|
77
|
|
|
// If the two items are equal, return 0 |
|
78
|
4 |
|
if ($item1->{$orderBy} == $item2->{$orderBy}) { |
|
79
|
|
|
return 0; |
|
80
|
|
|
} |
|
81
|
|
|
// If ascending, test whether Item 1 is less than Item 2 |
|
82
|
4 |
|
if ($order === 'asc') { |
|
83
|
2 |
|
return $item1->{$orderBy} < $item2->{$orderBy} ? -1 : 1; |
|
84
|
|
|
} |
|
85
|
|
|
// Else assuming descending. |
|
86
|
2 |
|
return $item1->{$orderBy} > $item2->{$orderBy} ? -1 : 1; |
|
87
|
|
|
} |
|
88
|
6 |
|
); |
|
89
|
|
|
|
|
90
|
4 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Truncate the items to a maximum number of results. |
|
95
|
|
|
* |
|
96
|
|
|
* @param null $max |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function truncate($max = null) |
|
101
|
|
|
{ |
|
102
|
2 |
|
if ($max) { |
|
103
|
2 |
|
$this->items = array_slice($this->items, 0, $max); |
|
104
|
2 |
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|