From 5442b488719cfcc750698b375d17deeff375df7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20K=C3=BCndig?= Date: Fri, 5 Oct 2018 10:37:16 +0200 Subject: [PATCH] Added ValueNotFound object If a value is queried it is possible that the value is null, '' or false. These are all valid return values that the user might want to use. To distinct these falsey values from a value that was effectifely not found, the ValueNotFound object was introduced in this commit. --- src/JsonQueriable.php | 7 +++--- src/Results/ValueNotFound.php | 9 +++++++ tests/JsonQueriableTest.php | 46 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/Results/ValueNotFound.php diff --git a/src/JsonQueriable.php b/src/JsonQueriable.php index 7192eb5..514818b 100644 --- a/src/JsonQueriable.php +++ b/src/JsonQueriable.php @@ -5,6 +5,7 @@ use Nahid\JsonQ\Exceptions\ConditionNotAllowedException; use Nahid\JsonQ\Exceptions\FileNotFoundException; use Nahid\JsonQ\Exceptions\InvalidJsonException; +use Nahid\JsonQ\Results\ValueNotFound; use Nahid\JsonQ\Condition; trait JsonQueriable @@ -339,13 +340,13 @@ protected function getFromNested($map, $node) } if ($terminate) { - return false; + return new ValueNotFound(); } return $map; } - return false; + return new ValueNotFound(); } /** @@ -383,7 +384,7 @@ protected function processConditions() } $value = $this->getFromNested($val, $rule['key']); - $return = $value === null || $value !== '' ? call_user_func_array($function, [$value, $rule['value']]) : false; + $return = $value instanceof ValueNotFound ? false : call_user_func_array($function, [$value, $rule['value']]); $tmp &= $return; } $res |= $tmp; diff --git a/src/Results/ValueNotFound.php b/src/Results/ValueNotFound.php new file mode 100644 index 0000000..b57861b --- /dev/null +++ b/src/Results/ValueNotFound.php @@ -0,0 +1,9 @@ + [ + 'level2' => [ + 'level3-1' => 'data31', + 'level3-2' => 32, + 'level3-3' => false, + 'level3-4' => null, + 'level3-5' => '', + ] + ] + ]; protected function createFile() { @@ -193,6 +206,39 @@ public function testGetDataFromFile($file, $result) } } + /** + * @param mixed $path + * @param mixed $expected + * + * @dataProvider getFromNestedProvider + */ + public function testGetFromNested($path, $expected) + { + $method = $this->makeCallable($this->jsonq, 'getFromNested'); + + $input = [self::$testDataNesting, $path]; + + $result = $method->invokeArgs($this->jsonq, $input); + + if ($result instanceof ValueNotFound) { + $result = ValueNotFound::class; + } + + $this->assertEquals($expected, $result); + } + + public function getFromNestedProvider() + { + return [ + ['level1.level2.level3-1', 'data31'], + ['level1.level2.level3-2', 32], + ['level1.level2.level3-3', false], + ['level1.level2.level3-4', null], + ['level1.level2.level3-5', ''], + ['level1.level2.not-existing', ValueNotFound::class], + ]; + } + public function getDataFromFileProvider() { return [