|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Maxim Sokolovsky |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace WS\Utils\Collections; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractCollection implements Collection |
|
9
|
|
|
{ |
|
10
|
|
|
protected $elements = []; |
|
11
|
|
|
protected $pointer = -1; |
|
12
|
|
|
|
|
13
|
311 |
|
public function __construct(?array $elements = null) |
|
14
|
|
|
{ |
|
15
|
311 |
|
if ($elements === null) { |
|
16
|
107 |
|
return; |
|
17
|
|
|
} |
|
18
|
281 |
|
$this->setElements($elements); |
|
19
|
273 |
|
} |
|
20
|
|
|
|
|
21
|
125 |
|
public static function of(...$elements): self |
|
22
|
|
|
{ |
|
23
|
125 |
|
return new static($elements ?: null); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
91 |
|
public function add($element): bool |
|
27
|
|
|
{ |
|
28
|
91 |
|
if ($this->pointer === PHP_INT_MAX) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
91 |
|
$this->pointer++; |
|
32
|
91 |
|
$this->elements[] = $element; |
|
33
|
91 |
|
$this->afterElementAdd($element); |
|
34
|
90 |
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
29 |
|
public function addAll(iterable $elements): bool |
|
38
|
|
|
{ |
|
39
|
29 |
|
foreach ($elements as $element) { |
|
40
|
24 |
|
$this->elements[] = $element; |
|
41
|
24 |
|
$this->afterElementAdd($element); |
|
42
|
|
|
} |
|
43
|
29 |
|
$newPointer = count($this->elements) - 1; |
|
44
|
29 |
|
if ($newPointer > PHP_INT_MAX) { |
|
45
|
|
|
$this->elements = array_slice($this->elements, 0, $this->pointer); |
|
46
|
|
|
} else { |
|
47
|
29 |
|
$this->pointer = $newPointer; |
|
48
|
|
|
} |
|
49
|
29 |
|
return true; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
6 |
|
public function merge(Collection $collection): bool |
|
53
|
|
|
{ |
|
54
|
6 |
|
return $this->addAll($collection->toArray()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
8 |
|
public function clear(): void |
|
58
|
|
|
{ |
|
59
|
8 |
|
$this->setElements([]); |
|
60
|
8 |
|
} |
|
61
|
|
|
|
|
62
|
10 |
|
public function contains($element): bool |
|
63
|
|
|
{ |
|
64
|
10 |
|
return in_array($element, $this->elements, true); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
48 |
|
public function equals(Collection $collection): bool |
|
68
|
|
|
{ |
|
69
|
48 |
|
return $this->toArray() === $collection->toArray(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
84 |
|
public function size(): int |
|
73
|
|
|
{ |
|
74
|
84 |
|
return $this->pointer + 1; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
27 |
|
public function isEmpty(): bool |
|
78
|
|
|
{ |
|
79
|
27 |
|
return $this->pointer === -1; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
227 |
|
public function toArray(): array |
|
83
|
|
|
{ |
|
84
|
227 |
|
return $this->elements; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
124 |
|
public function getIterator() |
|
88
|
|
|
{ |
|
89
|
124 |
|
yield from $this->toArray(); |
|
90
|
104 |
|
} |
|
91
|
|
|
|
|
92
|
162 |
|
public function copy(): Collection |
|
93
|
|
|
{ |
|
94
|
162 |
|
return clone $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
281 |
|
protected function setElements(array $elements): void |
|
98
|
|
|
{ |
|
99
|
281 |
|
$this->elements = array_values($elements); |
|
100
|
281 |
|
$this->pointer = count($elements) - 1; |
|
101
|
281 |
|
$this->afterElementsSet(); |
|
102
|
273 |
|
} |
|
103
|
|
|
|
|
104
|
10 |
|
protected function getElements(): array |
|
105
|
|
|
{ |
|
106
|
10 |
|
return $this->elements; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
abstract protected function afterElementAdd($element): void; |
|
110
|
|
|
|
|
111
|
|
|
abstract protected function afterElementsSet(): void; |
|
112
|
|
|
|
|
113
|
|
|
abstract public function stream(): Stream; |
|
114
|
|
|
} |
|
115
|
|
|
|