Download the PHP package andreypostal/json-handler-php without Composer
On this page you can find all versions of the php package andreypostal/json-handler-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andreypostal/json-handler-php
More information about andreypostal/json-handler-php
Files in andreypostal/json-handler-php
Package json-handler-php
Short Description Just a light and simple JSON helper that will make it easy for you to deal with json and objects
License MIT
Informations about the package json-handler-php
Json Handler
Just a light and simple JSON helper that will make it easy for you to deal with json and objects.
Installation
Usage
Classes
When creating your Value Objects that represent a JSON entity you just need
to add the JsonItemAttribute
to each property that will be present in the JSON.
In the case of the entire object being a JsonObject with a direct 1:1 match (or perfect mirror of the keys), you can use the JsonObjectAttribute
You can also combine both when need to add custom key or if you want to make an item required.
If your Value Object has some property that won't be present in the JSON, you can just omit the attribute for it and the other ones will be processed normally.
In case the items are required to exist in the JSON being processed, you must add the required flag in the attribute.
When some of the keys in your JSON are different from your object, you can include the JSON key in the attribute.
Also, if you have a property that is an array of other object, you must inform the class in the attribute using the type
option.
This will work as a hint so the hydrator can instantiate the appropriate object. This works with enums as well.
The type option can be used to validate that all the items in an array have some desired type as well, like "string", "integer"...
Handler
In order to utilize the definitions mentioned above, you must utilize the JsonHandler
. Two traits are available as well,
the JsonHydratorTrait
and JsonSerializerTrait
that provide the methods both for serialization and hydration.