|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Maxim Sokolovsky |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace WS\Utils\Collections; |
|
7
|
|
|
|
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
|
|
10
|
|
|
class ImmutableList implements ListSequence |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
private $decoratedList; |
|
14
|
|
|
|
|
15
|
9 |
|
public function __construct(?array $elements = null) |
|
16
|
|
|
{ |
|
17
|
9 |
|
$this->decoratedList = new ArrayList($elements); |
|
18
|
9 |
|
} |
|
19
|
|
|
|
|
20
|
1 |
|
public static function fromCollection(Collection $collection): self |
|
21
|
|
|
{ |
|
22
|
1 |
|
return new static($collection->toArray()); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public static function of(...$elements): self |
|
26
|
|
|
{ |
|
27
|
1 |
|
return new static($elements ?: null); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function stream(): Stream |
|
31
|
|
|
{ |
|
32
|
1 |
|
return $this->decoratedList->stream(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function remove($element): bool |
|
36
|
|
|
{ |
|
37
|
1 |
|
throw $this->createBlockingException(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function get(int $index) |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this->decoratedList->get($index); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function set($element, int $index) |
|
46
|
|
|
{ |
|
47
|
1 |
|
throw $this->createBlockingException(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function indexOf($element): ?int |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->decoratedList->indexOf($element); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
1 |
|
public function lastIndexOf($element): ?int |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->decoratedList->lastIndexOf($element); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
public function removeAt(int $index) |
|
61
|
|
|
{ |
|
62
|
1 |
|
throw $this->createBlockingException(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
7 |
|
private function createBlockingException(): RuntimeException |
|
66
|
|
|
{ |
|
67
|
7 |
|
return new RuntimeException('Is immutable list. Everything modifier call is prohibited'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function add($element): bool |
|
71
|
|
|
{ |
|
72
|
1 |
|
throw $this->createBlockingException(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function addAll(iterable $elements): bool |
|
76
|
|
|
{ |
|
77
|
1 |
|
throw $this->createBlockingException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function merge(Collection $collection): bool |
|
81
|
|
|
{ |
|
82
|
1 |
|
throw $this->createBlockingException(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function clear(): void |
|
86
|
|
|
{ |
|
87
|
1 |
|
throw $this->createBlockingException(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function contains($element): bool |
|
91
|
|
|
{ |
|
92
|
1 |
|
return $this->decoratedList->contains($element); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function equals(Collection $collection): bool |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->decoratedList->equals($collection); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
public function size(): int |
|
101
|
|
|
{ |
|
102
|
2 |
|
return $this->decoratedList->size(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function isEmpty(): bool |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->decoratedList->isEmpty(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2 |
|
public function toArray(): array |
|
111
|
|
|
{ |
|
112
|
2 |
|
return $this->decoratedList->toArray(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
1 |
|
public function copy(): Collection |
|
116
|
|
|
{ |
|
117
|
1 |
|
return $this->decoratedList->copy(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
public function getIterator() |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->decoratedList->getIterator(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|