Skip to content

Commit

Permalink
Add null and blank value handling
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Apr 3, 2020
1 parent 22438e6 commit d0e2505
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions src/Configuration.php
Expand Up @@ -3,7 +3,6 @@
namespace Selective\Config;

use Cake\Chronos\Chronos;
use Exception;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -32,13 +31,15 @@ public function __construct(array $data = [])
* @param string $key The key
* @param int|null $default The default value
*
* @throws InvalidArgumentException
*
* @return int The value
*/
public function getInt(string $key, int $default = null): int
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
}

Expand All @@ -49,19 +50,19 @@ public function getInt(string $key, int $default = null): int
* Get value as integer or null.
*
* @param string $key The key
* @param int $default The default value
* @param int|null $default The default value
*
* @return int|null The value
*/
public function findInt(string $key, int $default = null)
{
$result = $this->find($key, $default);
$value = $this->find($key, $default);

if ($result === null) {
if ($this->isNullOrBlank($value)) {
return null;
}

return (int)$result;
return (int)$value;
}

/**
Expand All @@ -70,6 +71,8 @@ public function findInt(string $key, int $default = null)
* @param string $key The key
* @param string|null $default The default value
*
* @throws InvalidArgumentException
*
* @return string The value
*/
public function getString(string $key, string $default = null): string
Expand Down Expand Up @@ -108,13 +111,15 @@ public function findString(string $key, string $default = null)
* @param string $key The key
* @param array|null $default The default value
*
* @throws InvalidArgumentException
*
* @return array The value
*/
public function getArray(string $key, array $default = null): array
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
}

Expand All @@ -133,7 +138,7 @@ public function findArray(string $key, array $default = null)
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
return null;
}

Expand All @@ -146,13 +151,15 @@ public function findArray(string $key, array $default = null)
* @param string $key The key
* @param float|null $default The default value
*
* @throws InvalidArgumentException
*
* @return float The value
*/
public function getFloat(string $key, float $default = null): float
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
}

Expand All @@ -171,7 +178,7 @@ public function findFloat(string $key, float $default = null)
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
return null;
}

Expand All @@ -184,13 +191,15 @@ public function findFloat(string $key, float $default = null)
* @param string $key The key
* @param bool|null $default The default value
*
* @throws InvalidArgumentException
*
* @return bool The value
*/
public function getBool(string $key, bool $default = null): bool
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
}

Expand All @@ -209,7 +218,7 @@ public function findBool(string $key, bool $default = null)
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
return null;
}

Expand All @@ -222,13 +231,15 @@ public function findBool(string $key, bool $default = null)
* @param string $key The key
* @param Chronos|null $default The default value
*
* @throws InvalidArgumentException
*
* @return Chronos The value
*/
public function getChronos(string $key, Chronos $default = null): Chronos
{
$value = $this->find($key, $default);

if ($value === null) {
if ($this->isNullOrBlank($value)) {
throw new InvalidArgumentException(sprintf('No value found for key "%s"', $key));
}

Expand All @@ -245,15 +256,17 @@ public function getChronos(string $key, Chronos $default = null): Chronos
* @param string $key The key
* @param Chronos $default The default value
*
* @throws Exception Chronos date time parsing error
*
* @return Chronos|null The value
*/
public function findChronos(string $key, Chronos $default = null)
{
$value = $this->find($key, $default);

if ($value === null || $value instanceof Chronos) {
if ($this->isNullOrBlank($value)) {
return null;
}

if ($value instanceof Chronos) {
return $value;
}

Expand Down Expand Up @@ -285,12 +298,24 @@ public function find(string $path, $default = null)
}

/**
* Return all data as array.
* Return all settings as array.
*
* @return array The data
*/
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 === '';
}
}

0 comments on commit d0e2505

Please sign in to comment.