1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpValueObjects\Collection; |
6
|
|
|
|
7
|
|
|
use PhpValueObjects\Collection\Exception\InvalidCollectionObjectException; |
8
|
|
|
|
9
|
|
|
abstract class ObjectCollection |
10
|
|
|
{ |
11
|
|
|
protected $objects; |
12
|
|
|
|
13
|
54 |
|
public function __construct(array $objects) |
14
|
|
|
{ |
15
|
54 |
|
foreach ($objects as $object) { |
16
|
54 |
|
if (false === is_a($object, $this->setClassName())) { |
17
|
54 |
|
throw new InvalidCollectionObjectException($object, $this->setClassName()); |
18
|
|
|
} |
19
|
|
|
} |
20
|
54 |
|
$this->objects = $objects; |
21
|
54 |
|
} |
22
|
|
|
|
23
|
|
|
public static function init(): self |
24
|
|
|
{ |
25
|
|
|
return new static([]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
abstract protected function setClassName(): string; |
29
|
|
|
|
30
|
12 |
|
public function getCollection(): array |
31
|
|
|
{ |
32
|
12 |
|
return $this->objects; |
33
|
|
|
} |
34
|
|
|
|
35
|
12 |
|
public function first() |
36
|
|
|
{ |
37
|
12 |
|
return reset($this->objects); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
public function last() |
41
|
|
|
{ |
42
|
9 |
|
return end($this->objects); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
public function key() |
46
|
|
|
{ |
47
|
3 |
|
return key($this->getCollection()); |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
public function next() |
51
|
|
|
{ |
52
|
6 |
|
return next($this->objects); |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
public function current() |
56
|
|
|
{ |
57
|
3 |
|
return current($this->objects); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
public function remove($key) |
61
|
|
|
{ |
62
|
3 |
|
if (!isset($this->objects[$key]) && !array_key_exists($key, $this->objects)) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
3 |
|
$removed = $this->objects[$key]; |
67
|
3 |
|
unset($this->objects[$key]); |
68
|
|
|
|
69
|
3 |
|
return $removed; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
public function removeElement($element): bool |
73
|
|
|
{ |
74
|
3 |
|
$key = array_search($element, $this->objects, true); |
75
|
|
|
|
76
|
3 |
|
if ($key === false) { |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
unset($this->objects[$key]); |
81
|
|
|
|
82
|
3 |
|
return true; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
public function contains($element): bool |
86
|
|
|
{ |
87
|
3 |
|
return in_array($element, $this->objects, true); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
public function get($key) |
91
|
|
|
{ |
92
|
6 |
|
return $this->objects[$key] ?? null; |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
public function getKeys() |
96
|
|
|
{ |
97
|
3 |
|
return array_keys($this->objects); |
98
|
|
|
} |
99
|
|
|
|
100
|
3 |
|
public function getValues() |
101
|
|
|
{ |
102
|
3 |
|
return array_values($this->objects); |
103
|
|
|
} |
104
|
|
|
|
105
|
3 |
|
public function count(): int |
106
|
|
|
{ |
107
|
3 |
|
return count($this->objects); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
public function set($key, $value): void |
111
|
|
|
{ |
112
|
3 |
|
$this->objects[$key] = $value; |
113
|
3 |
|
} |
114
|
|
|
|
115
|
3 |
|
public function add($element): void |
116
|
|
|
{ |
117
|
3 |
|
$this->objects[] = $element; |
118
|
3 |
|
} |
119
|
|
|
|
120
|
3 |
|
public function isEmpty(): bool |
121
|
|
|
{ |
122
|
3 |
|
return empty($this->objects); |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
public function clear(): void |
126
|
|
|
{ |
127
|
3 |
|
$this->objects = []; |
128
|
3 |
|
} |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|