TypedCollection::offsetSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 2
c 2
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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\CollectionInterface;
14
use Fusion\Collection\Contracts\CollectionValidationInterface;
15
use Fusion\Collection\Exceptions\CollectionException;
16
17
/**
18
 * An implementation of a type-specific collection.
19
 *
20
 * A type-specific collection holds values internally with a numeric index. Upon construction the
21
 * consumer of this class must specify the fully qualified name of a class or interface that this
22
 * collection will accept.  This collection will only hold values that have this type or a
23
 * `CollectionException` will be thrown.
24
 *
25
 * Type-specific collections are traversable and can be looped or accessed directly using array
26
 * index notation.
27
 *
28
 * @since 1.0.0
29
 */
30
class TypedCollection extends Collection
31
{
32
    private string $acceptedType;
33
34
    /**
35
     * Creates a type-specific collection that will allow instances of the `acceptedType`.
36
     *
37
     * Optionally, an array of starter items of the `acceptedType` can also be provided. The
38
     * constructor will throw a `CollectionException` if an empty string is provided for
39
     * `acceptedType` or if any of the starter items are not an instance of the `acceptedType`.
40
     *
41
     * @param 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 CollectionException
46
     */
47 14
    public function __construct(CollectionValidationInterface $validator, string $acceptedType, array $items = [])
48
    {
49 14
        $validator->validateNonEmptyAcceptedType($acceptedType);
50 13
        $this->acceptedType = $acceptedType;
51 13
        parent::__construct($validator, $items);
52 12
    }
53
54
    /**
55
     * Adds a value to the collection.
56
     *
57
     * This method will throw a `CollectionException` if the value is not an instance of the
58
     * `acceptedType`.
59
     *
60
     * @param mixed $value
61
     *
62
     * @return CollectionInterface
63
     *
64
     * @throws CollectionException
65
     */
66 12
    public function add($value): CollectionInterface
67
    {
68 12
        $this->validator->validateValueIsAcceptedType($value, $this->acceptedType);
69 10
        return parent::add($value);
70
    }
71
72
    /**
73
     * Replaces a value in the collection at the given key.
74
     *
75
     * This method will throw a `CollectionException` if the value give is not an instance of the
76
     * `acceptedType`.
77
     *
78
     * @param int $key
79
     * @param mixed $value
80
     *
81
     * @return CollectionInterface
82
     *
83
     * @throws CollectionException
84
     */
85 3
    public function replace(int $key, $value): CollectionInterface
86
    {
87 3
        $this->validator->validateValueIsAcceptedType($value, $this->acceptedType);
88 1
        return parent::replace($key, $value);
89
    }
90
91
    /**
92
     * Sets a value at the given offset.
93
     *
94
     * This method will throw a `CollectionException` if the value is not an instance of the
95
     * accepted type, if the offset does not exist, or if the offset is not an integer.
96
     *
97
     * @param mixed $offset
98
     * @param mixed $value
99
     *
100
     * @return void
101
     *
102
     * @throws CollectionException
103
     * @see \Fusion\Collection\Collection::offsetSet()
104
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
105
     *
106
     */
107 4
    public function offsetSet($offset, $value): void
108
    {
109 4
        $this->validator->validateValueIsAcceptedType($value, $this->acceptedType);
110 2
        parent::offsetSet($offset, $value);
111
    }
112
}