diff --git a/src/ArrayReader.php b/src/ArrayReader.php index ef0207b..f8f4c5d 100644 --- a/src/ArrayReader.php +++ b/src/ArrayReader.php @@ -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; diff --git a/tests/ArrayReaderTest.php b/tests/ArrayReaderTest.php index c10c852..7029034 100644 --- a/tests/ArrayReaderTest.php +++ b/tests/ArrayReaderTest.php @@ -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], + ]; + } }