-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAbstractStructArrayBase.php
308 lines (258 loc) · 7.22 KB
/
AbstractStructArrayBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
declare(strict_types=1);
namespace WsdlToPhp\PackageBase;
abstract class AbstractStructArrayBase extends AbstractStructBase implements StructArrayInterface
{
/**
* Array that contains values when only one parameter is set when calling __construct method
* @var array
*/
private array $internArray = [];
/**
* Bool that tells if array is set or not
* @var bool
*/
private bool $internArrayIsArray = false;
/**
* Items index browser
* @var int
*/
private int $internArrayOffset = 0;
/**
* Method alias to count
* @return int
*/
public function length(): int
{
$this->initInternArray();
return $this->count();
}
/**
* Method returning item length, alias to length
* @return int
*/
public function count(): int
{
$this->initInternArray();
return $this->getInternArrayIsArray() ? count($this->getInternArray()) : -1;
}
/**
* Method returning the current element
* @return mixed
*/
#[\ReturnTypeWillChange]
public function current()
{
$this->initInternArray();
return $this->offsetGet($this->internArrayOffset);
}
/**
* Method moving the current position to the next element
* @return AbstractStructArrayBase
*/
#[\ReturnTypeWillChange]
public function next(): self
{
$this->initInternArray();
return $this->setInternArrayOffset($this->getInternArrayOffset() + 1);
}
/**
* Method resetting itemOffset
* @return AbstractStructArrayBase
*/
#[\ReturnTypeWillChange]
public function rewind(): self
{
$this->initInternArray();
return $this->setInternArrayOffset(0);
}
/**
* Method checking if current itemOffset points to an existing item
* @return bool
*/
public function valid(): bool
{
$this->initInternArray();
return $this->offsetExists($this->getInternArrayOffset());
}
/**
* Method returning current itemOffset value, alias to getInternArrayOffset
* @return int
*/
public function key(): int
{
$this->initInternArray();
return $this->getInternArrayOffset();
}
/**
* Method alias to offsetGet
* @param mixed $index
* @return mixed
*/
public function item($index)
{
$this->initInternArray();
return $this->offsetGet($index);
}
/**
* Default method adding item to array
* @param mixed $item value
* @return AbstractStructArrayBase
*/
public function add($item): self
{
// init array
if (!is_array($this->getPropertyValue($this->getAttributeName()))) {
$this->setPropertyValue($this->getAttributeName(), []);
}
// current array
$currentArray = $this->getPropertyValue($this->getAttributeName());
$currentArray[] = $item;
$this
->setInternArray($currentArray)
->setInternArrayIsArray(true)
->setInternArrayOffset(0)
->setPropertyValue($this->getAttributeName(), $currentArray);
return $this;
}
/**
* Method returning the first item
* @return mixed
*/
public function first()
{
$this->initInternArray();
return $this->item(0);
}
/**
* Method returning the last item
* @return mixed
*/
public function last()
{
$this->initInternArray();
return $this->item($this->length() - 1);
}
/**
* Method testing index in item
* @param mixed $offset
* @return bool
*/
public function offsetExists($offset): bool
{
$this->initInternArray();
return ($this->getInternArrayIsArray() && array_key_exists($offset, $this->getInternArray()));
}
/**
* Method returning the item at "index" value
* @param mixed $offset
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
$this->initInternArray();
return $this->offsetExists($offset) ? $this->internArray[$offset] : null;
}
/**
* Method setting value at offset
* @param mixed $offset
* @param mixed $value
* @return AbstractStructArrayBase
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value): self
{
$this->initInternArray();
$this->internArray[$offset] = $value;
$this->setPropertyValue($this->getAttributeName(), $this->internArray);
return $this;
}
/**
* Method unsetting value at offset
* @param mixed $offset
* @return AbstractStructArrayBase
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset): self
{
$this->initInternArray();
if ($this->offsetExists($offset)) {
unset($this->internArray[$offset]);
$this->setPropertyValue($this->getAttributeName(), $this->internArray);
}
return $this;
}
/**
* Method returning intern array to iterate trough
* @return array
*/
private function getInternArray(): array
{
return $this->internArray;
}
/**
* Method setting intern array to iterate trough
* @param array $internArray
* @return AbstractStructArrayBase
*/
private function setInternArray(array $internArray): self
{
$this->internArray = $internArray;
return $this;
}
/**
* Method returns intern array index when iterating trough
* @return int
*/
private function getInternArrayOffset(): int
{
return $this->internArrayOffset;
}
/**
* Method initiating internArray
* @param array $array the array to iterate trough
* @param bool $internCall indicates that methods is calling itself
* @return AbstractStructArrayBase
*/
private function initInternArray($array = [], bool $internCall = false): self
{
if (is_array($array) && count($array) > 0) {
$this
->setInternArray($array)
->setInternArrayOffset(0)
->setInternArrayIsArray(true);
} elseif (!$this->internArrayIsArray && !$internCall && property_exists($this, $this->getAttributeName())) {
$this->initInternArray($this->getPropertyValue($this->getAttributeName()), true);
}
return $this;
}
/**
* Method setting intern array offset when iterating trough
* @param int $internArrayOffset
* @return AbstractStructArrayBase
*/
private function setInternArrayOffset(int $internArrayOffset): self
{
$this->internArrayOffset = $internArrayOffset;
return $this;
}
/**
* Method returning true if intern array is an actual array
* @return bool
*/
private function getInternArrayIsArray(): bool
{
return $this->internArrayIsArray;
}
/**
* Method setting if intern array is an actual array
* @param bool $internArrayIsArray
* @return AbstractStructArrayBase
*/
private function setInternArrayIsArray(bool $internArrayIsArray = false): self
{
$this->internArrayIsArray = $internArrayIsArray;
return $this;
}
}