diff --git a/Neos.Utility.Arrays/Classes/Arrays.php b/Neos.Utility.Arrays/Classes/Arrays.php index af19c9cab1..07938bd4c3 100644 --- a/Neos.Utility.Arrays/Classes/Arrays.php +++ b/Neos.Utility.Arrays/Classes/Arrays.php @@ -194,7 +194,31 @@ public static function array_reduce(array $array, string $function, $initial = n public static function getValueByPath(array &$array, $path) { if (is_string($path)) { - $path = explode('.', $path); + if (strpos($path, '\\.') === -1) { + $path = explode('.', $path); + } else { + $pattern = '#(?= 0 && preg_match('#^\\\\*$#', $value)) { + $result[$lastIndex] = $result[$lastIndex] . $value; + } else { + $result[] = $value; + } + return $result; + }, + [] + ); + $path = array_map( + static function ($value) { + return str_replace(['\\\\','\\.'], ['\\','.'], $value); + }, + $split + ); + } } elseif (!is_array($path)) { throw new \InvalidArgumentException('getValueByPath() expects $path to be string or array, "' . gettype($path) . '" given.', 1304950007); } diff --git a/Neos.Utility.Arrays/Tests/Unit/ArraysTest.php b/Neos.Utility.Arrays/Tests/Unit/ArraysTest.php index b38e948997..45e37535ee 100644 --- a/Neos.Utility.Arrays/Tests/Unit/ArraysTest.php +++ b/Neos.Utility.Arrays/Tests/Unit/ArraysTest.php @@ -80,6 +80,30 @@ public function getValueByPathReturnsTheValueOfANestedArrayByFollowingTheGivenPa $this->assertSame($expectedResult, $actualResult); } + /** + * @test + */ + public function getValueByPathReturnsTheValueOfANestedArrayByFollowingTheGivenPathIfPathIsStringWithEscapedDots() + { + $path = 'Foo.Bar\.Baz.2'; + $array = ['Foo' => ['Bar.Baz' => [2 => 'the value']]]; + $expectedResult = 'the value'; + $actualResult = Arrays::getValueByPath($array, $path); + self::assertSame($expectedResult, $actualResult); + } + + /** + * @test + */ + public function getValueByPathReturnsTheValueOfANestedArrayByFollowingTheGivenPathIfPathIsStringWithBackslashes() + { + $path = 'Foo.Bar\\\\.Baz.2'; + $array = ['Foo' => ['Bar\\' => ['Baz' => [2 => 'the value']]]]; + $expectedResult = 'the value'; + $actualResult = Arrays::getValueByPath($array, $path); + self::assertSame($expectedResult, $actualResult); + } + /** * @test * @expectedException \InvalidArgumentException