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