Download the PHP package zamaldev/json-model without Composer
On this page you can find all versions of the php package zamaldev/json-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zamaldev/json-model
More information about zamaldev/json-model
Files in zamaldev/json-model
Package json-model
Short Description Fast parse json info model
License MIT
Homepage https://github.com/zamaldev/json-model
Informations about the package json-model
JSON to Model
Simple and fast package to convert JSON into plain PHP objects.
Why?
Working with third-party APIs often means dealing with raw arrays or stdClass objects.
This package allows you to:
- Convert JSON into typed PHP objects
- Use IDE autocompletion and static analysis
- Keep your code clean and maintainable
- Generate model directly from response
- Do it with performance in mind
Requirements
- PHP 8+
Installation
Basic Usage
Key mapping
You can use Attributes\Map property attribute, so map JSON keys to model properties.
Arrays
To parse JSON arrays into array of models or scalars, Attributes\AsArray property attribute can be used. By default, array will be parsed into array of string, but you can specify type with additional properties. For nested array you could set nesting level by providing level property to Attributes\AsArray attribute.
DNF types
While working with inconsistent APIs, sometime you may need to support few types per one property. This is possible using Attributes\Caster interface. However, only you know the resolution logic, so you should write your own implementation for your specific case. Casters works with scalar typed properties and with arrays. If you need to cast value to any model, you have the $jsonModel parameter, this is current JSONModel instance, so you could use parse method.
Sanitizer
Don't blind trust any third party API you work with. It is important to validate or at least simple sanitize the input. For this purpose, you can use Attributes\Sanitizer interface. Package already provides Attributes\SimpleSanitizer, but you can write your own for your needs.
Note attributes order. As you can see from example, it goes from top to bottom, so sanitize chain will goes like this:
" vAlUe " -(trim)-> "vAlUe" -(strtolower)-> "value" -(ucfirst)-> "Value"
Generator
Working with many third party requests and huge responses, it is useful to just autogenerate everything. For this purposes JsonGenerator class exists.
Here is an example on how to use it:
This script will generate those files:
Some rules on how generator works:
- New class names generated at
[root model][key] - If key has prohibited symbols for class properties, they are simply omitted, but
Attributes\Mapis added - When there same class name is used for different objects (even with same structure), they resolved with incremental index
[root model][key][index] - If array has different types inside,
mixedtype will be used, so the json values will be used "as is"
All versions of json-model with dependencies
ext-json Version *