Completed
Push — master ( 3dee16...2bf36e )
by Andy
02:57
created

PopoloCollection::filter()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 14
cts 14
cp 1
rs 8.8571
cc 5
eloc 11
nc 7
nop 1
crap 5
1
<?php
2
3
namespace EveryPolitician\EveryPoliticianPopolo\Collections;
4
5
use \EveryPolitician\EveryPoliticianPopolo\Exceptions;
6
7
class PopoloCollection implements \Countable, \ArrayAccess, \Iterator
8
{
9
    protected $properties = [
10
        'first',
11
    ];
12
    public $lookupFromKey;
13
    private $objectClass;
14
    private $objectArr;
15
    private $position;
16
17
    /**
18
     *
19
     */
20 279
    public function __construct($dataArr, $objectClass, $allPopolo)
21
    {
22 279
        $this->position = 0;
23 279
        $this->objectClass = $objectClass;
24 279
        $this->objectArr = [];
25 279
        foreach ($dataArr as $data) {
26 255
            $this->objectArr[] = new $objectClass($data, $allPopolo);
27 186
        }
28 279
        $this->lookupFromKey = [];
29 279
        foreach ($this->objectArr as $o) {
30 255
            $this->lookupFromKey[$o->keyForHash] = $o;
31 186
        }
32 279
    }
33
34
    public function __toString()
35
    {
36
        return '<'.get_class($this).'>';
37
    }
38
39 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...
40
    {
41 126
        if (in_array($prop, $this->properties)) {
42 126
            $getter = 'get' . ucfirst($prop);
43 126
            return $this->$getter();
44
        }
45
        trigger_error('Undefined property: '.__CLASS__.'::$'.$prop, E_USER_ERROR);
46
    }
47
48 126
    private function getFirst()
49
    {
50 126
        return (count($this->objectArr) > 0) ? $this->objectArr[0] : null;
51
    }
52
53 15
    public function filter($filters)
54
    {
55 15
        $filtered = [];
56 15
        foreach ($this->objectArr as $obj) {
57 12
            $success = true;
58 12
            foreach ($filters as $prop => $value) {
59 12
                if ($obj->$prop !== $value) {
60 9
                    $success = false;
61 10
                    break;
62
                }
63 8
            }
64 12
            if ($success) {
65 10
                $filtered[] = $obj;
66 6
            }
67 10
        }
68 15
        return $filtered;
69
    }
70
71 9
    public function get($filters)
72
    {
73 9
        $matches = $this->filter($filters);
74 9
        $n = count($matches);
75 9
        if ($n == 0) {
76 3
            $msg = "No " . $this->objectClass . " found matching " . json_encode($filters);
77 3
            throw new Exceptions\ObjectDoesNotExistException($msg);
78 6
        } elseif ($n > 1) {
79 3
            $msg = "Multiple " . $this->objectClass . " objects ($n) found matching " . json_encode($filters);
80 3
            throw new Exceptions\MultipleObjectsReturnedException($msg);
81
        }
82 3
        return $matches[0];
83
    }
84
85
    // Countable interface
86 87
    public function count()
87
    {
88 87
        return count($this->objectArr);
89
    }
90
91
    // ArrayAccess interface
92
    public function offsetExists($offset)
93
    {
94
        return array_key_exists($offset, $this->objectArr);
95
    }
96
97
    // ArrayAccess interface
98 126
    public function offsetGet($offset)
99
    {
100 126
        return $this->objectArr[$offset];
101
    }
102
103
    // ArrayAccess interface
104
    public function offsetSet($offset, $value)
105
    {
106
        $this->objectArr[$offset] = $value;
107
    }
108
109
    // ArrayAccess interface
110
    public function offsetUnset($offset)
111
    {
112
        unset($this->objectArr[$offset]);
113
    }
114
115
    // Iterator interface
116 3
    public function current()
117
    {
118 3
        return $this->objectArr[$this->position];
119
    }
120
121
    // Iterator interface
122
    public function key()
123
    {
124
        return $this->position;
125
    }
126
127
    // Iterator interface
128 3
    public function next()
129
    {
130 3
        $this->position += 1;
131 3
    }
132
133
    // Iterator interface
134 3
    public function rewind()
135
    {
136 3
        $this->position = 0;
137 3
    }
138
139
    // Iterator interface
140 3
    public function valid()
141
    {
142 3
        return array_key_exists($this->position, $this->objectArr);
143
    }
144
}
145