|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Foo\Grid\Collection; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayAccess; |
|
8
|
|
|
use Countable; |
|
9
|
|
|
use Foo\Grid\Component\Component; |
|
10
|
|
|
use Foo\Grid\Component\IComponent; |
|
11
|
|
|
use Foo\Grid\Helper\StringHelper; |
|
12
|
|
|
use Foo\Translate\ITranslator; |
|
13
|
|
|
use InvalidArgumentException; |
|
14
|
|
|
use Iterator; |
|
15
|
|
|
use LogicException; |
|
16
|
|
|
|
|
17
|
|
|
class BaseCollection extends Component implements ArrayAccess, Countable, Iterator, IComponent |
|
18
|
|
|
{ |
|
19
|
|
|
const ERROR_INVALID_TYPE_ARG = 'Provided value must be an object instance of "%s", type "%s" is found'; |
|
20
|
|
|
const ERROR_INVALID_INSTANCE_ARG = 'Provided value must be an instance of "%s", not an instance of "%s"'; |
|
21
|
|
|
const ERROR_INVALID_TYPE_RETURN = 'Retrieved value is not an instance of "%s"'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var int */ |
|
24
|
|
|
protected $position = 0; |
|
25
|
|
|
|
|
26
|
|
|
/** @var IComponent[] */ |
|
27
|
|
|
protected $components = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var null|string */ |
|
30
|
|
|
protected $tag = null; |
|
31
|
|
|
|
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
protected $attributes = []; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
protected $indentation = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Collection constructor. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string|null $tag |
|
42
|
|
|
* @param array $attributes |
|
43
|
|
|
*/ |
|
44
|
92 |
|
public function __construct(string $tag = null, $attributes = []) |
|
45
|
|
|
{ |
|
46
|
92 |
|
$this->position = 0; |
|
47
|
|
|
|
|
48
|
92 |
|
parent::__construct('', $tag, $attributes); |
|
49
|
92 |
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
public function rewind() |
|
52
|
|
|
{ |
|
53
|
6 |
|
$this->position = 0; |
|
54
|
6 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return object |
|
58
|
|
|
*/ |
|
59
|
30 |
|
public function current() |
|
60
|
|
|
{ |
|
61
|
30 |
|
if (!array_key_exists($this->position, $this->components)) { |
|
62
|
6 |
|
return null; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
30 |
|
$component = $this->components[$this->position]; |
|
66
|
|
|
|
|
67
|
30 |
|
return $component; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
|
|
public function key() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->position; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
28 |
|
public function next() |
|
79
|
|
|
{ |
|
80
|
28 |
|
++$this->position; |
|
81
|
28 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function valid() |
|
87
|
|
|
{ |
|
88
|
|
|
return isset($this->components[$this->position]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int|null $offset |
|
93
|
|
|
* @param object $value |
|
94
|
|
|
*/ |
|
95
|
67 |
|
public function offsetSet($offset, $value) |
|
96
|
|
|
{ |
|
97
|
67 |
|
if (is_null($offset)) { |
|
98
|
67 |
|
$this->components[] = $value; |
|
99
|
|
|
} else { |
|
100
|
6 |
|
$this->components[$offset] = $value; |
|
101
|
|
|
} |
|
102
|
67 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param int $offset |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
|
|
public function offsetExists($offset) |
|
110
|
|
|
{ |
|
111
|
|
|
return isset($this->components[$offset]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param int $offset |
|
116
|
|
|
*/ |
|
117
|
|
|
public function offsetUnset($offset) |
|
118
|
|
|
{ |
|
119
|
|
|
unset($this->components[$offset]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param int $offset |
|
124
|
|
|
* |
|
125
|
|
|
* @return object|null |
|
126
|
|
|
*/ |
|
127
|
6 |
|
public function offsetGet($offset) |
|
128
|
|
|
{ |
|
129
|
6 |
|
return isset($this->components[$offset]) ? $this->components[$offset] : null; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return int |
|
134
|
|
|
*/ |
|
135
|
6 |
|
public function count() |
|
136
|
|
|
{ |
|
137
|
6 |
|
return count($this->components); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param int $num |
|
142
|
|
|
* @param string $whitespace |
|
143
|
|
|
*/ |
|
144
|
5 |
|
public function setIndentation(int $num, string $whitespace = ' ') |
|
145
|
|
|
{ |
|
146
|
5 |
|
foreach ($this->components as $component) { |
|
147
|
5 |
|
$component->setIndentation($num + 1, $whitespace); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
5 |
|
$this->indentation = str_repeat($whitespace, $num); |
|
151
|
5 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param ITranslator $translator |
|
155
|
|
|
*/ |
|
156
|
5 |
|
public function setTranslator(ITranslator $translator) |
|
157
|
|
|
{ |
|
158
|
5 |
|
foreach ($this->components as $component) { |
|
159
|
5 |
|
$component->setTranslator($translator); |
|
160
|
|
|
} |
|
161
|
5 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return string |
|
165
|
|
|
*/ |
|
166
|
15 |
|
public function __toString(): string |
|
167
|
|
|
{ |
|
168
|
15 |
|
$list = []; |
|
169
|
15 |
|
foreach ($this->components as $stringer) { |
|
170
|
15 |
|
$list[] = (string)$stringer; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
15 |
|
$content = implode("\n" . $this->indentation, $list); |
|
174
|
|
|
|
|
175
|
15 |
|
$result = StringHelper::wrapInTag($content, $this->tag, $this->attributes); |
|
176
|
|
|
|
|
177
|
15 |
|
return $result; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param object $object |
|
182
|
|
|
* @param string $className |
|
183
|
|
|
* |
|
184
|
|
|
* @throws InvalidArgumentException |
|
185
|
|
|
*/ |
|
186
|
63 |
|
protected function verifyArgument($object, $className) |
|
187
|
|
|
{ |
|
188
|
63 |
|
if ($object instanceof $className) { |
|
189
|
58 |
|
return; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
5 |
|
$type = gettype($object); |
|
193
|
5 |
|
if (gettype($object) !== 'object') { |
|
194
|
5 |
|
throw new InvalidArgumentException(sprintf(static::ERROR_INVALID_TYPE_ARG, $className, $type)); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
throw new InvalidArgumentException( |
|
199
|
|
|
sprintf(static::ERROR_INVALID_INSTANCE_ARG, $className, get_class($object)) |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @param object $object |
|
205
|
|
|
* @param string $className |
|
206
|
|
|
* |
|
207
|
|
|
* @throws LogicException |
|
208
|
|
|
*/ |
|
209
|
30 |
|
protected function verifyReturn($object, $className) |
|
210
|
|
|
{ |
|
211
|
30 |
|
if ($object === null || $object instanceof $className) { |
|
212
|
30 |
|
return; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
throw new LogicException(sprintf(static::ERROR_INVALID_TYPE_RETURN, $className)); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|