Download the PHP package shureban/laravel-object-mapper without Composer
On this page you can find all versions of the php package shureban/laravel-object-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-object-mapper
Laravel object mapper
Installation
Require this package with composer using the following command:
Add the following class to the providers
array in config/app.php
:
You can also publish the config file to change implementations (ie. interface to specific class).
How to use
You have 3 options to use ObjectMapper
Inheritance
Your mapped object (dto) must inheritance from \Shureban\LaravelObjectMapper\MappableObject
Using trait
Your mapped object (dto) must use \Shureban\LaravelObjectMapper\MappableTrait
Delegate mapping to ObjectMapper
Mappable cases
Below you will see cases which you can use for mapping data into your object
Simple types
mixed
string
bool
,boolean
int
,integer
double
,float
array
object
Box types
Carbon
DateTime
Collection
Custom types
CustomClass
Enum
Eloquent
Array of types
That typo of mapping may be realized only via phpDoc notation
int[]
int[][]
DateTime[]
CustomClass[]
Special cases
Constructor
If your type object has 1 required parameter and value is NOT an array, ObjectMapper will build instance of this type via constructor call If your type object has 0 or more than 1 required parameters, it will throw WrongConstructorParametersNumberException exception
Correct case:
Wrong case:
PhpDoc
PhpDoc type hinting has much more priority than main type.
Setters
If you want to realize your own logic for setting value, you may place setter method in your mapped object
This setter should start from set
word and been in camelCase notation.
Some words about second parameter $rawData
. Value of this parameter depends on method selected for mapping
- mapFromJson - $rawData will be JSON
- mapFromArray - $rawData will be Array
- mapFromRequest - $rawData will be FormRequest object
Readonly parameters
Readonly parameters will always be skipped
Config rewriting
In object_mapper.php
config file have been presented all mappable types classes. You have opportunity to rewrite
mapping flow or realize you own one.
If you need to create your own type mapping, follow this way:
- create class inherited from
\Shureban\LaravelObjectMapper\Types\Type
- place you type into config file in
type -> box
array