Skip to content

Koudela/eArc-parameter-transformer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

eArc-parameter-transformer

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 (transforms objects by calling setters and adding values to properties from input)
  • callableTransform (generates an array of arguments from input the callable can be called with)
  • callFromTransform (shortcut function: calls the callable or function with the arguments provided via callableTransform)
  • constructFromTransform (shortcut function: instantiates an object with the constructor arguments provided via callableTransform)
  • castFromTransform (shortcut function: retrieves an object via constructFromTransform and calls objectTransform 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 - if null input is taken from php://input)
  • config Configuration (a config object to fine tune transformation - if null 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

  1. 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.

  1. If the key does not exist, the next positional key (int) is used.
  2. 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

  1. If null is type hinted plus the starting value is the string 'null' the starting value will be treated as null.
  2. For build in primitive types the starting value is cast to the result value if it is not null.
  3. 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.).

  1. If a type hinted class implements the ParameterTransformerFactoryInterface the buildFromParameter() 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);
    }
}
  1. For all services implementing the ParameterTransformerFactoryServiceInterface and tagged by it will have the buildFromParameter() 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);
  1. If the type hinted class can be build via earc/di the result of di_get() will be taken as result value.

  2. 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.

  3. If the result value is null and there is a default value hinted, the default value is the new result value.

  4. If the result is a null value, but the type hint does not allow null a NullValueException is thrown.

You can disable this behaviour via the setNullIsAllowed() method:

use eArc\ParameterTransformer\Configuration;

$config = (new Configuration())->setNullIsAllowed(true);
  1. Transformed input values are removed from the input array.

how callables and functions are transformed

  1. If the target is a class string, the constructor method retrieved.
  2. For the parameters and their type hints type hint transformation is applied.
  3. 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.

  1. 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);
  1. 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.

  1. If no exception was thrown, the method is invoked with the result array.

  2. 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
  • type hint transformation is extendable via the ParameterTransformerFactoryServiceInterface to define your own class type hint transformation

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages