-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicCollection.php
More file actions
62 lines (49 loc) · 1.47 KB
/
Copy pathBasicCollection.php
File metadata and controls
62 lines (49 loc) · 1.47 KB
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
<?php
declare(strict_types=1);
namespace Echron\DataTypes;
abstract class BasicCollection implements \IteratorAggregate, \Countable, \JsonSerializable
{
private array $collection = [];
private int $index = 0;
public function __construct()
{
}
protected final function addToCollection(mixed $data): int
{
$index = $this->index;
$this->collection[$index] = $data;
$this->index++;
return $index;
}
protected final function removeFromCollection(int $index): void
{
unset($this->collection[$index]);
}
protected final function getByIndex(int $index): mixed
{
return $this->collection[$index];
}
public final function count(): int
{
return count($this->collection);
}
function jsonSerialize(): array
{
//TODO: good idea to remove keys?
$data = [];
/** @var IdCodeObject $item */
foreach ($this->collection as $item) {
$data[] = $item->jsonSerialize();
}
return $data;
}
public function getIterator(): \ArrayIterator
{
//TODO: is it possible to not return a new iterator on every call? this is not working for nested iterations!
// if (\is_null($this->iterator)) {
// $this->iterator = new \ArrayIterator($this->collection);
// }
// $this->iterator->rewind();
return new \ArrayIterator($this->collection);
}
}