Download the PHP package messere/php-value-mask without Composer
On this page you can find all versions of the php package messere/php-value-mask. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download messere/php-value-mask
More information about messere/php-value-mask
Files in messere/php-value-mask
Package php-value-mask
Short Description extract subset of array / object values
License MIT
Homepage https://github.com/Messere/php-value-mask
Informations about the package php-value-mask
php-value-mask
Purpose
Google in their Performance Tips for
APIs, suggest to limit required bandwidth by filtering out unused fields in response. Their APIs
support additional URL parameter fields
which asks API to include only specific fields in response.
fields
parameter follows a simple syntax, which allows to query for nested keys, multiple keys
or use wildcard to include all fields (see Syntax and Grammar sections below).
This library implements parsing of fields
parameter and filtering of arrays / objects.
See also: PSR-15 compatible middleware based on this library: Partial Response PSR-15 Middleware
Usage example
Let's analyze elements of used filter:
title
matches top level element with key title ('Good Omens')identifiers/isbn
matches top level elementidentifiers
and then includesisbn
element from matched object ('ISBN 83-85100-63-6')authors/firstName
finds an array of elements (list) under the keyauthors
and examines all elements, extractingfirstName
from each. ('Terry' and 'Neil')*(us,uk)
examines all properties and extracts fieldsus
anduk
. ('1990' and '1990' fromyear
element, 'Workman', 'Gollancz' frompublisher
)keywords
does not match anything and is silently ignored.
As a result we expect the following output:
ready to serialize to JSON
, etc.
Note that library does preserve the structure/nesting of values, but not necessarily types of values - all objects are converted to associative arrays with object's public properties as keys.
Syntax
a
selects keya
from inputa,b,c
comma separated list of elements: selects keysa
andb
andc
a/b/c
nested elements: selects keyc
from parent elementb
which in turn has parent elementa
a(b,c)
multiple elements: selects elementsb
andc
from parent elementa
a(b,c/d)
multiple elements: from parenta
select elementb
and elementd
nested inc
a/*/c
wildcard: selects elementc
from all children of elementa
*(b,c)
wildcard: selects elementsb
andc
from any parent
Etc. See tests for more examples as well as examples of invalid filters.
Grammar
Since Google does not provide detailed grammar for their "fields" language, this package uses the following arbitrarily selected rules, that in author's opinion closely resamble intent of original authors.
In EBNF notation:
Acknowledgements
Library is inspired by:
- Google's API performance tips.
- Similar JavaScript library: nemtsov/json-mask.
License
MIT
All versions of php-value-mask with dependencies
ext-json Version *