Passed
Push — master ( 7067f6...c51298 )
by Julien
01:15 queued 11s
created

Collection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 42.86%

Importance

Changes 0
Metric Value
dl 0
loc 140
ccs 9
cts 21
cp 0.4286
rs 10
c 0
b 0
f 0
wmc 16

13 Methods

Rating   Name   Duplication   Size   Complexity  
A unserialize() 0 3 1
A offsetSet() 0 6 2
A serialize() 0 3 1
A offsetGet() 0 3 2
A getIterator() 0 3 1
A offsetUnset() 0 3 1
A count() 0 3 1
A getTotalItems() 0 3 1
A getExtraProperties() 0 3 1
A offsetExists() 0 3 1
A toArray() 0 3 1
A __construct() 0 4 1
A getExtraProperty() 0 4 2
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
     * @access private
29
     */
30
    private $extraProperties;
31
32
    /**
33
     * @param array $elements The data elements as an array.
34
     * @param array $extraProperties The extra properties.
35
     */
36
    public function __construct(array $elements = [], array $extraProperties = [])
37
    {
38 1
        $this->elements = $elements;
39 1
        $this->extraProperties = $extraProperties;
40 1
    }
41
42
    /**
43
     * Returns inner elements collection.
44
     *
45
     * @return array
46
     */
47
    public function toArray()
48
    {
49 1
        return $this->elements;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function serialize()
56
    {
57
        return serialize($this->elements);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function unserialize($values)
64
    {
65
        $this->elements = unserialize($values);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function count()
72
    {
73 1
        return count($this->elements);
74
    }
75
76
    /**
77
     * Returns element count in collection.
78
     *
79
     * @return int
80
     */
81
    public function getTotalItems()
82
    {
83 1
        return $this->count();
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function offsetSet($offset, $value)
90
    {
91
        if (is_null($offset)) {
92
            $this->elements[] = $value;
93
        } else {
94
            $this->elements[$offset] = $value;
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function offsetExists($offset)
102
    {
103
        return isset($this->elements[$offset]);
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function offsetUnset($offset)
110
    {
111
        unset($this->elements[$offset]);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function offsetGet($offset)
118
    {
119
        return isset($this->elements[$offset]) ? $this->elements[$offset] : null;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getIterator()
126
    {
127
        return new ArrayIterator($this->elements);
128
    }
129
130
    /**
131
     * getExtraProperties
132
     *
133
     * @access public
134
     * @return array
135
     */
136
    public function getExtraProperties()
137
    {
138 1
        return $this->extraProperties;
139
    }
140
141
    /**
142
     * return the value of an extra property
143
     *
144
     * @param string $key
145
     * @access public
146
     * @return mixed
147
     */
148
    public function getExtraProperty($key)
149
    {
150 1
        if (isset($this->extraProperties[$key])) {
151 1
            return $this->extraProperties[$key];
152
        }
153 1
    }
154
}
155