Download the PHP package azavyalov/json-mapper without Composer
On this page you can find all versions of the php package azavyalov/json-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package json-mapper
This package maps PHP arrays to strictly-typed objects. You can convert arrays to Data Transfer Objects (DTO), Value Objects (VO), validate API JSON responses, or basically instantiate any classes with arrays. Inspired by Go/Rust/Zig/... structs and was born out of a necesity to validate 3rd-party API JSON responses.
Installation
Usage
Example
Below is a working example demonstrating most of the features (read the comments in the code too). Please read the following sections to have a better idea of what is and what is not allowed.
The Rules
This mapper is mostly strict with a few optional exceptions (see the following sections). Implicit conversions are not allowed, e.g. passing a '0' instead of 0 to an int field would result in an exception. Each class property has to have a type specified, which could be either a builtin PHP type or another class (nested objects). Properties can be nullable. Union and mixed types are not allowed.
Each array has to have its element type specified in the DocBlock in the [] format, e.g. @param string[] $myArrayField. Array elements could also be another class objects, e.g. @param ArrayElementClass[] $myArrayField.
If each element in an array is of the same type and cannot be null - you want a typed array, e.g. string[]. If elements of an array are of different types - you want a nested object (another class with typed properties). Sometimes objects will have peculiar key names that cannot be converted into PHP variable names, in which case your only option would be to use a map, e.g. array<string, int>.
A proeprty described in the class must be present in the JSON unless the property is marked as nullable. Properties not described in the class but present in the JSON will be simply ignored.
By default, each property has to have a type.
Options
allowIntToFloatConversion, e.g.$mapper = new JsonMapper(allowIntToFloatConversion: true)s(turned off by default).intvalues will be converted intofloatvalues when mapped into float type properties. When this option is off, passing anintvalue to afloatclass property would result in an exception.- This option is needed because JSON doesn't distinguish between
intandfloat, there's only a singlenumbertype. Many languages would convert round float numbers like5.0into5when producing JSON.
allowUntypedProperties, e.g.$mapper = new JsonMapper(allowUntypedProperties: true);(turned off by default).- Use of this option is discouraged and is meant to be used as a last resort.
Array Type Declarations
The type of PHP arrays has to be specified in the class' constructor's DocBlock. For arrays use the [] format and for maps use the array<> format with both key and value types specified.
What's the difference between an array and a map?
In PHP, an "array" is not an actual array and is more like a representation of a loose JSON object. An actual array (as seen in stritcly typed programming languages) is a sequence of values of the same type. The keys of an array are subsequential numbers starting from 0 (0 -> 1 -> 2 -> 3 -> ...).
A map is a collection of key-value pairs. With maps your keys could be both numbers and strings, and values could be whichiver type just like in an array. This package allows the keys to be either int or string, but not both (int|string is not allowed). All the values should be of the same type.