Elements   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 4 2
A getById() 0 8 3
A addMulti() 0 4 2
A search() 0 3 1
A exists() 0 3 1
A getIterator() 0 3 1
A count() 0 3 1
A remove() 0 5 2
1
<?php
2
3
namespace EaselDrawing;
4
5
class Elements implements \Countable, \IteratorAggregate
6
{
7
    /** @var ElementInterface[] elements */
8
    protected $elements = [];
9
10
    public function add(ElementInterface $element)
11
    {
12
        if (! $this->exists($element)) {
13
            $this->elements[] = $element;
14
        }
15
    }
16
17
    /**
18
     * @param ElementInterface[] $elements
19
     */
20
    public function addMulti(array $elements)
21
    {
22
        foreach ($elements as $element) {
23
            $this->add($element);
24
        }
25
    }
26
27
    /**
28
     * @param ElementInterface $element
29
     * @return false|int
30
     */
31
    public function search(ElementInterface $element)
32
    {
33
        return array_search($element, $this->elements, true);
34
    }
35
36
    public function remove(ElementInterface $element)
37
    {
38
        $id = $this->search($element);
39
        if (false !== $id) {
40
            unset($this->elements[$id]);
41
        }
42
    }
43
44
    public function exists(ElementInterface $element)
45
    {
46
        return (false !== $this->search($element));
47
    }
48
49
    public function getById(string $id): ElementInterface
50
    {
51
        foreach ($this->elements as $element) {
52
            if (0 === strcmp($id, $element->getId())) {
53
                return $element;
54
            }
55
        }
56
        throw new \InvalidArgumentException("The element $id does not exists");
57
    }
58
59
    public function count(): int
60
    {
61
        return count($this->elements);
62
    }
63
64
    public function getIterator()
65
    {
66
        return new \ArrayIterator($this->elements);
67
    }
68
}
69