diff --git a/composer.json b/composer.json index a67c661..2366cae 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "php": "^8.3", "psr/container": "^2.0", "symfony/yaml": "^7.3", - "vlucas/phpdotenv": "^5.6" + "vlucas/phpdotenv": "^5.6", + "dflydev/dot-access-data": "^3.0" }, "require-dev": { "phpstan/phpstan": "^2.1", diff --git a/src/Config.php b/src/Config.php index 9baeb51..832d703 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,18 +3,21 @@ namespace Borsch\Config; use Borsch\Config\Exception\NotFoundException; -use Psr\Container\{ContainerExceptionInterface, ContainerInterface, NotFoundExceptionInterface}; +use Psr\Container\ContainerInterface; +use Dflydev\DotAccessData\Data; class Config implements ContainerInterface { + private Data $config; + /** * @param array $config */ - public function __construct( - /** @var array */ - private array $config = [] - ) {} + public function __construct(array $config = []) + { + $this->config = new Data($config); + } public function get(string $id): mixed { @@ -22,33 +25,23 @@ public function get(string $id): mixed throw NotFoundException::forEntry($id); } - return $this->config[$id]; + return $this->config->get($id); } public function has(string $id): bool { - return isset($this->config[$id]); + return $this->config->has($id); } - /** - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - */ public function getOrDefault(string $id, mixed $default = null): mixed { - if ($this->has($id)) { - return $this->get($id); - } - - return $default; + return $this->config->get($id, $default); } public function merge(Config $config): Config { if ($config !== $this && !empty($config->config)) { - foreach ($config->config as $key => $value) { - $this->config[$key] = $value; - } + $this->config->importData($config->config); } return $this;