|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @author Mohammed Moussaoui |
| 5 | + * @license MIT license. For more license information, see the LICENSE file in the root directory. |
| 6 | + * @link https://github.com/DevNet-Framework |
| 7 | + */ |
| 8 | + |
| 9 | +namespace DevNet\Common\Configuration; |
| 10 | + |
| 11 | +use DevNet\System\PropertyTrait; |
| 12 | + |
| 13 | +class ConfigurationRoot implements IConfiguration |
| 14 | +{ |
| 15 | + use PropertyTrait; |
| 16 | + |
| 17 | + private array $settings = []; |
| 18 | + |
| 19 | + public function __construct(array $settings = []) |
| 20 | + { |
| 21 | + $this->settings = $settings; |
| 22 | + } |
| 23 | + |
| 24 | + public function get_Settings(): array |
| 25 | + { |
| 26 | + return $this->settings; |
| 27 | + } |
| 28 | + |
| 29 | + public function getValue(string $key) |
| 30 | + { |
| 31 | + $keys = explode(":", $key); |
| 32 | + $value = $this->settings; |
| 33 | + |
| 34 | + foreach ($keys as $key) { |
| 35 | + $value = $value[$key] ?? null; |
| 36 | + |
| 37 | + if ($value == null) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (is_array($value)) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + return $value; |
| 47 | + } |
| 48 | + |
| 49 | + public function getSection(string $key): IConfiguration |
| 50 | + { |
| 51 | + return new ConfigurationSection($this, $key); |
| 52 | + } |
| 53 | + |
| 54 | + public function getChildren(string $key = ''): array |
| 55 | + { |
| 56 | + $path = $key; |
| 57 | + $keys = explode(":", $key); |
| 58 | + |
| 59 | + $settings = $this->settings; |
| 60 | + |
| 61 | + foreach ($keys as $key) { |
| 62 | + $settings = $settings[$key] ?? null; |
| 63 | + |
| 64 | + if (!is_array($settings)) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $children = []; |
| 70 | + $keys = array_keys($settings); |
| 71 | + |
| 72 | + foreach ($keys as $key) { |
| 73 | + if ($path != '') { |
| 74 | + $key = $path . ":" . $key; |
| 75 | + } |
| 76 | + |
| 77 | + $children[] = new ConfigurationSection($this, $key); |
| 78 | + } |
| 79 | + |
| 80 | + return $children; |
| 81 | + } |
| 82 | +} |
0 commit comments