1
|
|
|
<?php |
2
|
|
|
namespace Intraxia\Jaxion\Axolotl; |
3
|
|
|
|
4
|
|
|
use Countable; |
5
|
|
|
use Intraxia\Jaxion\Contract\Axolotl\Serializes; |
6
|
|
|
use Iterator; |
7
|
|
|
use LogicException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Collection |
12
|
|
|
* |
13
|
|
|
* @package Intraxia\Jaxion |
14
|
|
|
* @subpackage Axolotl |
15
|
|
|
*/ |
16
|
|
|
class Collection implements Countable, Iterator, Serializes { |
17
|
|
|
/** |
18
|
|
|
* Collection elements. |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $elements = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Collection configuration. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $config = array(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Models registered to the collection. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $model; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Where Collection is in loop. |
40
|
|
|
* |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $position = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Collection constructor. |
47
|
|
|
* |
48
|
|
|
* @param array $elements |
49
|
|
|
* @param array $config |
50
|
|
|
*/ |
51
|
60 |
|
public function __construct( array $elements = array(), array $config = array() ) { |
52
|
60 |
|
$this->parse_config( $this->config = $config ); |
53
|
|
|
|
54
|
57 |
|
foreach ( $elements as $element ) { |
55
|
24 |
|
$this->add( $element ); |
56
|
57 |
|
} |
57
|
57 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Adds a new element to the Collection. |
61
|
|
|
* |
62
|
|
|
* @param mixed $element |
63
|
|
|
* |
64
|
|
|
* @throws RuntimeException |
65
|
|
|
*/ |
66
|
27 |
|
public function add( $element ) { |
67
|
27 |
|
if ( $this->model && is_array( $element ) ) { |
68
|
9 |
|
$element = new $this->model( $element ); |
69
|
9 |
|
} |
70
|
|
|
|
71
|
27 |
|
if ( $this->model && ! ( $element instanceof $this->model ) ) { |
72
|
3 |
|
throw new RuntimeException; |
73
|
|
|
} |
74
|
|
|
|
75
|
24 |
|
$this->elements[] = $element; |
76
|
24 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Removes an element by index or value. |
80
|
|
|
* |
81
|
|
|
* @param mixed $index |
82
|
|
|
* |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function remove( $index ) { |
86
|
|
|
if ( ! is_string( $index ) || ! is_numeric( $index ) || ! isset( $this->elements[ $index ]) ) { |
|
|
|
|
87
|
|
|
foreach ( $this->elements as $key => $element ) { |
88
|
|
|
if ( $element === $index ) { |
89
|
|
|
$index = $key; |
90
|
|
|
break; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ( isset( $this->elements[ $index ] ) ) { |
96
|
|
|
unset( $this->elements[ $index ] ); |
97
|
|
|
$this->elements = array_values( $this->elements ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Fetches the element at the provided index. |
105
|
|
|
* |
106
|
|
|
* @param int $index |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
12 |
|
public function at( $index ) { |
111
|
12 |
|
return isset( $this->elements[ $index ] ) ? $this->elements[ $index ] : null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Maps over the Collection's elements, |
116
|
|
|
* |
117
|
|
|
* @param callable $callback |
118
|
|
|
* |
119
|
|
|
* @return Collection |
120
|
|
|
*/ |
121
|
|
|
protected function map( callable $callback ) { |
122
|
|
|
return new Collection( array_map( $callback, $this->elements ), $this->config ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Filters the Collection's elements. |
127
|
|
|
* |
128
|
|
|
* @param callable $callback |
129
|
|
|
* |
130
|
|
|
* @return Collection |
131
|
|
|
*/ |
132
|
|
|
public function filter( callable $callback ) { |
133
|
|
|
return new Collection( array_filter( $this->elements, $callback ) ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function serialize() { |
142
|
9 |
|
return array_map(function( $element ) { |
143
|
6 |
|
if ( $element instanceof Serializes ) { |
144
|
3 |
|
return $element->serialize(); |
145
|
|
|
} |
146
|
|
|
|
147
|
3 |
|
return $element; |
148
|
9 |
|
}, $this->elements); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the current element. |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
3 |
|
public function current() { |
157
|
3 |
|
return $this->at( $this->position ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Move forward to next element. |
162
|
|
|
*/ |
163
|
3 |
|
public function next() { |
164
|
3 |
|
$this->position ++; |
165
|
3 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return the key of the current element. |
169
|
|
|
* |
170
|
|
|
* @return mixed |
171
|
|
|
*/ |
172
|
3 |
|
public function key() { |
173
|
3 |
|
return $this->position; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Checks if current position is valid. |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
3 |
|
public function valid() { |
182
|
3 |
|
return isset( $this->elements[ $this->position ] ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Rewind the Iterator to the first element. |
187
|
|
|
*/ |
188
|
3 |
|
public function rewind() { |
189
|
3 |
|
$this->position = 0; |
190
|
3 |
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Count elements of an object. |
194
|
|
|
* |
195
|
|
|
* @return int |
196
|
|
|
*/ |
197
|
6 |
|
public function count() { |
198
|
6 |
|
return count( $this->elements ); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Parses the Collection config to set its properties. |
203
|
|
|
* |
204
|
|
|
* @param array $config |
205
|
|
|
* |
206
|
|
|
* @throws LogicException |
207
|
|
|
*/ |
208
|
60 |
|
protected function parse_config( array $config ) { |
209
|
60 |
|
if ( isset( $config['model'] ) ) { |
210
|
15 |
|
$model = $config['model']; |
211
|
|
|
|
212
|
15 |
|
if ( ! is_subclass_of( $model, 'Intraxia\Jaxion\Axolotl\Model' ) ) { |
213
|
3 |
|
throw new LogicException; |
214
|
|
|
} |
215
|
|
|
|
216
|
12 |
|
$this->model = $model; |
217
|
12 |
|
} |
218
|
57 |
|
} |
219
|
|
|
} |
220
|
|
|
|