1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Railt package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Railt\SDL\Ast\Generic; |
12
|
|
|
|
13
|
|
|
use Railt\SDL\Ast\Node; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Collection |
17
|
|
|
*/ |
18
|
|
|
abstract class Collection extends Node implements \ArrayAccess, \Countable |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array|Node[] |
22
|
|
|
*/ |
23
|
|
|
private array $items = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Closure|null |
27
|
|
|
*/ |
28
|
|
|
private ?\Closure $generic = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Collection constructor. |
32
|
|
|
* |
33
|
|
|
* @param \Closure $generic |
34
|
|
|
* @param array $items |
35
|
|
|
* @throws \TypeError |
36
|
|
|
*/ |
37
|
|
|
public function __construct(\Closure $generic, array $items = []) |
38
|
|
|
{ |
39
|
|
|
$this->generic = $generic; |
40
|
|
|
|
41
|
|
|
foreach ($items as $key => $item) { |
42
|
|
|
$this->items[$key] = $this->assert($item); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param int $name |
48
|
|
|
* @param mixed $value |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function __set(int $name, $value): void |
52
|
|
|
{ |
53
|
|
|
$this->items[$name] = $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param mixed $value |
58
|
|
|
* @return mixed |
59
|
|
|
* @throws \TypeError |
60
|
|
|
*/ |
61
|
|
|
private function assert($value) |
62
|
|
|
{ |
63
|
|
|
if (! ($this->generic)($value)) { |
64
|
|
|
$type = \is_object($value) ? \get_class($value) : \gettype($value); |
65
|
|
|
|
66
|
|
|
throw new \TypeError(\sprintf('A type %s can not be a part of %s', $type, static::class)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \Traversable|Node[] |
74
|
|
|
*/ |
75
|
|
|
public function getIterator(): \Traversable |
76
|
|
|
{ |
77
|
|
|
return new \ArrayIterator($this->items); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int|string $offset |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function offsetExists($offset): bool |
85
|
|
|
{ |
86
|
|
|
\assert(\is_int($offset) || \is_string($offset)); |
87
|
|
|
|
88
|
|
|
return \array_key_exists($offset, $this->items); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param int|string $offset |
93
|
|
|
* @return mixed|null |
94
|
|
|
*/ |
95
|
|
|
public function offsetGet($offset) |
96
|
|
|
{ |
97
|
|
|
\assert(\is_int($offset) || \is_string($offset)); |
98
|
|
|
|
99
|
|
|
return $this->items[$offset] ?? null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int|string $offset |
104
|
|
|
* @param mixed $value |
105
|
|
|
* @return void |
106
|
|
|
* @throws \TypeError |
107
|
|
|
*/ |
108
|
|
|
public function offsetSet($offset, $value): void |
109
|
|
|
{ |
110
|
|
|
\assert(\is_int($offset) || \is_string($offset)); |
111
|
|
|
|
112
|
|
|
$this->items[$offset] = $this->assert($value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int|string $offset |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function offsetUnset($offset): void |
120
|
|
|
{ |
121
|
|
|
\assert(\is_int($offset) || \is_string($offset)); |
122
|
|
|
|
123
|
|
|
unset($this->items[$offset]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function count(): int |
130
|
|
|
{ |
131
|
|
|
return \count($this->items); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function jsonSerialize(): array |
138
|
|
|
{ |
139
|
|
|
return $this->items; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|