Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
36 / 36 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| ArrayConfig | |
100.00% |
36 / 36 |
|
100.00% |
7 / 7 |
21 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| get | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
| set | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| remove | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of php-fast-forward/config. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2025-2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/config |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\Config; |
| 20 | |
| 21 | use Traversable; |
| 22 | use FastForward\Config\Exception\InvalidArgumentException; |
| 23 | use FastForward\Config\Helper\ConfigHelper; |
| 24 | |
| 25 | /** |
| 26 | * Class ArrayConfig. |
| 27 | * |
| 28 | * Provides a configuration management system with dot notation access. |
| 29 | * This class SHALL encapsulate configuration data using native PHP arrays. |
| 30 | * It MUST support nested keys and provide export, iteration, and dynamic update capabilities. |
| 31 | */ |
| 32 | class ArrayConfig implements ConfigInterface |
| 33 | { |
| 34 | use ArrayAccessConfigTrait; |
| 35 | |
| 36 | /** |
| 37 | * @var array internal configuration storage |
| 38 | */ |
| 39 | private array $data; |
| 40 | |
| 41 | /** |
| 42 | * Constructs the ArrayConfig instance. |
| 43 | * |
| 44 | * This constructor SHALL initialize the internal configuration store using normalized keys. |
| 45 | * |
| 46 | * @param array $config an optional initial configuration array |
| 47 | */ |
| 48 | public function __construct(array $config = []) |
| 49 | { |
| 50 | $this->data = ConfigHelper::normalize($config); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Determines whether the given configuration key exists. |
| 55 | * |
| 56 | * @param string $key the dot-notation configuration key to check |
| 57 | * |
| 58 | * @return bool TRUE if the key exists, FALSE otherwise |
| 59 | */ |
| 60 | public function has(string $key): bool |
| 61 | { |
| 62 | $value = $this->data; |
| 63 | |
| 64 | foreach (ConfigHelper::path($key) as $part) { |
| 65 | if (! \is_array($value) || ! \array_key_exists($part, $value)) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $value = $value[$part]; |
| 70 | } |
| 71 | |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Retrieves a value from the configuration. |
| 77 | * |
| 78 | * If the value is a nested associative array, a new instance of ArrayConfig SHALL be returned. |
| 79 | * If the key is missing or its value is null, the default SHALL be returned. |
| 80 | * |
| 81 | * @param string $key the configuration key to retrieve |
| 82 | * @param mixed $default the default value to return if the key does not exist |
| 83 | * |
| 84 | * @return mixed|self the configuration value, or a nested ArrayConfig instance |
| 85 | */ |
| 86 | public function get(string $key, mixed $default = null): mixed |
| 87 | { |
| 88 | $value = $this->data; |
| 89 | |
| 90 | foreach (ConfigHelper::path($key) as $part) { |
| 91 | if (! \is_array($value) || ! \array_key_exists($part, $value)) { |
| 92 | $value = null; |
| 93 | |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | $value = $value[$part]; |
| 98 | } |
| 99 | |
| 100 | $value ??= $default; |
| 101 | |
| 102 | if (ConfigHelper::isAssoc($value)) { |
| 103 | return new self($value); |
| 104 | } |
| 105 | |
| 106 | return $value; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Sets configuration values into the internal data store. |
| 111 | * |
| 112 | * If a value is provided, the key MUST be a string. If the input is an associative array |
| 113 | * or another ConfigInterface instance, it SHALL be normalized before insertion. |
| 114 | * |
| 115 | * @param array|ConfigInterface|string $key the configuration key(s) or configuration object |
| 116 | * @param mixed|null $value the value to assign to the specified key, if applicable |
| 117 | * |
| 118 | * @return void |
| 119 | * |
| 120 | * @throws InvalidArgumentException if the key is not a string when a value is provided |
| 121 | */ |
| 122 | public function set(array|ConfigInterface|string $key, mixed $value = null): void |
| 123 | { |
| 124 | if (null !== $value || \is_string($key)) { |
| 125 | if (! \is_string($key)) { |
| 126 | throw InvalidArgumentException::forNonStringKeyWithValue(); |
| 127 | } |
| 128 | |
| 129 | $key = [ |
| 130 | $key => $value, |
| 131 | ]; |
| 132 | } |
| 133 | |
| 134 | if ($key instanceof ConfigInterface) { |
| 135 | $key = $key->toArray(); |
| 136 | } |
| 137 | |
| 138 | $this->data = array_replace_recursive($this->data, ConfigHelper::normalize($key)); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Removes a configuration key and its associated value. |
| 143 | * |
| 144 | * If the key does not exist, this method SHALL do nothing. |
| 145 | * |
| 146 | * @param string $key the configuration key to remove |
| 147 | * |
| 148 | * @return void |
| 149 | */ |
| 150 | public function remove(string $key): void |
| 151 | { |
| 152 | $parts = ConfigHelper::path($key); |
| 153 | $last = array_pop($parts); |
| 154 | $current = &$this->data; |
| 155 | |
| 156 | foreach ($parts as $part) { |
| 157 | if (! isset($current[$part]) || ! \is_array($current[$part])) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | $current = &$current[$part]; |
| 162 | } |
| 163 | |
| 164 | unset($current[$last]); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Retrieves a traversable set of flattened configuration data. |
| 169 | * |
| 170 | * This method SHALL return an iterator where each key represents |
| 171 | * the nested path in dot notation, and each value is the corresponding value. |
| 172 | * For example ['database' => ['host' => 'localhost']] becomes ['database.host' => 'localhost']. |
| 173 | * |
| 174 | * @return Traversable<string, mixed> an iterator of flattened key-value pairs |
| 175 | */ |
| 176 | public function getIterator(): Traversable |
| 177 | { |
| 178 | return ConfigHelper::flatten($this->data); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Converts the entire configuration to an associative array. |
| 183 | * |
| 184 | * This method MUST export the configuration in its current state. |
| 185 | * |
| 186 | * @return array the exported configuration array |
| 187 | */ |
| 188 | public function toArray(): array |
| 189 | { |
| 190 | return $this->data; |
| 191 | } |
| 192 | } |