Download the PHP package vcn/pipette without Composer
On this page you can find all versions of the php package vcn/pipette. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package pipette
Pipette
Easily extract what you need out of JSON.
Quickstart
composer require vcn/pipette
(From the examples/ directory)
Usage
Pipette expects you to have some string of which you have the strong suspicion that it represents JSON. You can ask Pipette to parse it:
Parsing might fail, but if it succeeds you are left with a Json\Value
.
It represents any of the possible values it could have parsed to and allows you to then query that value:
Note that if any of those assertions are false, an exception is thrown.
Optional values
Many methods can be prefixed with an upside-down question mark (¿).
This causes them to also return null
in the case of null
or the absence of a field in an object.
In the case of ¿field
the returned Json\OptionalValue
is a nullsafe variant where all subsequent calls will return null if the original field was null
or absent.
Unions
If you expect your JSON to match any of more than one structures, you can use either
:
See examples/huttonsrazor.php for another example.
Validation beyond the types
Pipette also provides a basic interface to hook in stronger validation methods. Currently it supports JSON Schema validations through justinrainbow/json-schema.
The idea is that you can define a JsonSchemaRepository
as a dependency, that then contains references to JsonSchemas
.
These schemas can then validate JSON after it has been parsed, but before you use pipette to transform it into typed data.
See the examples/ directory for a typical request parser setup.
Using Pipette for JSON-like data
Under the hood Pipette simply wraps the data returned by json_decode()
and performs ad-hoc validations based on your queries.
That means that if you have data whose type is isomorphic to that of json_decode()
you can use this library for that data as well:
What data does Pipette support? (or how does php-json
represent JSON?)
string
int
/float
(JSON numbers)bool
null
array
(Non-associative, JSON arrays)stdClass
(JSON objects)
Pipette's behaviour for any other type is undefined.
Careful!
Tests
Powered by phpspec. phpspec is configured to gather code coverage information. This requires xdebug or pcov. You can also disable code coverage.
All versions of pipette with dependencies
ext-json Version *