Skip to content

Commit

Permalink
Merge pull request #6 from codeliner/feature/path_exists
Browse files Browse the repository at this point in the history
New method: ArrayReader::pathExists()
  • Loading branch information
codeliner authored Jul 11, 2018
2 parents d7b2317 + 2a36abe commit db053de
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ArrayReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ public function mixedValue(string $aPath, $default = null)
return $value;
}

public function pathExists(string $aPath): bool
{
$pathKeys = $this->toPathKeys($aPath);

$arrayCopyOrValue = $this->originalArray;

foreach($pathKeys as $pathKey) {

if (!array_key_exists($pathKey, $arrayCopyOrValue)) {
return false;
}

$arrayCopyOrValue = $arrayCopyOrValue[$pathKey];
}

return true;
}

public function toArray(): array
{
return $this->originalArray;
Expand Down
21 changes: 21 additions & 0 deletions tests/ArrayReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,26 @@ public function provideForMixedTest()
['string']
];
}

/**
* @test
* @dataProvider provideForPathExistsTest
*/
public function it_checks_if_path_exists(string $path, array $arrayToCheck, bool $expected)
{
$reader = new ArrayReader($arrayToCheck);

$this->assertSame($expected, $reader->pathExists($path));
}

public function provideForPathExistsTest(): array
{
return [
['test.path', ['test' => ['path' => 'value']], true],
['test.path', ['test' => ['path' => null]], true],
['test.path', ['test' => ['other' => null]], false],
['test', ['test' => ['other' => null]], true],
];
}
}

0 comments on commit db053de

Please sign in to comment.