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

TypedCollection::throwExceptionIfNotAcceptedType()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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