neftaio/laravel-jmespath
laravel-jmespath
A Laravel 8 wrapper for use of the jmespath.php library. The jmespath.php library is an implementation of the JMESPath specification.
This package also provides Artisan commands to pre-compile and manage your JMESPath expressions.
Thanks to pakages developed by libtek libtek/laravel-jmespath and ofumbi ofumbi/laravel-jmespath.
Use this package carefully, actually, I only update dependencies to allow your installation on my projects.
Installation
Install through composer
$ composer require neftaio/laravel-jmespath
Add Service Provider
In config/app.php, add the service provider to the $providers array:
'providers' => [
// ...
Neftaio\Jmes\JmesServiceProvider::class,
],
Add alias
In config/app.php, add the facade to the $aliases array:
'aliases' => [
// ...
'Jmes' => Neftaio\Jmes\Facades\Jmes::class,
],
Publish the configuration file
If you'd like to modify the default configuration values or define expressions to pre-compile, publish the package config file:
php artisan vendor:publish --provider="Neftaio\Jmes\JmesServiceProvider"
This will create a jmes.php file in your config directory.
Usage
With facade:
$result = Jmes::search($expression, $data);
Helper function:
$result = jmes($expression, $data);
Search Collection: // Returns array or string
$names = collect($data);
$result = $names->search($expression);
Artisan commands
Two Artisan commands are available with the package:
jmes:compile
This compiles and caches JMESPath expressions. Expressions can be sourced in multiple ways:
-
Running the command with no options or arguments will look for expressions in the
jmes.phpconfig file:php artisan jmes:compile -
Passing a single expression to the command:
php artisan jmes:compile 'foo.*.baz' -
Setting the
-cor--clioption will prompt for expressions to be added manually:php artisan jmes:compile --cli Please enter a JMESPath expression: > foo.*.baz
jmes:clear
This will delete any previously compiled expressions:
php artisan jmes:clear
Pass -h or --help to either command to view its usage.
Related Packages
Powerful PHP database abstraction layer (DBAL) with many features for database s...
Laravel Serializable Closure provides an easy and secure way to serialize closur...