Skip to content

Commit

Permalink
Use is null and blank string as invalid values
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Jan 5, 2020
1 parent 8ad3976 commit 49671fb
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/ArrayReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Selective\ArrayReader;

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 @@ -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 === '';
}
}

0 comments on commit 49671fb

Please sign in to comment.