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