Completed
Push — master ( fdb8ab...a48276 )
by Guillermo
14s
created

EntityCollection::keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
nc 1
cc 1
eloc 2
nop 0
crap 1
1
<?php
2
3
namespace Atrapalo\PHPTools\Collection;
4
5
/**
6
 * Class EntityCollection
7
 * @package Atrapalo\PHPTools\Collection
8
 *
9
 * @author Guillermo González <[email protected]>
10
 */
11
abstract class EntityCollection implements \Countable, \Iterator
12
{
13
    /** @var array */
14
    private $entities = array();
15
    /** @var bool */
16
    protected $allowEntitiesChildren;
17
18
    /**
19
     * @return string
20
     */
21
    abstract public static function entityClass(): string;
22
23
    /**
24
     * Collection constructor.
25
     * @param array $entities
26
     * @param bool  $allowEntitiesChildren
27
     */
28 66
    public function __construct(array $entities = array(), bool $allowEntitiesChildren = false)
29
    {
30 66
        $this->allowEntitiesChildren = $allowEntitiesChildren;
31
32 66
        foreach ($entities as $entity) {
33 52
            $this->add($entity);
34
        }
35 62
    }
36
37
    /**
38
     * @param $entity
39
     * @throws \Exception
40
     */
41 103
    public function add($entity)
42
    {
43 103
        if (!$this->isValid($entity)) {
44 10
            throw static::customInvalidEntityException();
45
        }
46
47 94
        $this->entities[] = $entity;
48 94
    }
49
50
    /**
51
     * @param $key
52
     * @param $entity
53
     * @throws \Exception
54
     */
55 5
    protected function set($key, $entity)
56
    {
57 5
        if (!$this->isValid($entity)) {
58 1
            throw static::customInvalidEntityException();
59
        }
60
61 4
        $this->entities[$key] = $entity;
62 4
    }
63
64
    /**
65
     * @param $entity
66
     * @return bool
67
     */
68 109
    public function isValid($entity): bool
69
    {
70 109
        if (!is_object($entity)) return false;
71
72 105
        $entityClass = $this->allowEntitiesChildren ? get_parent_class($entity) : get_class($entity);
73 105
        if ($entityClass === static::entityClass()){
74 99
            return true;
75
        }
76
77 7
        return false;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83 7
    public function isEmpty(): bool
84
    {
85 7
        return $this->count() === 0;
86
    }
87
88
    /**
89
     * @return array
90
     */
91 11
    public function keys(): array
92
    {
93 11
        return array_keys($this->entities);
94
    }
95
96
    /**
97
     * @return array
98
     */
99 7
    public function values(): array
100
    {
101 7
        return array_values($this->entities);
102
    }
103
104
    /**
105
     * @return mixed
106
     */
107 7
    public function first()
108
    {
109 7
        return reset($this->entities);
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115 7
    public function last()
116
    {
117 7
        return end($this->entities);
118
    }
119
120
    /**
121
     * @return array
122
     */
123 19
    public function toArray(): array
124
    {
125 19
        return $this->entities;
126
    }
127
128
    /**
129
     * @param int  $offset
130
     * @param null $length
131
     * @return static
132
     */
133 16
    public function slice(int $offset, $length = null)
134
    {
135 16
        $entities = array_slice($this->entities, $offset, $length, true);
136
137 16
        return new static($entities, $this->allowEntitiesChildren);
138
    }
139
140
    /**
141
     * @return mixed
142
     */
143 21
    public function current()
144
    {
145 21
        return current($this->entities);
146
    }
147
148
    /**
149
     * @return mixed
150
     */
151 27
    public function next()
152
    {
153 27
        return next($this->entities);
154
    }
155
156
    /**
157
     * @return mixed
158
     */
159 19
    public function key()
160
    {
161 19
        return key($this->entities);
162
    }
163
164
    /**
165
     * @return bool
166
     */
167 8
    public function valid(): bool
168
    {
169 8
        return key($this->entities) !== null;
170
    }
171
172 8
    public function rewind()
173
    {
174 8
        reset($this->entities);
175 8
    }
176
177
    /**
178
     * @return int
179
     */
180 39
    public function count(): int
181
    {
182 39
        return count($this->entities);
183
    }
184
185
    /**
186
     * @return \Exception
187
     */
188 4
    public static function customInvalidEntityException(): \Exception
189
    {
190 4
        return new \UnexpectedValueException('Element is not valid instance of '.static::entityClass());
191
    }
192
}
193