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:
titlematches top level element with key title ('Good Omens')identifiers/isbnmatches top level elementidentifiersand then includesisbnelement from matched object ('ISBN 83-85100-63-6')authors/firstNamefinds an array of elements (list) under the keyauthorsand examines all elements, extractingfirstNamefrom each. ('Terry' and 'Neil')*(us,uk)examines all properties and extracts fieldsusanduk. ('1990' and '1990' fromyearelement, 'Workman', 'Gollancz' frompublisher)keywordsdoes 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
aselects keyafrom inputa,b,ccomma separated list of elements: selects keysaandbandca/b/cnested elements: selects keycfrom parent elementbwhich in turn has parent elementaa(b,c)multiple elements: selects elementsbandcfrom parent elementaa(b,c/d)multiple elements: from parentaselect elementband elementdnested inca/*/cwildcard: selects elementcfrom all children of elementa*(b,c)wildcard: selects elementsbandcfrom 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 *