|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Settings Manager |
|
5
|
|
|
* |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT |
|
7
|
|
|
* @link https://github.com/caseyamcl/settings-manager |
|
8
|
|
|
* @package caseyamcl/settings-manager |
|
9
|
|
|
* @author Casey McLaughlin <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
* |
|
14
|
|
|
* ------------------------------------------------------------------ |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace SettingsManager\Registry; |
|
20
|
|
|
|
|
21
|
|
|
use ArrayIterator; |
|
22
|
|
|
use Countable; |
|
23
|
|
|
use IteratorAggregate; |
|
24
|
|
|
use SettingsManager\Contract\SettingDefinition; |
|
25
|
|
|
use SettingsManager\Exception\UndefinedSettingException; |
|
26
|
|
|
use SettingsManager\Exception\SettingNameCollisionException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class SettingDefinitionRegistry |
|
30
|
|
|
*/ |
|
31
|
|
|
class SettingDefinitionRegistry implements IteratorAggregate, Countable |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var ArrayIterator|SettingDefinition[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $items; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* SettingDefinitionRegistry constructor. |
|
40
|
|
|
* @param iterable|SettingDefinition[]|null $items |
|
41
|
|
|
*/ |
|
42
|
117 |
|
public function __construct(?iterable $items = []) |
|
43
|
|
|
{ |
|
44
|
117 |
|
$this->items = new ArrayIterator([]); |
|
45
|
|
|
|
|
46
|
117 |
|
foreach ($items as $item) { |
|
47
|
6 |
|
$this->add($item); |
|
48
|
|
|
} |
|
49
|
117 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
69 |
|
final public function has(string $name): bool |
|
56
|
|
|
{ |
|
57
|
69 |
|
return $this->items->offsetExists($name); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $name |
|
62
|
|
|
* @return SettingDefinition |
|
63
|
|
|
*/ |
|
64
|
66 |
|
final public function get(string $name): SettingDefinition |
|
65
|
|
|
{ |
|
66
|
66 |
|
if ($this->has($name)) { |
|
67
|
63 |
|
return $this->items[$name]; |
|
68
|
|
|
} else { |
|
69
|
3 |
|
throw UndefinedSettingException::forSetting($name); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Add a setting definition to the registry |
|
75
|
|
|
* |
|
76
|
|
|
* @param SettingDefinition $setting |
|
77
|
|
|
* @return SettingDefinitionRegistry |
|
78
|
|
|
*/ |
|
79
|
114 |
|
final public function add(SettingDefinition $setting): self |
|
80
|
|
|
{ |
|
81
|
114 |
|
if ($this->items->offsetExists($setting->getName()) && $setting !== $this->items[$setting->getName()]) { |
|
82
|
12 |
|
throw SettingNameCollisionException::fromName($setting->getName()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
114 |
|
$this->items[$setting->getName()] = $setting; |
|
86
|
114 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return ArrayIterator|SettingDefinition[] |
|
91
|
|
|
*/ |
|
92
|
57 |
|
public function getIterator(): ArrayIterator |
|
93
|
|
|
{ |
|
94
|
57 |
|
return $this->items; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return int |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function count(): int |
|
101
|
|
|
{ |
|
102
|
6 |
|
return $this->items->count(); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|