Download the PHP package emulgeator/array-to-class-mapper without Composer
On this page you can find all versions of the php package emulgeator/array-to-class-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download emulgeator/array-to-class-mapper
More information about emulgeator/array-to-class-mapper
Files in emulgeator/array-to-class-mapper
Package array-to-class-mapper
Short Description A library what is capable of transforming a multilevel array to a class object
License MIT
Informations about the package array-to-class-mapper
Array to Class Mapper
A simple library which automatically maps a given multi dimensional array to a given class.
Getting Started
Installing
Run composer require emulgeator/array-to-class-mapper
to add this library as a dependency to your project
Usage
Simple scalar type mapping
As you'll see the library casts the values from the given array.
So for example: $dto->id
will be an integer indeed.
The mapper does that by checking the type of the property. If it's not set it tries to fetch this information from the DocBlock
Class mapping
Complex type mapping
In case of complex types you both DocBlock and type hinting can be used. See the previous example modified to have a series of inner objects:
Defining custom mapper for specific type
If a data mapping is not possible by simply casting the stored value, custom mappers can be set:
The same works if the Custom type is in an array but in this case the mapper function will receive the subarray as is:
Limitations
The library currently is not able to import and auto load a class defined in a DocBlock. The following example will not work because the class is not imported neither we know the FQCN:
To fix this you need to use the FQCN of the class:
All versions of array-to-class-mapper with dependencies
ext-json Version *