diff --git a/src/Symfony/Component/JsonPath/JsonPath.php b/src/Symfony/Component/JsonPath/JsonPath.php index b44f35795793c..1009369b0a56d 100644 --- a/src/Symfony/Component/JsonPath/JsonPath.php +++ b/src/Symfony/Component/JsonPath/JsonPath.php @@ -43,11 +43,21 @@ public function deepScan(): static return new self($this->path.'..'); } - public function anyIndex(): static + public function all(): static { return new self($this->path.'[*]'); } + public function first(): static + { + return new self($this->path.'[0]'); + } + + public function last(): static + { + return new self($this->path.'[-1]'); + } + public function slice(int $start, ?int $end = null, ?int $step = null): static { $slice = $start; diff --git a/src/Symfony/Component/JsonPath/Tests/JsonPathTest.php b/src/Symfony/Component/JsonPath/Tests/JsonPathTest.php index 66b27356c07e1..52d05bdaeb813 100644 --- a/src/Symfony/Component/JsonPath/Tests/JsonPathTest.php +++ b/src/Symfony/Component/JsonPath/Tests/JsonPathTest.php @@ -35,4 +35,31 @@ public function testBuildWithFilter() $this->assertSame('$.users[?(@.age > 18)]', (string) $path); } + + public function testAll() + { + $path = new JsonPath(); + $path = $path->key('users') + ->all(); + + $this->assertSame('$.users[*]', (string) $path); + } + + public function testFirst() + { + $path = new JsonPath(); + $path = $path->key('users') + ->first(); + + $this->assertSame('$.users[0]', (string) $path); + } + + public function testLast() + { + $path = new JsonPath(); + $path = $path->key('users') + ->last(); + + $this->assertSame('$.users[-1]', (string) $path); + } }