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

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Apr 9, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

Small 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().

$path = new JsonPath();

// Get the first user of the collection
$path = $path->key('users')->first();
$path = new JsonPath();

// Get the last user of the collection
$path = $path->key('users')->last();
$path = new JsonPath();

// Get all users of the collection
$path = $path->key('users')->all();

@carsonbot carsonbot added Status: Needs Review DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature JsonPath labels Apr 9, 2025
@carsonbot carsonbot added this to the 7.3 milestone Apr 9, 2025
@carsonbot carsonbot changed the title [JsonPath][DX] Add utils methods to JsonPath builder [JsonPath] Add utils methods to JsonPath builder Apr 9, 2025
@alexandre-daubois alexandre-daubois changed the title [JsonPath] Add utils methods to JsonPath builder [JsonPath] Add two utils methods to JsonPath builder Apr 9, 2025
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

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

Good idea, this names are the are commonly used in libraries.

@@ -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).

@GromNaN
Copy link
Member

GromNaN commented Apr 10, 2025

Thank you @alexandre-daubois.

@GromNaN GromNaN merged commit c16162f into symfony:7.3 Apr 10, 2025
6 of 11 checks passed
@GromNaN GromNaN changed the title [JsonPath] Add two utils methods to JsonPath builder [JsonPath] Add utils methods first and last to JsonPath builder Apr 10, 2025
@fabpot fabpot mentioned this pull request May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX DX = Developer eXperience (anything that improves the experience of using Symfony) Feature JsonPath Status: Reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants