Go to file
Romain Canon e437d9405c feat: introduce attribute DynamicConstructor
In some situations the type handled by a constructor is only known at
runtime, in which case the constructor needs to know what class must be
used to instantiate the object.

For instance, an interface may declare a static constructor that is then
implemented by several child classes. One solution would be to register
the constructor for each child class, which leads to a lot of
boilerplate code and would require a new registration each time a new
child is created. Another way is to use the attribute
`\CuyZ\Valinor\Mapper\Object\DynamicConstructor`.

When a constructor uses this attribute, its first parameter must be a
string and will be filled with the name of the actual class that the
mapper needs to build when the constructor is called. Other arguments
may be added and will be mapped normally, depending on the source given
to the mapper.

```php
interface InterfaceWithStaticConstructor
{
    public static function from(string $value): self;
}

final class ClassWithInheritedStaticConstructor implements InterfaceWithStaticConstructor
{
    private function __construct(private SomeValueObject $value) {}

    public static function from(string $value): self
    {
        return new self(new SomeValueObject($value));
    }
}

(new \CuyZ\Valinor\MapperBuilder())
    ->registerConstructor(
        #[\CuyZ\Valinor\Attribute\DynamicConstructor]
        function (string $className, string $value): InterfaceWithStaticConstructor {
            return $className::from($value);
        }
    )
    ->mapper()
    ->map(ClassWithInheritedStaticConstructor::class, 'foo');
```
2022-08-30 15:15:41 +02:00
.github chore(deps): bump actions/cache from 3.0.4 to 3.0.5 2022-07-25 22:37:02 +02:00
docs feat: introduce attribute DynamicConstructor 2022-08-30 15:15:41 +02:00
qa feat: allow injecting a cache implementation that is used by the mapper 2022-05-23 20:28:02 +02:00
src feat: introduce attribute DynamicConstructor 2022-08-30 15:15:41 +02:00
tests feat: introduce attribute DynamicConstructor 2022-08-30 15:15:41 +02:00
.changelog release: version 0.10.0 2022-06-10 19:00:31 +02:00
.gitattributes qa: include and use Rector 2022-05-21 16:37:16 +02:00
.gitignore misc: ignore .idea folder 2022-07-05 23:12:51 +02:00
.php-cs-fixer.dist.php feat!: allow mapping to any type 2022-01-02 00:48:01 +01:00
composer.json doc: introduce mkdocs as a static documentation generator 2022-06-10 17:32:07 +02:00
composer.lock qa: include and use Rector 2022-05-21 16:37:16 +02:00
infection.json.dist feat!: improve message customization with formatters 2022-05-21 16:30:24 +02:00
LICENSE.md misc!: change license from GPL 3 to MIT 2021-11-30 13:01:20 +01:00
phpstan.neon.dist feat: allow injecting a cache implementation that is used by the mapper 2022-05-23 20:28:02 +02:00
phpunit.xml.dist feat: initial release 2021-11-28 18:21:56 +01:00
psalm.xml test: configure vimeo/psalm to verify type inference 2022-01-13 19:55:20 +01:00
README.md doc: format badges in readme file 2022-06-12 17:59:12 +02:00
rector.php qa: cache all QA tools results during GitHub workflow 2022-05-26 18:21:37 +02:00

Valinor • PHP object mapper with strong type support

Latest Stable Version PHP Version Require Total Downloads Mutation testing badge


Valinor is a PHP library that helps to map any input into a strongly-typed value object structure.

The conversion can handle native PHP types as well as other well-known advanced type annotations like array shapes, generics and more.

Installation

composer require cuyz/valinor

Documentation

Documentation can be found at valinor.cuyz.io.

Credits & thank you

The development of this library is mainly motivated by the kind words and the help of many people. I am grateful to everyone, especially to the contributors of this repository who directly help to push the project forward.

I also want to thank blackfire-logo Blackfire for providing a license of their awesome tool, leading to notable performance gains when using this library.