Skip to content

Commit 5442b48

Browse files
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.
1 parent 74c2a0b commit 5442b48

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

src/JsonQueriable.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nahid\JsonQ\Exceptions\ConditionNotAllowedException;
66
use Nahid\JsonQ\Exceptions\FileNotFoundException;
77
use Nahid\JsonQ\Exceptions\InvalidJsonException;
8+
use Nahid\JsonQ\Results\ValueNotFound;
89
use Nahid\JsonQ\Condition;
910

1011
trait JsonQueriable
@@ -339,13 +340,13 @@ protected function getFromNested($map, $node)
339340
}
340341

341342
if ($terminate) {
342-
return false;
343+
return new ValueNotFound();
343344
}
344345

345346
return $map;
346347
}
347348

348-
return false;
349+
return new ValueNotFound();
349350
}
350351

351352
/**
@@ -383,7 +384,7 @@ protected function processConditions()
383384
}
384385

385386
$value = $this->getFromNested($val, $rule['key']);
386-
$return = $value === null || $value !== '' ? call_user_func_array($function, [$value, $rule['value']]) : false;
387+
$return = $value instanceof ValueNotFound ? false : call_user_func_array($function, [$value, $rule['value']]);
387388
$tmp &= $return;
388389
}
389390
$res |= $tmp;

src/Results/ValueNotFound.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Nahid\JsonQ\Results;
4+
5+
/**
6+
* This class represents a query result where a given
7+
* value was queried but did not exist.
8+
*/
9+
class ValueNotFound {}

tests/JsonQueriableTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nahid\JsonQ\Jsonq;
66
use Nahid\JsonQ\Exceptions\FileNotFoundException;
77
use Nahid\JsonQ\Exceptions\InvalidJsonException;
8+
use Nahid\JsonQ\Results\ValueNotFound;
89

910
class JsonQueriableTest extends AbstractTestCase
1011
{
@@ -62,6 +63,18 @@ class JsonQueriableTest extends AbstractTestCase
6263
]
6364
]
6465
];
66+
67+
protected static $testDataNesting = [
68+
'level1' => [
69+
'level2' => [
70+
'level3-1' => 'data31',
71+
'level3-2' => 32,
72+
'level3-3' => false,
73+
'level3-4' => null,
74+
'level3-5' => '',
75+
]
76+
]
77+
];
6578

6679
protected function createFile()
6780
{
@@ -193,6 +206,39 @@ public function testGetDataFromFile($file, $result)
193206
}
194207
}
195208

209+
/**
210+
* @param mixed $path
211+
* @param mixed $expected
212+
*
213+
* @dataProvider getFromNestedProvider
214+
*/
215+
public function testGetFromNested($path, $expected)
216+
{
217+
$method = $this->makeCallable($this->jsonq, 'getFromNested');
218+
219+
$input = [self::$testDataNesting, $path];
220+
221+
$result = $method->invokeArgs($this->jsonq, $input);
222+
223+
if ($result instanceof ValueNotFound) {
224+
$result = ValueNotFound::class;
225+
}
226+
227+
$this->assertEquals($expected, $result);
228+
}
229+
230+
public function getFromNestedProvider()
231+
{
232+
return [
233+
['level1.level2.level3-1', 'data31'],
234+
['level1.level2.level3-2', 32],
235+
['level1.level2.level3-3', false],
236+
['level1.level2.level3-4', null],
237+
['level1.level2.level3-5', ''],
238+
['level1.level2.not-existing', ValueNotFound::class],
239+
];
240+
}
241+
196242
public function getDataFromFileProvider()
197243
{
198244
return [

0 commit comments

Comments
 (0)