|
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 $itemPriority => $items) { |
|
56
|
1 |
|
if (isset($items[$name])) { |
|
57
|
1 |
|
unset($this->items[$itemPriority][$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
|
7 |
|
public function getItems() : array |
|
76
|
|
|
{ |
|
77
|
7 |
|
return $this->items; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
public function getSortedItems() : array |
|
81
|
|
|
{ |
|
82
|
2 |
|
$items = $this->getItems(); |
|
83
|
2 |
|
krsort($items, SORT_NUMERIC); |
|
84
|
|
|
|
|
85
|
2 |
|
return $items; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function getFlatSortedItems() : array |
|
89
|
|
|
{ |
|
90
|
1 |
|
$sortedItems = $this->getSortedItems(); |
|
91
|
1 |
|
$flatItems = []; |
|
92
|
|
|
|
|
93
|
|
|
/** @var array $items */ |
|
94
|
1 |
|
foreach ($sortedItems as $items) { |
|
95
|
1 |
|
foreach ($items as $item) { |
|
96
|
1 |
|
$flatItems[] = $item; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
return $flatItems; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return $this |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function flush() |
|
107
|
|
|
{ |
|
108
|
1 |
|
$this->items = []; |
|
109
|
1 |
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|