Download the PHP package biig/optimus without Composer
On this page you can find all versions of the php package biig/optimus. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package optimus
Optimus
The goal of the library is to map two arrays (in and out) with an YAML file config.
- This library respects SemVer.
Install
You can install this library using composer with the following command:
Usage
To use this library, you have to create a transformer which needs to extends the AbstractMappingTransformer
class.
This Transformer will read mapping and apply it to input data.
Basic example
Transformer
You first have to create your Transformer and implement transform
method.
Here we use symfony/yaml ^3.4
to parse a YAML file.
The $result
variable now contains the new array.
Configuration
Assuming you got the PHP array $inputArray
(input data)
And considering you want to transform it to the following array (expected output)
You will have to implements the following YAML:
Available mapping options
You can declare your node
from
: string, the key path of theinput
arrayto
: string, the key path of theoutput
arrayfunction
: array, the php function to use to transform the dataname
: string, the function's nameparams
: [Optional] array, the function's parameters (key paths of theinput
array)
required
: boolean, if the key isrequired
condition
: array, the conditions that specify if the key is required or not
Examples
Transform a key a
to key b
Get a key b
by using a function
Note: Function getB($arg1)
must be declared in your related transformer
You can use functions to transform the input value to the expected value.
For example person civility (Mr, Mrs) to numbers (1, 2)
Make a field required
Actually, if from
value don't exist in your input array, the to
field don't appear in the output array.
If you want this field required, you can add required
key.
Use a default value
Actually, if from
value doesn't exist in your input array, the to
field doesn't appear in the output array.
If you want this field to get a default value, you can add default
key.
Add a condition
If you want to transform a field A only if a field B exists.
You can put multiple fields in your condition :
Get all errors mapping at a time
If you want to return all errors at a time you can set the next parameters in config