PopoloCollection::__get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 8
loc 8
c 0
b 0
f 0
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2.1481
1
<?php
2
3
namespace EveryPolitician\EveryPoliticianPopolo\Collections;
4
5
use \Countable;
6
use \ArrayAccess;
7
use \Iterator;
8
use \EveryPolitician\EveryPoliticianPopolo\Exceptions;
9
10
class PopoloCollection implements Countable, ArrayAccess, Iterator
11
{
12
    protected $properties = [
13
        'first',
14
    ];
15
    public $lookupFromKey;
16
    private $objectClass;
17
    private $objectArr;
18
    private $position;
19
20
    /**
21
     * Creates a new instance
22
     */
23 279
    public function __construct($dataArr, $objectClass, $allPopolo)
24
    {
25 279
        $this->position = 0;
26 279
        $this->objectClass = $objectClass;
27 279
        $this->objectArr = [];
28 279
        foreach ($dataArr as $data) {
29 255
            $this->objectArr[] = new $objectClass($data, $allPopolo);
30 93
        }
31 279
        $this->lookupFromKey = [];
32 279
        foreach ($this->objectArr as $o) {
33 255
            $this->lookupFromKey[$o->keyForHash] = $o;
34 93
        }
35 279
    }
36
37
    /**
38
     * String representation of {@link PopoloCollection}
39
     *
40
     * @return string
41
     */
42
    public function __toString()
43
    {
44
        return '<'.get_class($this).'>';
45
    }
46
47
    /**
48
     * Getter for public attributes
49
     *
50
     * @param string $prop the attribute to get
51
     *
52
     * @return mixed
53
     */
54 126 View Code Duplication
    public function __get($prop)
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...
55
    {
56 126
        if (in_array($prop, $this->properties)) {
57 126
            $getter = 'get'.ucfirst($prop);
58 126
            return $this->$getter();
59
        }
60
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
61
    }
62
63
    /**
64
     * Get the first item in this collection
65
     *
66
     * @return mixed
67
     */
68 126
    private function getFirst()
69
    {
70 126
        return (count($this->objectArr) > 0) ? $this->objectArr[0] : null;
71
    }
72
73
    /**
74
     * Gets an array of matching items, according to the
75
     * array of filters provided. Filters are key-value pairs
76
     * that are ANDed together, i.e. returned items match all
77
     * the criteria
78
     *
79
     * @param string[] $filters key-value set of criteria
80
     *
81
     * @return object[]
82
     */
83 15
    public function filter($filters)
84
    {
85 15
        $filtered = [];
86 15
        foreach ($this->objectArr as $obj) {
87 12
            $success = true;
88 12
            foreach ($filters as $prop => $value) {
89 12
                if ($obj->$prop !== $value) {
90 9
                    $success = false;
91 11
                    break;
92
                }
93 4
            }
94 12
            if ($success) {
95 11
                $filtered[] = $obj;
96 3
            }
97 5
        }
98 15
        return $filtered;
99
    }
100
101
    /**
102
     * Gets a single matching item, according to the
103
     * array of filters provided. Filters are key-value pairs
104
     * that are ANDed together, i.e. returned items match all
105
     * the criteria. If more or less than one item matches,
106
     * an exception is thrown
107
     *
108
     * @param string[] $filters key-value set of criteria
109
     *
110
     * @return object
111
     */
112 9
    public function get($filters)
113
    {
114 9
        $matches = $this->filter($filters);
115 9
        $n = count($matches);
116 9
        if ($n == 0) {
117 3
            $msg = "No ".$this->objectClass." found matching ".json_encode($filters);
118 3
            throw new Exceptions\ObjectDoesNotExistException($msg);
119 6
        } elseif ($n > 1) {
120 3
            $msg = "Multiple ".$this->objectClass." objects ($n) found matching ".json_encode($filters);
121 3
            throw new Exceptions\MultipleObjectsReturnedException($msg);
122
        }
123 3
        return $matches[0];
124
    }
125
126
    /**
127
     * Count elements of an object
128
     *
129
     * Part of the Countable interface
130
     *
131
     * @return int
132
     */
133 87
    public function count()
134
    {
135 87
        return count($this->objectArr);
136
    }
137
138
    /**
139
     * Checks if current position is valid
140
     *
141
     * Part of the ArrayAccess interface
142
     *
143
     * @param mixed $offset array offset
144
     *
145
     * @return boolean
146
     */
147
    public function offsetExists($offset)
148
    {
149
        return array_key_exists($offset, $this->objectArr);
150
    }
151
152
    /**
153
     * Offset to retrieve
154
     *
155
     * Part of the ArrayAccess interface
156
     *
157
     * @param mixed $offset array offset
158
     *
159
     * @return mixed
160
     */
161 126
    public function offsetGet($offset)
162
    {
163 126
        return $this->objectArr[$offset];
164
    }
165
166
    /**
167
     * Assign a value to the specified offset
168
     *
169
     * Part of the ArrayAccess interface
170
     *
171
     * @param mixed $offset array offset
172
     * @param mixed $value value to assign
173
     */
174
    public function offsetSet($offset, $value)
175
    {
176
        $this->objectArr[$offset] = $value;
177
    }
178
179
    /**
180
     * Unset an offset
181
     *
182
     * Part of the ArrayAccess interface
183
     *
184
     * @param mixed $offset array offset
185
     */
186
    public function offsetUnset($offset)
187
    {
188
        unset($this->objectArr[$offset]);
189
    }
190
191
    /**
192
     * Return the current element
193
     *
194
     * Part of the Iterator interface
195
     *
196
     * @return mixed
197
     */
198 3
    public function current()
199
    {
200 3
        return $this->objectArr[$this->position];
201
    }
202
203
    /**
204
     * Return the key of the current element
205
     *
206
     * Part of the Iterator interface
207
     *
208
     * @return scalar
209
     */
210
    public function key()
211
    {
212
        return $this->position;
213
    }
214
215
    /**
216
     * Move forward to next element
217
     *
218
     * Part of the Iterator interface
219
     */
220 3
    public function next()
221
    {
222 3
        $this->position += 1;
223 3
    }
224
225
    /**
226
     * Rewind the Iterator to the first element
227
     *
228
     * Part of the Iterator interface
229
     */
230 3
    public function rewind()
231
    {
232 3
        $this->position = 0;
233 3
    }
234
235
    /**
236
     * Checks if current position is valid
237
     *
238
     * Part of the Iterator interface
239
     *
240
     * @return boolean
241
     */
242 3
    public function valid()
243
    {
244 3
        return array_key_exists($this->position, $this->objectArr);
245
    }
246
}
247