Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.78% |
44 / 45 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ConfigHelper | |
97.78% |
44 / 45 |
|
83.33% |
5 / 6 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isAssoc | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| path | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| merge | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
7 | |||
| normalize | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
13 | |||
| flatten | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 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\Helper; |
| 20 | |
| 21 | use Traversable; |
| 22 | use FastForward\Config\Exception\InvalidArgumentException; |
| 23 | |
| 24 | /** |
| 25 | * Class ConfigHelper. |
| 26 | * |
| 27 | * Provides a set of static helper methods for manipulating configuration arrays, |
| 28 | * particularly handling associative arrays with dot notation and nested structures. |
| 29 | * This class SHALL NOT be instantiated and MUST be used statically. |
| 30 | */ |
| 31 | class ConfigHelper |
| 32 | { |
| 33 | private const array DELIMITERS = ['.', '/']; |
| 34 | |
| 35 | /** |
| 36 | * This constructor is private to prevent instantiation of the class. |
| 37 | * |
| 38 | * The class MUST be used in a static context only. |
| 39 | */ |
| 40 | private function __construct() |
| 41 | { |
| 42 | // Prevent instantiation |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Determines if the provided value is an associative array. |
| 47 | * |
| 48 | * This method SHALL check whether the given array uses string keys, |
| 49 | * distinguishing it from indexed arrays. |
| 50 | * |
| 51 | * @param mixed $value the value to check |
| 52 | * |
| 53 | * @return bool true if the array is associative; false otherwise |
| 54 | */ |
| 55 | public static function isAssoc(mixed $value): bool |
| 56 | { |
| 57 | if (! \is_array($value)) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | foreach (array_keys($value) as $key) { |
| 62 | if (\is_int($key)) { |
| 63 | return false; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Splits a non-empty configuration key, preserving empty path segments. |
| 72 | * |
| 73 | * @param string $key the dot- or slash-separated configuration key |
| 74 | * |
| 75 | * @return non-empty-list<string> the path segments |
| 76 | * |
| 77 | * @throws InvalidArgumentException if the path is empty |
| 78 | * |
| 79 | * @internal |
| 80 | */ |
| 81 | public static function path(string $key): array |
| 82 | { |
| 83 | if ('' === $key) { |
| 84 | throw InvalidArgumentException::forEmptyPath(); |
| 85 | } |
| 86 | |
| 87 | return explode('.', str_replace(self::DELIMITERS, '.', $key)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Merges maps recursively and concatenates sequential lists, retaining duplicates. |
| 92 | * Later scalar values SHALL replace earlier values. Sparse and mixed arrays SHALL |
| 93 | * retain their keys and merge matching indexes recursively. |
| 94 | * |
| 95 | * @param array $first the existing configuration |
| 96 | * @param array $second the configuration to merge |
| 97 | * |
| 98 | * @return array the merged configuration |
| 99 | * |
| 100 | * @internal |
| 101 | */ |
| 102 | public static function merge(array $first, array $second): array |
| 103 | { |
| 104 | if (array_is_list($first) && array_is_list($second)) { |
| 105 | return array_merge($first, $second); |
| 106 | } |
| 107 | |
| 108 | foreach ($second as $key => $value) { |
| 109 | $first[$key] = isset($first[$key]) && \is_array($first[$key]) && \is_array($value) |
| 110 | ? self::merge($first[$key], $value) |
| 111 | : $value; |
| 112 | } |
| 113 | |
| 114 | return $first; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Normalizes a configuration array using dot notation delimiters. |
| 119 | * |
| 120 | * This method SHALL recursively convert keys containing delimiters into nested arrays. |
| 121 | * For example, a key like "database.host" SHALL be transformed into |
| 122 | * ['database' => ['host' => 'value']]. |
| 123 | * |
| 124 | * @param array $config the configuration array to normalize |
| 125 | * |
| 126 | * @return array the normalized configuration array |
| 127 | */ |
| 128 | public static function normalize(array $config): array |
| 129 | { |
| 130 | if (! self::isAssoc($config)) { |
| 131 | return $config; |
| 132 | } |
| 133 | |
| 134 | $normalized = []; |
| 135 | |
| 136 | $delimiterChars = implode('', self::DELIMITERS); |
| 137 | |
| 138 | foreach ($config as $key => $value) { |
| 139 | if (self::isAssoc($value)) { |
| 140 | $value = self::normalize($value); |
| 141 | } |
| 142 | |
| 143 | if (! \is_string($key) || false === strpbrk($key, $delimiterChars)) { |
| 144 | $normalized[$key] = $value; |
| 145 | |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | $parts = self::path($key); |
| 150 | $current = &$normalized; |
| 151 | $lastIndex = \count($parts) - 1; |
| 152 | |
| 153 | foreach ($parts as $index => $part) { |
| 154 | if ($index !== $lastIndex) { |
| 155 | if (! isset($current[$part]) || ! \is_array($current[$part])) { |
| 156 | $current[$part] = []; |
| 157 | } |
| 158 | |
| 159 | $current = &$current[$part]; |
| 160 | |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | if (isset($current[$part]) && \is_array($current[$part]) && \is_array($value)) { |
| 165 | $current[$part] = self::merge($current[$part], $value); |
| 166 | |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | $current[$part] = $value; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return $normalized; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Flattens a nested configuration array into a dot-notated traversable set. |
| 179 | * |
| 180 | * This method SHALL recursively iterate through the nested array structure |
| 181 | * and convert it into a flat representation where keys reflect the nested path. |
| 182 | * A value like ['database' => ['host' => 'localhost']] SHALL be transformed into |
| 183 | * ['database.host' => 'localhost']. |
| 184 | * |
| 185 | * @param array $config the configuration array to flatten |
| 186 | * @param string $rootKey (Optional) The root key prefix for recursive calls |
| 187 | * |
| 188 | * @return Traversable<string, mixed> a traversable list of flattened key-value pairs |
| 189 | */ |
| 190 | public static function flatten(array $config, string $rootKey = ''): Traversable |
| 191 | { |
| 192 | foreach ($config as $key => $value) { |
| 193 | if (\is_array($value)) { |
| 194 | yield from self::flatten($value, $rootKey . $key . '.'); |
| 195 | } else { |
| 196 | yield $rootKey . $key => $value; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |