Skip to content
Closed
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 @@ -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",
Expand Down
169 changes: 23 additions & 146 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

use Cake\Chronos\Chronos;
use InvalidArgumentException;
use Selective\ArrayReader\ArrayReader;

/**
* Configuration.
*/
final class Configuration
{
/**
* @var array<mixed> The data
* @var ArrayReader
*/
private $data;
private ArrayReader $arrayReader;

/**
* Constructor.
Expand All @@ -22,7 +23,7 @@ final class Configuration
*/
public function __construct(array $data = [])
{
$this->data = $data;
$this->arrayReader = new ArrayReader($data);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -134,15 +105,9 @@ public function getArray(string $key, ?array $default = null): array
*
* @return array<mixed>|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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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();
}
}