PHPackages                             earc/parameter-transformer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. earc/parameter-transformer

ActiveLibrary

earc/parameter-transformer
==========================

eArc - the explicit architecture framework - parameter transformer component

0.0.1(4y ago)0161MITPHPPHP ^8.0

Since May 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Koudela/eArc-parameter-transformer)[ Packagist](https://packagist.org/packages/earc/parameter-transformer)[ RSS](/packages/earc-parameter-transformer/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (1)

eArc-parameter-transformer
==========================

[](#earc-parameter-transformer)

Lightweight parameter transformer component. Use type hints to auto-wire `objects`, `functions` and `methods`.

Table of Contents
-----------------

[](#table-of-contents)

- [install](#install)
- [bootstrap](#bootstrap)
- [basic usage](#basic-usage)
    - [auto wire functions and methods](#auto-wire-functions-and-methods)
    - [auto update objects](#auto-update-objects)
- [configure and extend transformation](#configure-and-extend-transformation)
    - [how the input array is determined](#how-the-input-array-is-determined)
    - [how the input value is chosen](#how-the-input-value-is-chosen)
    - [how type hints are transformed](#how-type-hints-are-transformed)
    - [how callables and functions are transformed](#how-callables-and-functions-are-transformed)
    - [how objects are transformed](#how-objects-are-transformed)
- [additional info](#additional-info)
- [releases](#releases)
    - [release 0.0](#release-00)

install
-------

[](#install)

```
$ composer require earc/parameter-transformer
```

bootstrap
---------

[](#bootstrap)

earc/parameter-transformer uses [earc/di](https://github.com/Koudela/eArc-di) for dependency injection and tagging.

```
use eArc\DI\DI;

DI::init();
```

basic usage
-----------

[](#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

[](#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

[](#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
-----------------------------------

[](#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

[](#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

[](#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.

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

[](#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.).

4. 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);
    }
}
```

5. 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);
```

6. If the type hinted class can be build via [earc/di](https://github.com/Koudela/eArc-di) the result of `di_get()` will be taken as result value.
7. 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.
8. If the result value is `null` and there is a default value hinted, the default value is the new result value.
9. 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);
```

10. Transformed input values are removed from the input array.

### how callables and functions are transformed

[](#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](#how-type-hints-are-transformed) is applied.
3. The result is returned as ordered array with the parameter names as keys.

### how objects are transformed

[](#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);
```

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

3. If no exception was thrown, the method is invoked with the result array.
4. 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
---------------

[](#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](https://github.com/Koudela/eArc-cast) if it can be done by considering the property names without the type hints.

releases
--------

[](#releases)

### release 0.0

[](#release-00)

- 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](https://github.com/Koudela/eArc-di) package
- type hint transformation is extendable via the `ParameterTransformerFactoryServiceInterface`to define your own class type hint transformation

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

2

Last Release

1817d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b7308f2797252cace014cfec12c1b5978c1bf5608be78d7a188ff690192959f3?d=identicon)[Thomas Koudela](/maintainers/Thomas%20Koudela)

---

Top Contributors

[![Koudela](https://avatars.githubusercontent.com/u/21366492?v=4)](https://github.com/Koudela "Koudela (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/earc-parameter-transformer/health.svg)

```
[![Health](https://phpackages.com/badges/earc-parameter-transformer/health.svg)](https://phpackages.com/packages/earc-parameter-transformer)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
