|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Knp\DictionaryBundle\Dictionary; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayAccess; |
|
8
|
|
|
use ArrayIterator; |
|
9
|
|
|
use Countable; |
|
10
|
|
|
use IteratorAggregate; |
|
11
|
|
|
use Knp\DictionaryBundle\Dictionary; |
|
12
|
|
|
use Knp\DictionaryBundle\Exception\DictionaryNotFoundException; |
|
13
|
|
|
use RuntimeException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @implements ArrayAccess<string, Dictionary<mixed>> |
|
17
|
|
|
* @implements IteratorAggregate<string, Dictionary<mixed>> |
|
18
|
|
|
*/ |
|
19
|
|
|
final class Collection implements ArrayAccess, Countable, IteratorAggregate |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var array<string, Dictionary<mixed>> |
|
|
|
|
|
|
23
|
|
|
*/ |
|
24
|
|
|
private $dictionaries = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Dictionary<mixed> ...$dictionaries |
|
28
|
|
|
*/ |
|
29
|
32 |
|
public function __construct(Dictionary ...$dictionaries) |
|
30
|
|
|
{ |
|
31
|
32 |
|
foreach ($dictionaries as $dictionary) { |
|
32
|
21 |
|
$this->add($dictionary); |
|
33
|
|
|
} |
|
34
|
32 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Dictionary<mixed> $dictionary |
|
38
|
|
|
*/ |
|
39
|
23 |
|
public function add(Dictionary $dictionary): void |
|
40
|
|
|
{ |
|
41
|
23 |
|
$this->dictionaries[$dictionary->getName()] = $dictionary; |
|
42
|
23 |
|
} |
|
43
|
|
|
|
|
44
|
11 |
|
public function offsetExists($offset) |
|
45
|
|
|
{ |
|
46
|
11 |
|
return \array_key_exists($offset, $this->dictionaries); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param mixed $offset |
|
51
|
|
|
* |
|
52
|
|
|
* @throws DictionaryNotFoundException |
|
53
|
|
|
* |
|
54
|
|
|
* @return Dictionary<mixed> |
|
55
|
|
|
*/ |
|
56
|
10 |
|
public function offsetGet($offset) |
|
57
|
|
|
{ |
|
58
|
10 |
|
if (!$this->offsetExists($offset)) { |
|
59
|
|
|
throw new DictionaryNotFoundException($offset, array_keys($this->dictionaries)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
10 |
|
return $this->dictionaries[$offset]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function offsetSet($offset, $value): void |
|
66
|
|
|
{ |
|
67
|
1 |
|
throw new RuntimeException( |
|
68
|
1 |
|
'To add a Dictionary to the Collection, use Knp\DictionaryBundle\Dictionary\Collection::add(Dictionary $dictionary).' |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function offsetUnset($offset): void |
|
73
|
|
|
{ |
|
74
|
1 |
|
throw new RuntimeException('It is not possible to remove a dictionary from the collection.'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
public function getIterator() |
|
78
|
|
|
{ |
|
79
|
2 |
|
return new ArrayIterator($this->dictionaries); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function count(): int |
|
83
|
|
|
{ |
|
84
|
1 |
|
return \count($this->dictionaries); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|