|
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
|
240 |
|
public function __construct(?array $elements = null) |
|
14
|
|
|
{ |
|
15
|
240 |
|
if ($elements === null) { |
|
16
|
96 |
|
return; |
|
17
|
|
|
} |
|
18
|
211 |
|
$this->setElements($elements); |
|
19
|
211 |
|
} |
|
20
|
|
|
|
|
21
|
105 |
|
public static function of(...$elements): self |
|
22
|
|
|
{ |
|
23
|
105 |
|
return new static($elements ?: null); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
79 |
|
public function add($element): bool |
|
27
|
|
|
{ |
|
28
|
79 |
|
if ($this->pointer === PHP_INT_MAX) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
79 |
|
$this->pointer++; |
|
32
|
79 |
|
$this->elements[] = $element; |
|
33
|
79 |
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
28 |
|
public function addAll(iterable $elements): bool |
|
37
|
|
|
{ |
|
38
|
28 |
|
foreach ($elements as $element) { |
|
39
|
23 |
|
$this->elements[] = $element; |
|
40
|
|
|
} |
|
41
|
28 |
|
$newPointer = count($this->elements) - 1; |
|
42
|
28 |
|
if ($newPointer > PHP_INT_MAX) { |
|
43
|
|
|
$this->elements = array_slice($this->elements, 0, $this->pointer); |
|
44
|
|
|
} else { |
|
45
|
28 |
|
$this->pointer = $newPointer; |
|
46
|
|
|
} |
|
47
|
28 |
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
public function merge(Collection $collection): bool |
|
51
|
|
|
{ |
|
52
|
6 |
|
return $this->addAll($collection->toArray()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
8 |
|
public function clear(): void |
|
56
|
|
|
{ |
|
57
|
8 |
|
$this->setElements([]); |
|
58
|
8 |
|
} |
|
59
|
|
|
|
|
60
|
10 |
|
public function contains($element): bool |
|
61
|
|
|
{ |
|
62
|
10 |
|
return in_array($element, $this->elements, true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
45 |
|
public function equals(Collection $collection): bool |
|
66
|
|
|
{ |
|
67
|
45 |
|
return $this->toArray() === $collection->toArray(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
72 |
|
public function size(): int |
|
71
|
|
|
{ |
|
72
|
72 |
|
return $this->pointer + 1; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
27 |
|
public function isEmpty(): bool |
|
76
|
|
|
{ |
|
77
|
27 |
|
return $this->pointer === -1; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
169 |
|
public function toArray(): array |
|
81
|
|
|
{ |
|
82
|
169 |
|
return $this->elements; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
75 |
|
public function getIterator() |
|
86
|
|
|
{ |
|
87
|
75 |
|
yield from $this->toArray(); |
|
88
|
61 |
|
} |
|
89
|
|
|
|
|
90
|
135 |
|
public function copy(): Collection |
|
91
|
|
|
{ |
|
92
|
135 |
|
return clone $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
211 |
|
protected function setElements(array $elements): void |
|
96
|
|
|
{ |
|
97
|
211 |
|
$this->elements = array_values($elements); |
|
98
|
211 |
|
$this->pointer = count($elements) - 1; |
|
99
|
211 |
|
} |
|
100
|
|
|
|
|
101
|
10 |
|
protected function getElements(): array |
|
102
|
|
|
{ |
|
103
|
10 |
|
return $this->elements; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
abstract public function stream(): Stream; |
|
107
|
|
|
} |
|
108
|
|
|
|