Completed
Push — master ( f4ddf2...7a5f37 )
by Pablo
04:56
created

ObjectCollection::setClassName()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpValueObjects\Collection;
6
7
use PhpValueObjects\Collection\Exception\InvalidCollectionObjectException;
8
9
abstract class ObjectCollection
10
{
11
    protected $objects;
12
13 54
    public function __construct(array $objects)
14
    {
15 54
        foreach ($objects as $object) {
16 54
            if (false === is_a($object, $this->setClassName())) {
17 54
                throw new InvalidCollectionObjectException($object, $this->setClassName());
18
            }
19
        }
20 54
        $this->objects = $objects;
21 54
    }
22
23
    abstract protected function setClassName(): string;
24
25 12
    public function getCollection(): array
26
    {
27 12
        return $this->objects;
28
    }
29
30 12
    public function first()
31
    {
32 12
        return reset($this->objects);
33
    }
34
35 9
    public function last()
36
    {
37 9
        return end($this->objects);
38
    }
39
40 3
    public function key()
41
    {
42 3
        return key($this->getCollection());
43
    }
44
45 6
    public function next()
46
    {
47 6
        return next($this->objects);
48
    }
49
50 3
    public function current()
51
    {
52 3
        return current($this->objects);
53
    }
54
55 3
    public function remove($key)
56
    {
57 3
        if (!isset($this->objects[$key]) && !array_key_exists($key, $this->objects)) {
58
            return null;
59
        }
60
61 3
        $removed = $this->objects[$key];
62 3
        unset($this->objects[$key]);
63
64 3
        return $removed;
65
    }
66
67 3
    public function removeElement($element): bool
68
    {
69 3
        $key = array_search($element, $this->objects, true);
70
71 3
        if ($key === false) {
72
            return false;
73
        }
74
75 3
        unset($this->objects[$key]);
76
77 3
        return true;
78
    }
79
80 3
    public function contains($element): bool
81
    {
82 3
        return in_array($element, $this->objects, true);
83
    }
84
85 6
    public function get($key)
86
    {
87 6
        return $this->objects[$key] ?? null;
88
    }
89
90 3
    public function getKeys()
91
    {
92 3
        return array_keys($this->objects);
93
    }
94
95 3
    public function getValues()
96
    {
97 3
        return array_values($this->objects);
98
    }
99
100 3
    public function count(): int
101
    {
102 3
        return count($this->objects);
103
    }
104
105 3
    public function set($key, $value): void
106
    {
107 3
        $this->objects[$key] = $value;
108 3
    }
109
110 3
    public function add($element): void
111
    {
112 3
        $this->objects[] = $element;
113 3
    }
114
115 3
    public function isEmpty(): bool
116
    {
117 3
        return empty($this->objects);
118
    }
119
120 3
    public function clear(): void
121
    {
122 3
        $this->objects = [];
123 3
    }
124
125
}
126