1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Part of the Fusion.Collection package. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Fusion\Collection; |
12
|
|
|
|
13
|
|
|
use Fusion\Collection\Contracts\CollectionValidationInterface; |
14
|
|
|
use Fusion\Collection\Contracts\DictionaryInterface; |
15
|
|
|
use Fusion\Collection\Exceptions\CollectionException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* An implementation of a type-specific dictionary collection. |
19
|
|
|
* |
20
|
|
|
* A type-specific dictionary holds values in a key/value pairs. Upon construction the consumer of |
21
|
|
|
* this class must specify the fully qualified name of a class or interface that this collection |
22
|
|
|
* will accept. This collection will only hold values that have this type or a `CollectionException` |
23
|
|
|
* will be thrown. |
24
|
|
|
* |
25
|
|
|
* Type-specific dictionaries are traversable and can be looped or accessed directly using array |
26
|
|
|
* index notation. |
27
|
|
|
* |
28
|
|
|
* @since 1.0.0 |
29
|
|
|
*/ |
30
|
|
|
class TypedDictionary extends Dictionary |
31
|
|
|
{ |
32
|
|
|
private string $acceptedType; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates a new `TypedDictionary` instance with an optional set of starter items. |
36
|
|
|
* |
37
|
|
|
* The initial items must be an associative array with string keys and values that are instances |
38
|
|
|
* of the `acceptedType`. The constructor will throw a `CollectionException` if an empty string |
39
|
|
|
* is provided for `acceptedType` or if any of the starter items are not an instance of the |
40
|
|
|
* `acceptedType`. |
41
|
|
|
* |
42
|
|
|
* @param CollectionValidationInterface $validator |
43
|
|
|
* @param string $acceptedType The fully qualified name of instances the collection will accept. |
44
|
|
|
* @param array $items A set of items to populate the collection with. |
45
|
|
|
* |
46
|
|
|
* @throws CollectionException |
47
|
|
|
*/ |
48
|
11 |
|
public function __construct(CollectionValidationInterface $validator, string $acceptedType, array $items = []) |
49
|
|
|
{ |
50
|
11 |
|
$validator->validateNonEmptyAcceptedType($acceptedType); |
51
|
10 |
|
$this->acceptedType = $acceptedType; |
52
|
10 |
|
parent::__construct($validator, $items); |
53
|
8 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adds a value to the collection at the given key offset. |
57
|
|
|
* |
58
|
|
|
* This method will throw a `CollectionException` if the value given is not an instance of the |
59
|
|
|
* `acceptedType`. |
60
|
|
|
* |
61
|
|
|
* @param string $key |
62
|
|
|
* @param $value |
63
|
|
|
* |
64
|
|
|
* @return DictionaryInterface |
65
|
|
|
* |
66
|
|
|
* @throws CollectionException |
67
|
|
|
* @see \Fusion\Collection\Dictionary::add() |
68
|
|
|
* |
69
|
|
|
*/ |
70
|
7 |
|
public function add(string $key, $value): DictionaryInterface |
71
|
|
|
{ |
72
|
7 |
|
$this->validator->validateValueIsAcceptedType($value, $this->acceptedType); |
73
|
5 |
|
return parent::add($key, $value); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** {@inheritdoc} */ |
77
|
2 |
|
public function replace(string $key, $value): DictionaryInterface |
78
|
|
|
{ |
79
|
2 |
|
return $this->add($key, $value); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets a value at the given offset. |
84
|
|
|
* |
85
|
|
|
* This method will throw a `CollectionException` if the offset is not a string or if the value |
86
|
|
|
* is not an instance of the `acceptedType`. |
87
|
|
|
* |
88
|
|
|
* @param mixed $offset |
89
|
|
|
* @param mixed $value |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
* |
93
|
|
|
* @throws CollectionException |
94
|
|
|
* @see \Fusion\Collection\Dictionary::offsetSet() |
95
|
|
|
* |
96
|
|
|
*/ |
97
|
7 |
|
public function offsetSet($offset, $value): void |
98
|
|
|
{ |
99
|
7 |
|
$this->validator->validateValueIsAcceptedType($value, $this->acceptedType); |
100
|
6 |
|
parent::offsetSet($offset, $value); |
101
|
|
|
} |
102
|
|
|
} |