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\AbstractCollection; |
14
|
|
|
use Fusion\Collection\Contracts\CollectionValidationInterface; |
15
|
|
|
use Fusion\Collection\Contracts\DictionaryInterface; |
16
|
|
|
use Fusion\Collection\Exceptions\CollectionException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* An implementation of a dictionary collection. |
20
|
|
|
* |
21
|
|
|
* A dictionary holds values in a key/value pair with the keys consisting of strings and the values |
22
|
|
|
* consisting of any value that can be stored in a PHP array that isn't `null`. |
23
|
|
|
* |
24
|
|
|
* Dictionaries are traversable and can be looped or accessed directly using array index notation. |
25
|
|
|
* |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
*/ |
28
|
|
|
class Dictionary extends AbstractCollection implements DictionaryInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Creates a new `Dictionary` instance with an optional set of starter items. |
32
|
|
|
* |
33
|
|
|
* The initial items must be an associative array with string keys and values that are not |
34
|
|
|
* `null`. |
35
|
|
|
* |
36
|
|
|
* This constructor with throw a `CollectionException` if any of the starter items contain a |
37
|
|
|
* `null` value. |
38
|
|
|
* |
39
|
|
|
* @param CollectionValidationInterface $validator |
40
|
|
|
* @param array $items |
41
|
|
|
* |
42
|
|
|
* @throws CollectionException |
43
|
|
|
*/ |
44
|
31 |
|
public function __construct(CollectionValidationInterface $validator, array $items = []) |
45
|
|
|
{ |
46
|
31 |
|
$this->validator = $validator; |
47
|
|
|
|
48
|
31 |
|
foreach ($items as $key => $value) { |
49
|
7 |
|
$this->add($key, $value); |
50
|
|
|
} |
51
|
29 |
|
} |
52
|
|
|
|
53
|
|
|
/** {@inheritdoc} */ |
54
|
19 |
|
public function add(string $key, $value): DictionaryInterface |
55
|
|
|
{ |
56
|
19 |
|
$this->offsetSet($key, $value); |
57
|
18 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** {@inheritdoc} */ |
61
|
2 |
|
public function replace(string $key, $value): DictionaryInterface |
62
|
|
|
{ |
63
|
2 |
|
return $this->add($key, $value); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** {@inheritdoc} */ |
67
|
5 |
|
public function find(string $key) |
68
|
|
|
{ |
69
|
5 |
|
$this->validator->validateOffsetExists($key, $this); |
70
|
3 |
|
return $this->offsetGet($key); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Retrieves a value at the given offset. |
75
|
|
|
* |
76
|
|
|
* This method will throw a `CollectionException` if the given offset is not a string or if the |
77
|
|
|
* given offset does not exist in the collection. |
78
|
|
|
* |
79
|
|
|
* @param mixed $offset |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
* |
83
|
|
|
* @throws CollectionException |
84
|
|
|
* @see \Fusion\Collection\Contracts\AbstractCollection::offsetGet() |
85
|
|
|
* |
86
|
|
|
*/ |
87
|
8 |
|
public function offsetGet($offset) |
88
|
|
|
{ |
89
|
8 |
|
$this->validator->validateStringValue($offset); |
90
|
7 |
|
$this->validator->validateOffsetExists($offset, $this); |
91
|
5 |
|
return parent::offsetGet($offset); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Sets a value at the given offset. |
96
|
|
|
* |
97
|
|
|
* This method will throw a `CollectionException` if the offset is not a string or if the value |
98
|
|
|
* is `null`. |
99
|
|
|
* |
100
|
|
|
* @param mixed $offset |
101
|
|
|
* @param mixed $value |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
* |
105
|
|
|
* @throws CollectionException |
106
|
|
|
* @see \Fusion\Collection\Contracts\AbstractCollection::offsetSet() |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
22 |
|
public function offsetSet($offset, $value): void |
110
|
|
|
{ |
111
|
22 |
|
$this->validator->validateStringValue($offset); |
112
|
21 |
|
parent::offsetSet($offset, $value); |
113
|
|
|
} |
114
|
|
|
} |