diff --git a/composer.json b/composer.json index 85d933b..4523ba0 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,8 @@ "homepage": "https://github.com/selective-php/config", "require": { "php": "8.1.* || 8.2.* || 8.3.* || 8.4.*", - "cakephp/chronos": "^2 || ^3" + "cakephp/chronos": "^2 || ^3", + "selective/array-reader": "^2" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3", diff --git a/src/Configuration.php b/src/Configuration.php index acba5d7..d5fb9fa 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -4,6 +4,7 @@ use Cake\Chronos\Chronos; use InvalidArgumentException; +use Selective\ArrayReader\ArrayReader; /** * Configuration. @@ -11,9 +12,9 @@ final class Configuration { /** - * @var array The data + * @var ArrayReader */ - private $data; + private ArrayReader $arrayReader; /** * Constructor. @@ -22,7 +23,7 @@ final class Configuration */ public function __construct(array $data = []) { - $this->data = $data; + $this->arrayReader = new ArrayReader($data); } /** @@ -37,13 +38,7 @@ public function __construct(array $data = []) */ public function getInt(string $key, ?int $default = null): int { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); - } - - return (int)$value; + return $this->arrayReader->getInt($key, $default); } /** @@ -54,15 +49,9 @@ public function getInt(string $key, ?int $default = null): int * * @return int|null The value */ - public function findInt(string $key, ?int $default = null) + public function findInt(string $key, ?int $default = null): ?int { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - return null; - } - - return (int)$value; + return $this->arrayReader->findInt($key, $default); } /** @@ -77,13 +66,7 @@ public function findInt(string $key, ?int $default = null) */ public function getString(string $key, ?string $default = null): string { - $value = $this->find($key, $default); - - if ($value === null) { - throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); - } - - return (string)$value; + return $this->arrayReader->getString($key, $default); } /** @@ -94,15 +77,9 @@ public function getString(string $key, ?string $default = null): string * * @return string|null The value */ - public function findString(string $key, ?string $default = null) + public function findString(string $key, ?string $default = null): ?string { - $value = $this->find($key, $default); - - if ($value === null) { - return null; - } - - return (string)$value; + return $this->arrayReader->findString($key, $default); } /** @@ -117,13 +94,7 @@ public function findString(string $key, ?string $default = null) */ public function getArray(string $key, ?array $default = null): array { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); - } - - return (array)$value; + return $this->arrayReader->getArray($key, $default); } /** @@ -134,15 +105,9 @@ public function getArray(string $key, ?array $default = null): array * * @return array|null The value */ - public function findArray(string $key, ?array $default = null) + public function findArray(string $key, ?array $default = null): ?array { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - return null; - } - - return (array)$value; + return $this->arrayReader->findArray($key, $default); } /** @@ -157,13 +122,7 @@ public function findArray(string $key, ?array $default = null) */ public function getFloat(string $key, ?float $default = null): float { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); - } - - return (float)$value; + return $this->arrayReader->getFloat($key, $default); } /** @@ -174,15 +133,9 @@ public function getFloat(string $key, ?float $default = null): float * * @return float|null The value */ - public function findFloat(string $key, ?float $default = null) + public function findFloat(string $key, ?float $default = null): ?float { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - return null; - } - - return (float)$value; + return $this->arrayReader->findFloat($key, $default); } /** @@ -197,13 +150,7 @@ public function findFloat(string $key, ?float $default = null) */ public function getBool(string $key, ?bool $default = null): bool { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); - } - - return (bool)$value; + return $this->arrayReader->getBool($key, $default); } /** @@ -214,15 +161,9 @@ public function getBool(string $key, ?bool $default = null): bool * * @return bool|null The value */ - public function findBool(string $key, ?bool $default = null) + public function findBool(string $key, ?bool $default = null): ?bool { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - return null; - } - - return (bool)$value; + return $this->arrayReader->findBool($key, $default); } /** @@ -237,17 +178,7 @@ public function findBool(string $key, ?bool $default = null) */ public function getChronos(string $key, ?Chronos $default = null): Chronos { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key)); - } - - if ($value instanceof Chronos) { - return $value; - } - - return new Chronos($value); + return $this->arrayReader->getChronos($key, $default); } /** @@ -258,51 +189,9 @@ public function getChronos(string $key, ?Chronos $default = null): Chronos * * @return Chronos|null The value */ - public function findChronos(string $key, ?Chronos $default = null) + public function findChronos(string $key, ?Chronos $default = null): ?Chronos { - $value = $this->find($key, $default); - - if ($this->isNullOrBlank($value)) { - return null; - } - - if ($value instanceof Chronos) { - return $value; - } - - return new Chronos($value); - } - - /** - * Find mixed value. - * - * @param string $path The path - * @param mixed|null $default The default value - * - * @return mixed|null The value - */ - public function find(string $path, $default = null) - { - if (array_key_exists($path, $this->data)) { - return $this->data[$path] ?? $default; - } - - if (strpos($path, '.') === false) { - return $default; - } - - $pathKeys = explode('.', $path); - - $arrayCopyOrValue = $this->data; - - foreach ($pathKeys as $pathKey) { - if (!isset($arrayCopyOrValue[$pathKey])) { - return $default; - } - $arrayCopyOrValue = $arrayCopyOrValue[$pathKey]; - } - - return $arrayCopyOrValue; + return $this->arrayReader->findChronos($key, $default); } /** @@ -312,18 +201,6 @@ public function find(string $path, $default = null) */ public function all(): array { - return $this->data; - } - - /** - * Is null or blank. - * - * @param mixed $value The value - * - * @return bool The status - */ - private function isNullOrBlank($value): bool - { - return $value === null || $value === ''; + return $this->arrayReader->all(); } }