From 8fdbb58f5e67061e47ecde1202cb1cc5cce1b685 Mon Sep 17 00:00:00 2001 From: heinrichschiller Date: Mon, 7 Jul 2025 12:01:35 +0200 Subject: [PATCH 1/3] added, selective/array-reader library --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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", From 14c18083a038fbb500535afb91c91241d4d41f1b Mon Sep 17 00:00:00 2001 From: heinrichschiller Date: Mon, 7 Jul 2025 12:04:05 +0200 Subject: [PATCH 2/3] Configuration refactored, direct use of ArrayReader --- src/Configuration.php | 157 +++++------------------------------------- 1 file changed, 17 insertions(+), 140 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index acba5d7..2d92c78 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); } /** @@ -56,13 +51,7 @@ public function getInt(string $key, ?int $default = null): int */ public function findInt(string $key, ?int $default = null) { - $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); } /** @@ -96,13 +79,7 @@ public function getString(string $key, ?string $default = null): string */ public function findString(string $key, ?string $default = null) { - $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); } /** @@ -136,13 +107,7 @@ public function getArray(string $key, ?array $default = null): array */ public function findArray(string $key, ?array $default = null) { - $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); } /** @@ -176,13 +135,7 @@ public function getFloat(string $key, ?float $default = null): float */ public function findFloat(string $key, ?float $default = null) { - $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); } /** @@ -216,13 +163,7 @@ public function getBool(string $key, ?bool $default = null): bool */ public function findBool(string $key, ?bool $default = null) { - $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); } /** @@ -260,49 +191,7 @@ public function getChronos(string $key, ?Chronos $default = null): Chronos */ public function findChronos(string $key, ?Chronos $default = null) { - $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(); } } From 7878c0b411b8e0c2d3c342e5724f88c9a967eaac Mon Sep 17 00:00:00 2001 From: heinrichschiller Date: Mon, 7 Jul 2025 12:07:51 +0200 Subject: [PATCH 3/3] added, missing typehints --- src/Configuration.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Configuration.php b/src/Configuration.php index 2d92c78..d5fb9fa 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -49,7 +49,7 @@ 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 { return $this->arrayReader->findInt($key, $default); } @@ -77,7 +77,7 @@ 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 { return $this->arrayReader->findString($key, $default); } @@ -105,7 +105,7 @@ 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 { return $this->arrayReader->findArray($key, $default); } @@ -133,7 +133,7 @@ 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 { return $this->arrayReader->findFloat($key, $default); } @@ -161,7 +161,7 @@ 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 { return $this->arrayReader->findBool($key, $default); } @@ -189,7 +189,7 @@ 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 { return $this->arrayReader->findChronos($key, $default); }