From 49671fb8a9c31ebc83a95f15298d957a8233e187 Mon Sep 17 00:00:00 2001 From: odan Date: Sun, 5 Jan 2020 19:48:43 +0100 Subject: [PATCH] Use is null and blank string as invalid values --- src/ArrayReader.php | 57 ++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index 06d8dbe..afe5ca3 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -3,7 +3,6 @@ namespace Selective\ArrayReader; use Cake\Chronos\Chronos; -use Exception; use InvalidArgumentException; /** @@ -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)); } @@ -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; } /** @@ -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 @@ -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)); } @@ -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; } @@ -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)); } @@ -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; } @@ -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)); } @@ -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; } @@ -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)); } @@ -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; } @@ -332,4 +345,16 @@ public function isEmpty(string $path): bool { return empty($this->find($path)); } + + /** + * Is null or blank. + * + * @param mixed $value The value + * + * @return bool The status + */ + private function isNullOrBlank($value): bool + { + return $value === null || $value === ''; + } }