EntityCollection::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
cc 2
eloc 15
nc 2
nop 2
1
<?php
2
3
namespace Chadicus\Entity;
4
5
use Chadicus\Spl\Traits\IteratorTrait;
6
use DominionEnterprises\Util;
7
8
/**
9
 * Represents a collection of entities.
10
 */
11
final class EntityCollection implements EntityInterface, \Iterator, \ArrayAccess
12
{
13
    use IteratorTrait;
14
15
    /**
16
     * Array of EntityInterface objects.
17
     *
18
     * @var EntityInterface[]
19
     */
20
    private $container = [];
21
22
    /**
23
     * Sets the value at the specified index $offset to $value.
24
     *
25
     * @param integer $offset The index being set.
26
     * @param mixed   $value  The new value for the index.
27
     *
28
     * @return void
29
     *
30
     * @throws \InvalidArgumentException Thrown if $offset is not null and is not an integer.
31
     */
32
    public function offsetSet($offset, $value)
33
    {
34
        Util::ensure(
35
            true,
36
            $value instanceof EntityInterface,
37
            '\InvalidArgumentException',
38
            ['$value must be an instance of EntityInterface', 400]
39
        );
40
41
        if ($offset === null) {
42
            $this->container[] = $value;
43
            return;
44
        }
45
46
        $offset = Util::ensureNot(
47
            false,
48
            filter_var($offset, FILTER_VALIDATE_INT),
49
            '\InvalidArgumentException',
50
            ['$offset must be an integer', 400]
51
        );
52
53
        $this->container[$offset] = $value;
54
    }
55
56
    /**
57
     * Returns whether the requested index exists.
58
     *
59
     * @param integer $offset The index being checked.
60
     *
61
     * @return boolean
62
     */
63 View Code Duplication
    public function offsetExists($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        $offset = filter_var($offset, FILTER_VALIDATE_INT);
66
        if ($offset !== false) {
67
            return array_key_exists($offset, $this->container);
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * Unsets the value at the specified index.
75
     *
76
     * @param integer $offset The index being unset.
77
     *
78
     * @return void
79
     */
80
    public function offsetUnset($offset)
81
    {
82
        $offset = filter_var($offset, FILTER_VALIDATE_INT);
83
        if ($offset !== false) {
84
            unset($this->container[$offset]);
85
        }
86
    }
87
88
    /**
89
     * Returns the value at the specified index.
90
     *
91
     * @param integer $offset The index with the value.
92
     *
93
     * @return EntityInterface|null
94
     */
95 View Code Duplication
    public function offsetGet($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $offset = filter_var($offset, FILTER_VALIDATE_INT);
98
        if ($offset === false) {
99
            return null;
100
        }
101
102
        return Util\Arrays::get($this->container, $offset);
103
    }
104
105
    /**
106
     * Converts this entity collection into an xml string.
107
     *
108
     * @return string
109
     */
110
    public function jsonSerialize()
111
    {
112
        return $this->container;
113
    }
114
}
115