Completed
Push — master ( b27094...afbc6d )
by Julien
05:50 queued 01:38
created

Collection::offsetSet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Collection;
4
5
use ArrayIterator;
6
7
/**
8
 * Class Collection
9
 *
10
 * @author Florent Clerc <[email protected]>
11
 */
12
class Collection implements \IteratorAggregate, \Serializable, \Countable, \ArrayAccess
13
{
14
    /**
15
     * The elements of the collection.
16
     *
17
     * @var array
18
     */
19
    private $elements;
20
21
    /**
22
     * extra properties that are not the main list but linked data
23
     * It can be "_embed" or "_links" for HAL response
24
     * or "hydra:totalItems" for JSON-LD
25
     * or anything you want to really ("foo" is OK for exemple)
26
     *
27
     * @var array
28
     */
29
    private $extraProperties;
30
31
    /**
32
     * @param array $elements the data elements as an array
33
     * @param array $extraProperties the extra properties
34
     */
35
    public function __construct(array $elements = [], array $extraProperties = [])
36
    {
37 1
        $this->elements = $elements;
38 1
        $this->extraProperties = $extraProperties;
39 1
    }
40
41
    /**
42
     * Returns inner elements collection.
43
     *
44
     * @return array
45
     */
46
    public function toArray()
47
    {
48 1
        return $this->elements;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function serialize()
55
    {
56
        return serialize($this->elements);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function unserialize($values)
63
    {
64
        $this->elements = unserialize($values);
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function count()
71
    {
72 1
        return count($this->elements);
73
    }
74
75
    /**
76
     * Returns element count in collection.
77
     *
78
     * @return int
79
     */
80
    public function getTotalItems()
81
    {
82 1
        return $this->count();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function offsetSet($offset, $value)
89
    {
90
        if (is_null($offset)) {
91
            $this->elements[] = $value;
92
        } else {
93
            $this->elements[$offset] = $value;
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function offsetExists($offset)
101
    {
102
        return isset($this->elements[$offset]);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function offsetUnset($offset)
109
    {
110
        unset($this->elements[$offset]);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function offsetGet($offset)
117
    {
118
        return isset($this->elements[$offset]) ? $this->elements[$offset] : null;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getIterator()
125
    {
126
        return new ArrayIterator($this->elements);
127
    }
128
129
    /**
130
     * getExtraProperties
131
     *
132
     * @return array
133
     */
134
    public function getExtraProperties()
135
    {
136 1
        return $this->extraProperties;
137
    }
138
139
    /**
140
     * return the value of an extra property
141
     *
142
     * @param string $key
143
     *
144
     * @return mixed
145
     */
146
    public function getExtraProperty($key)
147
    {
148 1
        if (isset($this->extraProperties[$key])) {
149 1
            return $this->extraProperties[$key];
150
        }
151 1
    }
152
}
153