1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Knp\DictionaryBundle\Dictionary; |
6
|
|
|
|
7
|
|
|
use Knp\DictionaryBundle\Dictionary; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @template E |
11
|
|
|
* |
12
|
|
|
* @implements Dictionary<E> |
13
|
|
|
*/ |
14
|
|
|
final class Invokable implements Dictionary |
15
|
|
|
{ |
16
|
|
|
private bool $invoked = false; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array<mixed, mixed> |
20
|
|
|
*/ |
21
|
|
|
private array $values = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var callable |
25
|
|
|
*/ |
26
|
|
|
private $callable; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param mixed[] $callableArgs |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
private readonly string $name, |
33
|
|
|
callable $callable, |
34
|
|
|
private readonly array $callableArgs = [] |
35
|
|
|
) { |
36
|
|
|
$this->callable = $callable; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getValues(): array |
40
|
|
|
{ |
41
|
|
|
$this->invoke(); |
42
|
|
|
|
43
|
|
|
return $this->values; |
44
|
|
|
} |
45
|
32 |
|
|
46
|
|
|
public function getName(): string |
47
|
32 |
|
{ |
48
|
32 |
|
return $this->name; |
49
|
32 |
|
} |
50
|
32 |
|
|
51
|
|
|
public function getKeys(): array |
52
|
6 |
|
{ |
53
|
|
|
$this->invoke(); |
54
|
6 |
|
|
55
|
|
|
return array_keys($this->values); |
56
|
|
|
} |
57
|
11 |
|
|
58
|
|
|
public function offsetExists($offset): bool |
59
|
11 |
|
{ |
60
|
|
|
$this->invoke(); |
61
|
10 |
|
|
62
|
|
|
return \array_key_exists($offset, $this->values); |
63
|
|
|
} |
64
|
3 |
|
|
65
|
|
|
#[\ReturnTypeWillChange] |
66
|
3 |
|
public function offsetGet($offset) |
67
|
|
|
{ |
68
|
3 |
|
$this->invoke(); |
69
|
|
|
|
70
|
|
|
return $this->values[$offset]; |
71
|
2 |
|
} |
72
|
|
|
|
73
|
2 |
|
public function offsetSet($offset, $value): void |
74
|
|
|
{ |
75
|
2 |
|
$this->invoke(); |
76
|
|
|
|
77
|
|
|
$this->values[$offset] = $value; |
78
|
5 |
|
} |
79
|
|
|
|
80
|
5 |
|
public function offsetUnset($offset): void |
81
|
|
|
{ |
82
|
5 |
|
$this->invoke(); |
83
|
|
|
|
84
|
|
|
unset($this->values[$offset]); |
85
|
2 |
|
} |
86
|
|
|
|
87
|
2 |
|
/** |
88
|
|
|
* @return \ArrayIterator<int|string, mixed> |
89
|
2 |
|
*/ |
90
|
2 |
|
public function getIterator(): \ArrayIterator |
91
|
|
|
{ |
92
|
2 |
|
$this->invoke(); |
93
|
|
|
|
94
|
2 |
|
return new \ArrayIterator($this->values); |
95
|
|
|
} |
96
|
2 |
|
|
97
|
2 |
|
public function count(): int |
98
|
|
|
{ |
99
|
|
|
$this->invoke(); |
100
|
|
|
|
101
|
|
|
return \count($this->values); |
102
|
3 |
|
} |
103
|
|
|
|
104
|
3 |
|
private function invoke(): void |
105
|
|
|
{ |
106
|
3 |
|
if ($this->invoked) { |
107
|
|
|
return; |
108
|
|
|
} |
109
|
2 |
|
|
110
|
|
|
$values = ($this->callable)(...$this->callableArgs); |
111
|
2 |
|
|
112
|
|
|
if (!\is_array($values)) { |
113
|
2 |
|
throw new \InvalidArgumentException( |
114
|
|
|
'Dictionary callable must return an array or an instance of ArrayAccess.' |
115
|
|
|
); |
116
|
23 |
|
} |
117
|
|
|
|
118
|
23 |
|
$this->values = $values; |
119
|
8 |
|
$this->invoked = true; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|