Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 12 additions & 19 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,45 @@
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<string, mixed> $config
*/
public function __construct(
/** @var array<string, mixed> */
private array $config = []
) {}
public function __construct(array $config = [])
{
$this->config = new Data($config);
}

public function get(string $id): mixed
{
if (!$this->has($id)) {
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;
Expand Down