1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/configuration |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Configuration\Common; |
11
|
|
|
|
12
|
|
|
use ArrayAccess; |
13
|
|
|
use ArrayIterator; |
14
|
|
|
use Countable; |
15
|
|
|
use IteratorAggregate; |
16
|
|
|
use Slick\Configuration\ConfigurationInterface; |
17
|
|
|
use Traversable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PriorityList |
21
|
|
|
* |
22
|
|
|
* @package Slick\Configuration\Common |
23
|
|
|
* |
24
|
|
|
* @implements ArrayAccess<int|string, ConfigurationInterface> |
25
|
|
|
* @implements IteratorAggregate<ConfigurationInterface> |
26
|
|
|
*/ |
27
|
|
|
class PriorityList implements ArrayAccess, Countable, IteratorAggregate |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array<int|string, array{element: ConfigurationInterface, priority: int}> |
31
|
|
|
*/ |
32
|
|
|
private array $data = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private int $lastPriority = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Inserts the provided element in the right order given the priority |
41
|
|
|
* |
42
|
|
|
* The lowest priority will be the first element in the list. |
43
|
|
|
* |
44
|
|
|
* @param ConfigurationInterface $element |
45
|
|
|
* @param int $priority |
46
|
|
|
* |
47
|
|
|
* @return PriorityList<ConfigurationInterface> |
48
|
|
|
*/ |
49
|
|
|
public function insert(ConfigurationInterface $element, int $priority = 0): PriorityList |
50
|
|
|
{ |
51
|
|
|
$data = []; |
52
|
|
|
$inserted = false; |
53
|
|
|
$priority = $priority === 0 ? $this->lastPriority : $priority; |
54
|
|
|
|
55
|
|
|
foreach ($this->data as $datum) { |
56
|
|
|
$inserted = $this->tryToInsert($element, $priority, $data, $datum); |
57
|
|
|
$data[] = ['element' => $datum['element'], 'priority' => $datum['priority']]; |
58
|
|
|
$this->lastPriority = $datum['priority']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$inserted) { |
62
|
|
|
$data[] = ['element' => $element, 'priority' => $priority]; |
63
|
|
|
$this->lastPriority = $priority; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->data = $data; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Tries to insert the provided element in the given data array |
72
|
|
|
* |
73
|
|
|
* @param ConfigurationInterface $element |
74
|
|
|
* @param integer $priority |
75
|
|
|
* @param array<string, array{element: ConfigurationInterface, priority: int}> $data |
76
|
|
|
* @param array{element: ConfigurationInterface, priority: int} $datum |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
private function tryToInsert(ConfigurationInterface $element, int $priority, array &$data, array $datum): bool |
81
|
|
|
{ |
82
|
|
|
$inserted = false; |
83
|
|
|
if ($datum['priority'] > $priority) { |
84
|
|
|
$data[] = ['element' => $element, 'priority' => $priority]; |
85
|
|
|
$inserted = true; |
86
|
|
|
} |
87
|
|
|
return $inserted; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the inserted elements in the order given by priority as an array |
92
|
|
|
* |
93
|
|
|
* @return array<int|string, ConfigurationInterface> |
94
|
|
|
*/ |
95
|
|
|
public function asArray(): array |
96
|
|
|
{ |
97
|
|
|
$data = []; |
98
|
|
|
foreach ($this->data as $datum) { |
99
|
|
|
$data[] = $datum['element']; |
100
|
|
|
} |
101
|
|
|
return $data; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
|
|
public function offsetExists(mixed $offset): bool |
108
|
|
|
{ |
109
|
|
|
return isset($this->data[$offset]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
* @return ConfigurationInterface |
115
|
|
|
*/ |
116
|
|
|
public function offsetGet(mixed $offset): ?ConfigurationInterface |
117
|
|
|
{ |
118
|
|
|
return $this->data[$offset] ? $this->data[$offset]['element'] : null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritdoc |
123
|
|
|
*/ |
124
|
|
|
public function offsetSet(mixed $offset, mixed $value): void |
125
|
|
|
{ |
126
|
|
|
if ($value instanceof ConfigurationInterface) { |
127
|
|
|
$this->insert($value); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
134
|
|
|
public function offsetUnset(mixed $offset): void |
135
|
|
|
{ |
136
|
|
|
unset($this->data[$offset]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @inheritdoc |
141
|
|
|
*/ |
142
|
|
|
public function count(): int |
143
|
|
|
{ |
144
|
|
|
return count($this->data); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @inheritdoc |
149
|
|
|
* @return Traversable<ConfigurationInterface> |
150
|
|
|
*/ |
151
|
|
|
public function getIterator(): Traversable |
152
|
|
|
{ |
153
|
|
|
return new ArrayIterator($this->asArray()); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|