1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SyncFS; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Bag |
7
|
|
|
* |
8
|
|
|
* @package SyncFS |
9
|
|
|
* @author Matej Velikonja <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Bag implements \IteratorAggregate, \Countable, \ArrayAccess |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $elements; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $elements |
20
|
|
|
*/ |
21
|
33 |
|
public function __construct(array $elements = array()) |
22
|
|
|
{ |
23
|
33 |
|
$this->setData($elements); |
24
|
33 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $elements |
28
|
|
|
*/ |
29
|
33 |
|
public function setData(array $elements) |
30
|
|
|
{ |
31
|
33 |
|
$this->elements = $elements; |
32
|
33 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param mixed $element |
36
|
|
|
* |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
7 |
|
public function add($element) |
40
|
|
|
{ |
41
|
7 |
|
$this->elements[] = $element; |
42
|
|
|
|
43
|
7 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns last element.. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
3 |
|
public function last() |
52
|
|
|
{ |
53
|
3 |
|
return end($this->elements); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns second last element. |
58
|
|
|
* |
59
|
|
|
* @return string | false |
60
|
|
|
*/ |
61
|
3 |
|
public function secondLast() |
62
|
|
|
{ |
63
|
3 |
|
end($this->elements); |
64
|
|
|
|
65
|
3 |
|
return prev($this->elements); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns first element. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
3 |
|
public function first() |
74
|
|
|
{ |
75
|
3 |
|
return reset($this->elements); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns number of all elements. |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
7 |
|
public function count() |
84
|
|
|
{ |
85
|
7 |
|
return count($this->elements); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return \ArrayIterator|\Traversable |
90
|
|
|
*/ |
91
|
2 |
|
public function getIterator() |
92
|
|
|
{ |
93
|
2 |
|
return new \ArrayIterator($this->elements); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $offset |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
2 |
|
public function offsetExists($offset) |
102
|
|
|
{ |
103
|
2 |
|
return isset($this->elements[$offset]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param int $offset |
108
|
|
|
* |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
2 |
|
public function offsetGet($offset) |
112
|
|
|
{ |
113
|
2 |
|
return $this->elements[$offset]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param int $offset |
118
|
|
|
* @param mixed $value |
119
|
|
|
*/ |
120
|
1 |
|
public function offsetSet($offset, $value) |
121
|
|
|
{ |
122
|
1 |
|
$this->elements[$offset] = $value; |
123
|
1 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param int $offset |
127
|
|
|
*/ |
128
|
1 |
|
public function offsetUnset($offset) |
129
|
|
|
{ |
130
|
1 |
|
unset($this->elements[$offset]); |
131
|
1 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Removes all elements. |
135
|
|
|
* |
136
|
|
|
* @return $this |
137
|
|
|
*/ |
138
|
1 |
|
public function clear() |
139
|
|
|
{ |
140
|
1 |
|
$this->elements = array(); |
141
|
|
|
|
142
|
1 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
3 |
|
public function isEmpty() |
149
|
|
|
{ |
150
|
3 |
|
return ! $this->elements; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
4 |
|
public function all() |
157
|
|
|
{ |
158
|
4 |
|
return $this->elements; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param mixed $needle |
163
|
|
|
* |
164
|
|
|
* @return bool |
165
|
|
|
*/ |
166
|
1 |
|
public function exists($needle) |
167
|
|
|
{ |
168
|
1 |
|
foreach ($this->elements as $e) { |
169
|
1 |
|
if ($e === $needle) { |
170
|
1 |
|
return true; |
171
|
|
|
} |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
2 |
|
public function __toString() |
181
|
|
|
{ |
182
|
2 |
|
return implode(PHP_EOL, $this->elements); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|