From 93e7e733df60d8b4dbd998e1f543313ecf2aaac0 Mon Sep 17 00:00:00 2001 From: odan Date: Wed, 23 Oct 2019 08:45:40 +0200 Subject: [PATCH 1/3] Init --- src/ArrayReader.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index 06d8dbe..03dd21e 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -32,6 +32,8 @@ 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 @@ -49,7 +51,7 @@ 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 */ @@ -70,6 +72,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,6 +112,8 @@ 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 @@ -146,6 +152,8 @@ 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 @@ -184,6 +192,8 @@ 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 @@ -222,6 +232,8 @@ 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 From 544e22f19e0596784312b5ef98dacfcacc570705 Mon Sep 17 00:00:00 2001 From: odan Date: Tue, 10 Dec 2019 09:48:36 +0100 Subject: [PATCH 2/3] Add check for blank strings --- src/ArrayReader.php | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/ArrayReader.php b/src/ArrayReader.php index 03dd21e..b52ee6f 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -40,7 +40,7 @@ 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)); } @@ -57,13 +57,13 @@ public function getInt(string $key, int $default = null): int */ 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; } /** @@ -120,7 +120,7 @@ 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)); } @@ -139,7 +139,7 @@ public function findArray(string $key, array $default = null) { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { return null; } @@ -160,7 +160,7 @@ 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)); } @@ -179,7 +179,7 @@ public function findFloat(string $key, float $default = null) { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { return null; } @@ -200,7 +200,7 @@ 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)); } @@ -219,7 +219,7 @@ public function findBool(string $key, bool $default = null) { $value = $this->find($key, $default); - if ($value === null) { + if ($this->isNullOrBlank($value)) { return null; } @@ -240,7 +240,7 @@ 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)); } @@ -265,7 +265,11 @@ 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; } @@ -344,4 +348,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 === ''; + } } From 2e866ef17c0eaa645f3a85e3dd314c470d933b44 Mon Sep 17 00:00:00 2001 From: odan Date: Wed, 11 Dec 2019 13:38:28 +0100 Subject: [PATCH 3/3] Allow chronos v2 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c9ed7ce..641abdb 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "license": "MIT", "require": { "php": "^7.0", - "cakephp/chronos": "^1.2" + "cakephp/chronos": "^1.2|^2" }, "require-dev": { "overtrue/phplint": "^1.1",