earc / parameter-transformer
eArc - the explicit architecture framework - parameter transformer component
Requires
- php: ^8.0
- earc/di: ^3.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.2
This package is auto-updated.
Last update: 2025-03-24 23:38:51 UTC
README
Lightweight parameter transformer component. Use type hints to auto-wire
objects
, functions
and methods
.
Table of Contents
install
$ composer require earc/parameter-transformer
bootstrap
earc/parameter-transformer uses earc/di for dependency injection and tagging.
use eArc\DI\DI; DI::init();
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
use eArc\ParameterTransformer\Configuration; use eArc\ParameterTransformer\ParameterTransformer; $input = [ 'parameterNameOne' => 'someValue', 'parameterNameTwo' => 'anotherValue', //... ]; // $target has to be a callable a class string or an instance of \ReflectionFunctionAbstract $target = [MyClass::class, 'myMethod']; $argv = (new ParameterTransformer())->callableTransform($target, $input, new Configuration());
auto update objects
use eArc\ParameterTransformer\Configuration; use eArc\ParameterTransformer\ParameterTransformer; $input = [ 'parameterNameOne' => 'someValue', 'parameterNameTwo' => 'anotherValue', //... ]; $target = new MyClass(); $target = (new ParameterTransformer())->objectTransform($target, $input, new Configuration());
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:
use eArc\ParameterTransformer\Configuration; $config = (new Configuration())->setDefaultResource([new MyInputProvider(), 'getDefault']);
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:
use eArc\ParameterTransformer\Configuration; $mapping = [ 'id' => 0, 'product' => 'productName', 'description' => 'text', ]; $config = (new Configuration())->setMapping($mapping);
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:
use eArc\ParameterTransformer\Configuration; $config = (new Configuration())->setNoInputIsAllowed(true);
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:
use eArc\ParameterTransformer\Configuration; $config = (new Configuration())->setPredefinedTypeHints([ MyServiceOne::class => new MyServiceOne(), MyServiceTwo::class => new MyServiceTwo(), //... ]);
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.
use eArc\ParameterTransformer\Interfaces\ParameterTransformerFactoryInterface; class MyClient implements ParameterTransformerFactoryInterface { //... public function __construct(string $connectionConfigurationString) { //... } //... public static function buildFromParameter($parameter) : static { return new MyClient((string) $parameter); } }
- 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.
use eArc\Data\Entity\Interfaces\EntityInterface; use eArc\ParameterTransformer\Interfaces\ParameterTransformerFactoryServiceInterface; class MyEntityTypeHintingService implements ParameterTransformerFactoryServiceInterface { public function buildFromParameter(string $fQCN, $parameter) : object|null { if (is_subclass_of($fQCN, EntityInterface::class)) { return data_load($fQCN, $parameter); } return di_get(EntityManagerInterface::class) ->getRepository($fQCN) ?->find($parameter); } } di_tag(ParameterTransformerFactoryServiceInterface::class, MyEntityTypeHintingService::class);
-
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:
use eArc\ParameterTransformer\Configuration; $config = (new Configuration())->setNullIsAllowed(true);
- 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:
use eArc\ParameterTransformer\Configuration; $config = (new Configuration())->setMethodsFirst(false);
- 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:
use eArc\ParameterTransformer\Configuration; $config = (new Configuration()) ->setFilterMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED) ->setMaxParameterCount(3) ->setNoInputIsAllowed(true);
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:
use eArc\ParameterTransformer\Configuration; $config = (new Configuration()) ->setFilterProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED); $config = (new Configuration()) ->setUsePropertyTransformation(false);
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