Passed
Branch validation-refactor (de59e2)
by Jason
03:09
created

Dictionary::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 2
crap 2
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
17
/**
18
 * An implementation of a dictionary collection.
19
 *
20
 * A dictionary holds values in a key/value pair with the keys consisting of strings and the values
21
 * consisting of any value that can be stored in a PHP array that isn't `null`.
22
 *
23
 * Dictionaries are traversable and can be looped or accessed directly using array index notation.
24
 *
25
 * @since 1.0.0
26
 */
27
class Dictionary extends AbstractCollection implements DictionaryInterface
28
{
29
    /**
30
     * Creates a new `Dictionary` instance with an optional set of starter items.
31
     *
32
     * The initial items must be an associative array with string keys and values that are not
33
     * `null`.
34
     *
35
     * This constructor with throw a `CollectionException` if any of the starter items contain a
36
     * `null` value.
37
     *
38
     * @param \Fusion\Collection\Contracts\CollectionValidationInterface $validator
39
     * @param array $items
40
     *
41
     * @throws \Fusion\Collection\Exceptions\CollectionException
42
     */
43 30
    public function __construct(CollectionValidationInterface $validator, array $items = [])
44
    {
45 30
        $this->validator = $validator;
46
47 30
        foreach ($items as $key => $value)
48
        {
49 5
            $this->add($key, $value);
50
        }
51 28
    }
52
53
    /** {@inheritdoc} */
54 18
    public function add(string $key, $value): DictionaryInterface
55
    {
56 18
        $this->offsetSet($key, $value);
57 17
        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
     * @see \Fusion\Collection\Contracts\AbstractCollection::offsetGet()
80
     *
81
     * @param mixed $offset
82
     *
83
     * @return mixed
84
     *
85
     * @throws \Fusion\Collection\Exceptions\CollectionException
86
     */
87 9
    public function offsetGet($offset)
88
    {
89 9
        $this->validator->validateStringValue($offset);
90 8
        $this->validator->validateOffsetExists($offset, $this);
91 6
        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
     * @see \Fusion\Collection\Contracts\AbstractCollection::offsetSet()
101
     *
102
     * @param mixed $offset
103
     * @param mixed $value
104
     *
105
     * @return void
106
     *
107
     * @throws \Fusion\Collection\Exceptions\CollectionException
108
     */
109 21
    public function offsetSet($offset, $value): void
110
    {
111 21
        $this->validator->validateStringValue($offset);
112 20
        parent::offsetSet($offset, $value);
113
    }
114
}