Download the PHP package mittwald/jsonmapping without Composer
On this page you can find all versions of the php package mittwald/jsonmapping. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mittwald/jsonmapping
More information about mittwald/jsonmapping
Files in mittwald/jsonmapping
Package jsonmapping
Short Description Library for mapping PHP objects to JSON structures
License MIT
Informations about the package jsonmapping
Object-to-JSON mapping framework for PHP
This package contains a framework for mapping PHP objects into arbitrary JSON structures.
Installation
Install this package via Composer:
$ composer require mittwald/php-jsonmapping
Usage
Mapping objects
The basic interface provided by this package is the interface
Mw\JsonMapping\MappingInterface
. It models a basic mapping from one value to
another.
The most powerful implementation of this interface is the
Mw\JsonMapping\ObjectMapping
. The ObjectMapping
is used to map PHP objects
into an array structure (which can then be used for JSON serialization):
Alternatively, use the Mw\JsonMapping\MappingBuilder
for more concise expressions:
On the first glance, this code is similar to the following
However, the ObjectMapping
does more than simply calling getter methods and
building an array from them. The ObjectMapping also handles null objects or
getter methods not being available. So all in all, the following code is a much
better equivalent for the example:
Chaining mappings
Mappings can also be chained together:
This can also be used to map sub-objects:
Alternatively, using the MappingBuilder
:
Filtering
Object mappings can be filtered for specific properties:
Filters can also be nested, using the FilterSet
class:
Merging
Also, object mappings can be merged together:
Putting it all together
Find a complete example of all available mappings below; also, the examples/ folder contains more examples:.