Created
November 3, 2020 20:05
-
-
Save rieschl/0e6e89b38dd181f901783d3ed31dd58e to your computer and use it in GitHub Desktop.
MVC Configuration classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Eventjet\Config; | |
interface ConfigInterface | |
{ | |
public function string(string $key): string; | |
public function int(string $key): int; | |
public function float(string $key): float; | |
public function bool(string $key): bool; | |
public function array(string $key): array; | |
public function has(string $key): bool; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Eventjet\Config; | |
use Eventjet\Config\Exception\UnknownKeyException; | |
use function explode; | |
final class NestedArrayConfig implements ConfigInterface | |
{ | |
public const DELIMITER = '/'; | |
/** @var array<string, mixed> */ | |
private array $config; | |
public function __construct(array $config) | |
{ | |
$this->config = $config; | |
} | |
public function string(string $key): string | |
{ | |
return $this->get($key); | |
} | |
public function int(string $key): int | |
{ | |
return $this->get($key); | |
} | |
public function float(string $key): float | |
{ | |
return $this->get($key); | |
} | |
public function bool(string $key): bool | |
{ | |
return $this->get($key); | |
} | |
public function array(string $key): array | |
{ | |
return $this->get($key); | |
} | |
public function has(string $key): bool | |
{ | |
return $this->softGet($key) !== null; | |
} | |
/** | |
* @return mixed | |
* @throws UnknownKeyException | |
*/ | |
private function get(string $key) | |
{ | |
$value = $this->softGet($key); | |
if ($value === null) { | |
throw UnknownKeyException::fromKey($key); | |
} | |
return $value; | |
} | |
/** | |
* @return string|int|float|bool|array<array-key, mixed>|null | |
*/ | |
private function softGet(string $key) | |
{ | |
$paths = explode(self::DELIMITER, $key); | |
$current = $this->config; | |
foreach ($paths as $index) { | |
if (!isset($current[$index])) { | |
return null; | |
} | |
$current = $current[$index]; | |
} | |
return $current; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace Eventjet\Config; | |
use Psr\Container\ContainerInterface; | |
class NestedArrayConfigFactory | |
{ | |
public function __invoke(ContainerInterface $container): NestedArrayConfig | |
{ | |
return new NestedArrayConfig($container->get('config')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment