Skip to content

[JsonPath] Add utils methods first and last to JsonPath builder #60188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Symfony/Component/JsonPath/JsonPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ public function deepScan(): static
return new self($this->path.'..');
}

public function anyIndex(): static
Copy link
Member

@GromNaN GromNaN Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's good that you renamed this method. Its name wasn't clear (to me).

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;
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/JsonPath/Tests/JsonPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading