Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc16162f

Browse files
committed
feature#60188 [JsonPath] Add two utils methods toJsonPath builder (alexandre-daubois)
This PR was merged into the 7.3 branch.Discussion----------[JsonPath] Add two utils methods to `JsonPath` builder| Q | A| ------------- | ---| Branch? | 7.3| Bug fix? | no| New feature? | yes| Deprecations? | no| Issues | -| License | MITSmall DX improvements that goes with#60105 and#60083.This PR adds two new methods, `first()` and `last()`, added to JsonPath builder. This voluntary reminds methods from the DomCrawler component. The goal is not to add every possible method, but I think `first()` and `last()` are common enough to be added.I also propose to rename `anyIndex()` to `all()`.```php$path = new JsonPath();// Get the first user of the collection$path = $path->key('users')->first();``````php$path = new JsonPath();// Get the last user of the collection$path = $path->key('users')->last();``````php$path = new JsonPath();// Get all users of the collection$path = $path->key('users')->all();```Commits-------3bc3559 [JsonPath][DX] Add utils methods to `JsonPath` builder
2 parents79fa5f2 +3bc3559 commitc16162f

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

‎src/Symfony/Component/JsonPath/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
returnnewself($this->path.'..');
4444
}
4545

46-
publicfunctionanyIndex():static
46+
publicfunctionall():static
4747
{
4848
returnnewself($this->path.'[*]');
4949
}
5050

51+
publicfunctionfirst():static
52+
{
53+
returnnewself($this->path.'[0]');
54+
}
55+
56+
publicfunctionlast():static
57+
{
58+
returnnewself($this->path.'[-1]');
59+
}
60+
5161
publicfunctionslice(int$start, ?int$end =null, ?int$step =null):static
5262
{
5363
$slice =$start;

‎src/Symfony/Component/JsonPath/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+
publicfunctiontestAll()
40+
{
41+
$path =newJsonPath();
42+
$path =$path->key('users')
43+
->all();
44+
45+
$this->assertSame('$.users[*]', (string)$path);
46+
}
47+
48+
publicfunctiontestFirst()
49+
{
50+
$path =newJsonPath();
51+
$path =$path->key('users')
52+
->first();
53+
54+
$this->assertSame('$.users[0]', (string)$path);
55+
}
56+
57+
publicfunctiontestLast()
58+
{
59+
$path =newJsonPath();
60+
$path =$path->key('users')
61+
->last();
62+
63+
$this->assertSame('$.users[-1]', (string)$path);
64+
}
3865
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp