Skip to content

Commit

Permalink
Merge pull request #34 from OFFLINE-GmbH/master
Browse files Browse the repository at this point in the history
Added ValueNotFound object
  • Loading branch information
nahid authored Oct 5, 2018
2 parents 74c2a0b + 5442b48 commit 72128f1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/JsonQueriable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -339,13 +340,13 @@ protected function getFromNested($map, $node)
}

if ($terminate) {
return false;
return new ValueNotFound();
}

return $map;
}

return false;
return new ValueNotFound();
}

/**
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/Results/ValueNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Nahid\JsonQ\Results;

/**
* This class represents a query result where a given
* value was queried but did not exist.
*/
class ValueNotFound {}
46 changes: 46 additions & 0 deletions tests/JsonQueriableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Nahid\JsonQ\Jsonq;
use Nahid\JsonQ\Exceptions\FileNotFoundException;
use Nahid\JsonQ\Exceptions\InvalidJsonException;
use Nahid\JsonQ\Results\ValueNotFound;

class JsonQueriableTest extends AbstractTestCase
{
Expand Down Expand Up @@ -62,6 +63,18 @@ class JsonQueriableTest extends AbstractTestCase
]
]
];

protected static $testDataNesting = [
'level1' => [
'level2' => [
'level3-1' => 'data31',
'level3-2' => 32,
'level3-3' => false,
'level3-4' => null,
'level3-5' => '',
]
]
];

protected function createFile()
{
Expand Down Expand Up @@ -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 [
Expand Down

0 comments on commit 72128f1

Please sign in to comment.