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