Download the PHP package earc/parameter-transformer without Composer
On this page you can find all versions of the php package earc/parameter-transformer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download earc/parameter-transformer
More information about earc/parameter-transformer
Files in earc/parameter-transformer
Package parameter-transformer
Short Description eArc - the explicit architecture framework - parameter transformer component
License MIT
Informations about the package parameter-transformer
eArc-parameter-transformer
Lightweight parameter transformer component. Use type hints to auto-wire
objects
, functions
and methods
.
Table of Contents
- install
- bootstrap
- basic usage
- auto wire functions and methods
- auto update objects
- configure and extend transformation
- how the input array is determined
- how the input value is chosen
- how type hints are transformed
- how callables and functions are transformed
- how objects are transformed
- additional info
- releases
- release 0.0
install
bootstrap
earc/parameter-transformer uses earc/di for dependency injection and tagging.
basic usage
All transforming functionality of the earc/parameter-transformer is bundled into
the service ParameterTransformer
. The parameter transformer exposes five methods:
objectTransform
(transformsobjects
by calling setters and adding values to properties from input)callableTransform
(generates an array of arguments from input thecallable
can be called with)callFromTransform
(shortcut function: calls thecallable
orfunction
with the arguments provided viacallableTransform
)constructFromTransform
(shortcut function: instantiates an object with the constructor arguments provided viacallableTransform
)castFromTransform
(shortcut function: retrieves an object viaconstructFromTransform
and callsobjectTransform
on it)
All methods take three arguments:
- target (the target the type hints are read from and transformations are applied to)
- input
array
(the input the arguments for the type hints are calculated from - ifnull
input is taken fromphp://input
) - config
Configuration
(a config object to fine tune transformation - ifnull
a default config is used)
auto wire functions and methods
auto update objects
configure and extend transformation
The transformation is configured via the Configuration
object and extended
via the ParameterTransformerFactoryInterface
and the
ParameterTransformerFactoryServiceInterface
. To use this properly you have to
understand how the transformation process works.
how the input array is determined
If an input array is provided via the seconds service method parameter, this
input is taken. Otherwise php://input
is used to create an input array.
You can provide an alternative fallback via the setDefaultResource()
method:
how the input value is chosen
- The name of the variable is used to retrieve the starting value from the input.
For example if the variable is named
$product
the$input['product']
is taken as value.
You can configure this behaviour via the setMapping()
method:
The mapping is applied to the key before choosing the input value.
- If the key does not exist, the next positional key (
int
) is used. - If neither the named key nor the next positional key exists a
NoInputException
is thrown.
You can change this behaviour via the setNoInputIsAllowed()
method:
Instead of throwing an exception a null
value is used.
how type hints are transformed
- If
null
is type hinted plus the starting value is the string'null'
the starting value will be treated asnull
. - For build in primitive types the starting value is cast to the result value if
it is not
null
. - If it's not a build in primitive type plus a predefined type hint value exists, the predefined type hint value is used as result value.
You set the predefined type hint values via the setPredefinedTypeHints()
method:
If you want to provide a complete Service-Container to enhance this library with
the power of your dependency injection system, you should use the
ParameterTransformerFactoryServiceInterface
to provide a dynamic solution (see 5.).
- If a type hinted class implements the
ParameterTransformerFactoryInterface
thebuildFromParameter()
method is used to build the result value.
You can implement this if there is a way to build or retrieve an object via a single parameter, for example an entity.
- For all services implementing the
ParameterTransformerFactoryServiceInterface
and tagged by it will have thebuildFromParameter()
called until one of them returns an object. This object is used as result value.
This is especially useful for entities.
-
If the type hinted class can be build via earc/di the result of
di_get()
will be taken as result value. -
If it is a union type one type hint after the other is taken for evaluation (1.-6.). The first result different from the input value (
!=
) and not null is the result value. -
If the result value is
null
and there is a default value hinted, the default value is the new result value. - If the result is a
null
value, but the type hint does not allownull
aNullValueException
is thrown.
You can disable this behaviour via the setNullIsAllowed()
method:
- Transformed input values are removed from the input array.
how callables and functions are transformed
- If the target is a class string, the constructor method retrieved.
- For the parameters and their type hints type hint transformation is applied.
- The result is returned as ordered array with the parameter names as keys.
how objects are transformed
In contrast to callables and functions there is no result array. The transformation is applied to the object directly.
- The methods are processed first and then the properties.
You can change this behaviour via the setMethodsFirst()
method:
- All public methods that have exactly one parameter are processed until the complete input array is processed.
You can change this behaviour via the setFilterMethods()
the setMaxParameterCount()
and the setNoInputIsAllowed()
method:
If the maximal parameter count is greater than one, the methods are processed in the order of their parameter count.
To use only property transformation set the maximal parameter count to zero.
If the input array is empty and no input is allowed null
values are used for input.
-
If no exception was thrown, the method is invoked with the result array.
- Foreach public property the type hint is evaluated until the complete input array is processed. The current values are replaced by the result values.
You can change this behaviour by the setFilterProperties()
and setUsePropertyTransformation()
methods:
additional info
There is no recursion implemented. Building type hinted classes via the earc/parameter-transformer has to be done explicit. The input data can hold complex data-structures thus preprocessing is an option if the object structure is known.
To map data from an input array to an arbitrary object can be done more efficient using earc/cast if it can be done by considering the property names without the type hints.
releases
release 0.0
- first official release
- PHP ^8.0
- supported type hints:
- native:
- null
- int
- float
- bool
- string
- self:
ParameterTransformerFactoryInterface
- extern:
- all classes that can be build via the
di_get()
function of the earc/di package
- all classes that can be build via the
- native:
- type hint transformation is extendable via the
ParameterTransformerFactoryServiceInterface
to define your own class type hint transformation