Collection::replace()   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 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\AbstractCollection;
14
use Fusion\Collection\Contracts\CollectionInterface;
15
use Fusion\Collection\Contracts\CollectionValidationInterface;
16
use Fusion\Collection\Exceptions\CollectionException;
17
18
/**
19
 * An implementation of a collection.
20
 *
21
 * A collection holds values internally with a numeric index. The values can consist of any value
22
 * that can be stored in a PHP array that isn't `null`. The internal index grows and shrink with
23
 * the collection as items are removed or added.
24
 *
25
 * Collections are traversable and can be looped or accessed directly using array index notation.
26
 *
27
 * @since 1.0.0
28
 */
29
class Collection extends AbstractCollection implements CollectionInterface
30
{
31
    /**
32
     * Instantiates a collection object with an optional array of starter items.
33
     *
34
     * If the starting items contain any `null` values, an exception will be thrown.
35
     *
36
     * @param CollectionValidationInterface $validator
37
     * @param array $items
38
     *
39
     * @throws CollectionException
40
     */
41 56
    public function __construct(CollectionValidationInterface $validator, array $items = [])
42
    {
43 56
        $this->validator = $validator;
44
45 56
        foreach ($items as $item) {
46 14
            $this->add($item);
47
        }
48 55
    }
49
50
    /** {@inheritdoc} */
51 46
    public function add($value): CollectionInterface
52
    {
53 46
        $this->validator->validateNonNullValue($value);
54 46
        array_push($this->collection, $value);
55 46
        return $this;
56
    }
57
58
    /** {@inheritdoc} */
59 3
    public function replace(int $key, $value): CollectionInterface
60
    {
61 3
        $this->offsetSet($key, $value);
62 2
        return $this;
63
    }
64
65
    /** {@inheritdoc} */
66 6
    public function find(int $key)
67
    {
68 6
        $this->validator->validateOffsetExists($key, $this);
69 5
        return $this->offsetGet($key);
70
    }
71
72
    /**
73
     * Retrieves a value at the given offset.
74
     *
75
     * This method will throw a `CollectionException` if the offset is not an integer or if the
76
     * offset does not exist.
77
     *
78
     * @param mixed $offset
79
     *
80
     * @return mixed
81
     *
82
     * @throws CollectionException
83
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
84
     *
85
     * @see \Fusion\Collection\Contracts\AbstractCollection::offsetGet()
86
     */
87 13
    public function offsetGet($offset)
88
    {
89 13
        $this->validator->validateIntValue($offset);
90 12
        $this->validator->validateOffsetExists($offset, $this);
91 9
        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 does not exist or if the offset
98
     * is not an integer.
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
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
108
     *
109
     */
110 8
    public function offsetSet($offset, $value): void
111
    {
112 8
        $this->validator->validateIntValue($offset);
113 7
        $this->validator->validateOffsetExists($offset, $this);
114 6
        parent::offsetSet($offset, $value);
115
    }
116
}