Skip to content

Commit 6cfdf02

Browse files
[JsonPath][DX] Add utils methods to JsonPath builder
1 parent 65e268e commit 6cfdf02

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

JsonPath.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ public function deepScan(): static
4343
return new self($this->path.'..');
4444
}
4545

46-
public function anyIndex(): static
46+
public function all(): static
4747
{
4848
return new self($this->path.'[*]');
4949
}
5050

51+
public function first(): static
52+
{
53+
return new self($this->path.'[0]');
54+
}
55+
56+
public function last(): static
57+
{
58+
return new self($this->path.'[-1]');
59+
}
60+
5161
public function slice(int $start, ?int $end = null, ?int $step = null): static
5262
{
5363
$slice = $start;

Tests/JsonPathTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,31 @@ public function testBuildWithFilter()
3535

3636
$this->assertSame('$.users[?(@.age > 18)]', (string) $path);
3737
}
38+
39+
public function testAll()
40+
{
41+
$path = new JsonPath();
42+
$path = $path->key('users')
43+
->all();
44+
45+
$this->assertSame('$.users[*]', (string) $path);
46+
}
47+
48+
public function testFirst()
49+
{
50+
$path = new JsonPath();
51+
$path = $path->key('users')
52+
->first();
53+
54+
$this->assertSame('$.users[0]', (string) $path);
55+
}
56+
57+
public function testLast()
58+
{
59+
$path = new JsonPath();
60+
$path = $path->key('users')
61+
->last();
62+
63+
$this->assertSame('$.users[-1]', (string) $path);
64+
}
3865
}

0 commit comments

Comments
 (0)