Completed
Push — master ( ef04ad...a21927 )
by Alexpts
02:06
created

Collection::getFlatItems()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types = 1);
3
namespace PTS\Tools;
4
5
class Collection implements CollectionInterface
6
{
7
    /** @var array */
8
    protected $items = [];
9
10
    /**
11
     * @param string $name
12
     * @param mixed $item
13
     * @param int $priority
14
     *
15
     * @return $this
16
     *
17
     * @throws DuplicateKeyException
18
     */
19 8
    public function addItem(string $name, $item, int $priority = 50)
20
    {
21 8
        if ($this->has($name)) {
22 1
            throw new DuplicateKeyException('Item with name '.$name.' already defined');
23
        }
24
25 8
        $this->items[$priority][$name] = $item;
26 8
        return $this;
27
    }
28
29
    /**
30
     * @param string $name
31
     * @param null|int $priority
32
     *
33
     * @return $this
34
     */
35 2
    public function removeItem(string $name, int $priority = null)
36
    {
37 2
        if ($priority !== null) {
38 1
            if (isset($this->items[$priority][$name])) {
39 1
                unset($this->items[$priority][$name]);
40
            }
41
42 1
            return $this;
43
        }
44
45 1
        return $this->removeItemWithoutPriority($name);
46
    }
47
48
    /**
49
     * @param string $name
50
     *
51
     * @return $this
52
     */
53 1
    protected function removeItemWithoutPriority(string $name)
54
    {
55 1
        foreach ($this->items as $priority => $items) {
56 1
            if (isset($items[$name])) {
57 1
                unset($this->items[$priority][$name]);
58
            }
59
        }
60
61 1
        return $this;
62
    }
63
64 8
    public function has(string $name) : bool
65
    {
66 8
        foreach ($this->items as $items) {
67 3
            if (isset($items[$name])) {
68 3
                return true;
69
            }
70
        }
71
72 8
        return false;
73
    }
74
75
    /**
76
     * @param bool $sort
77
     * @return array[]
78
     */
79 7
    public function getItems(bool $sort = true) : array
80
    {
81 7
        $items = $this->items;
82
83 7
        if ($sort) {
84 7
            krsort($items, SORT_NUMERIC);
85
        }
86
87 7
        return $items;
88
    }
89
90 1
    public function getFlatItems(bool $sort = true) : array
91
    {
92 1
        $flatItems = [];
93
94
        /** @var array $items */
95 1
        foreach ($this->getItems($sort) as $items) {
96 1
            foreach ($items as $item) {
97 1
                $flatItems[] = $item;
98
            }
99
        }
100
101 1
        return $flatItems;
102
    }
103
104
    /**
105
     * @return $this
106
     */
107 1
    public function flush()
108
    {
109 1
        $this->items = [];
110 1
        return $this;
111
    }
112
}
113