|
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
|
10 |
|
public function add($item, $key = null) |
|
34
|
|
|
{ |
|
35
|
10 |
|
if ($key == null) { |
|
36
|
4 |
|
$this->items[] = $item; |
|
37
|
4 |
|
} 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
|
10 |
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Add error to collection |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $message |
|
52
|
|
|
* |
|
53
|
|
|
* @return Collection |
|
54
|
|
|
*/ |
|
55
|
10 |
|
public function addError($message) |
|
56
|
|
|
{ |
|
57
|
10 |
|
if (isset($message)) { |
|
58
|
8 |
|
$this->errors[] = $message; |
|
59
|
8 |
|
} else { |
|
60
|
2 |
|
$this->errors[] = "Invalid error mesage."; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
10 |
|
return $this; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get all items from collection |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function all() |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $this->items; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get count of items in collection |
|
78
|
|
|
* |
|
79
|
|
|
* @return integer |
|
80
|
|
|
*/ |
|
81
|
2 |
|
public function count() |
|
82
|
|
|
{ |
|
83
|
2 |
|
return count($this->items); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Delete item from collection at specific key |
|
88
|
|
|
* |
|
89
|
|
|
* @param integer|string $key |
|
90
|
|
|
* |
|
91
|
|
|
* @return Collection |
|
92
|
|
|
*/ |
|
93
|
4 |
|
public function delete($key) |
|
94
|
|
|
{ |
|
95
|
4 |
|
if (isset($this->items[$key])) { |
|
96
|
2 |
|
unset($this->items[$key]); |
|
97
|
2 |
|
} else { |
|
98
|
2 |
|
return $this->addError("Invalid key $key."); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
2 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get item from collection at specific key |
|
106
|
|
|
* |
|
107
|
|
|
* @param integer|string $key |
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
6 |
|
public function get($key) |
|
112
|
|
|
{ |
|
113
|
6 |
|
if (isset($this->items[$key])) { |
|
114
|
2 |
|
return $this->items[$key]; |
|
115
|
|
|
} else { |
|
116
|
4 |
|
$this->addError("Invalid key $key."); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
4 |
|
return null; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get all errors from Collection |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
10 |
|
public function getErrors() |
|
128
|
|
|
{ |
|
129
|
10 |
|
return $this->errors; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|