Commit Graph

108 Commits

Author SHA1 Message Date
Romain Canon
2f310cf5ab misc: add configuration for Composer allowed plugins 2022-05-04 19:45:34 +02:00
Romain Canon
f2d9d1aa6c qa: validate composer configuration with strict mode 2022-05-04 19:45:34 +02:00
Romain Canon
9792722e4f misc: add Psalm configuration file to .gitattributes 2022-05-04 19:45:34 +02:00
Romain Canon
ee0d1fed6b qa: run Psalm when using composer check 2022-05-04 19:45:34 +02:00
Romain Canon
a681c78281 test: fix compiled cache file test 2022-04-09 17:44:32 +02:00
Romain Canon
3687379217 misc: remove symfony/polyfill-php80 dependency 2022-04-09 17:44:32 +02:00
Romain Canon
095e257884 test: add test for native constructor registration 2022-04-06 18:25:40 +02:00
Romain Canon
511a0dfee8 fix: handle function definition cache invalidation when file is modified 2022-04-06 18:24:16 +02:00
Romain Canon
0b042bc495 feat: handle filename in function definition 2022-04-06 18:24:16 +02:00
Romain Canon
03c84a1f09 misc: declare code type in docblocks 2022-04-06 18:19:35 +02:00
Romain Canon
b7923bc383 feat: handle class string of union of object 2022-04-06 18:18:17 +02:00
dependabot[bot]
fdb8154368 chore(deps): bump actions/cache from 2 to 3.0.1
Bumps [actions/cache](https://github.com/actions/cache) from 2 to 3.0.1.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v2...v3.0.1)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-04-06 18:17:08 +02:00
Romain Canon
aab7ae9adb test: add test to cover several value altering functions registration 2022-04-05 19:41:58 +02:00
Nathan Boiron
2f08e1a9b3 fix: call value altering function only if value is accepted 2022-04-05 19:41:58 +02:00
Romain Canon
c0208d084a release: version 0.7.0 2022-03-24 14:38:48 +01:00
dependabot[bot]
3bd6a8e834 chore(deps): bump actions/checkout from 2 to 3
Bumps [actions/checkout](https://github.com/actions/checkout) from 2 to 3.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v2...v3)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2022-03-24 14:24:59 +01:00
Nathan Boiron
ad51039cc3
feat: introduce a source builder
The `Source` class is a new entry point for sources that are not plain 
array or iterable. It allows accessing other features like camel-case 
keys or custom paths mapping in a convenient way.

It should be used as follows:

```php
$source = \CuyZ\Valinor\Mapper\Source\Source::json($jsonString)
    ->camelCaseKeys()
    ->map([
        'towns' => 'cities',
        'towns.*.label' => 'name',
    ]);

$result = (new \CuyZ\Valinor\MapperBuilder())
    ->mapper()
    ->map(SomeClass::class, $source);
```
2022-03-24 14:23:03 +01:00
Romain Canon
ecafba3b21 feat!: introduce method to register constructors used during mapping
It is now mandatory to explicitly register custom constructors —
including named constructors — that can be used by the mapper. The
former automatic registration of named constructor feature doesn't
work anymore.

BREAKING CHANGE: existing code must list all named constructors that
were previously automatically used by the mapper, and registerer them
using the method `MapperBuilder::registerConstructor()`.

The method `MapperBuilder::bind()` has been deprecated, the method above
should be used instead.

```php
final class SomeClass
{
    public static function namedConstructor(string $foo): self
    {
        // …
    }
}

(new \CuyZ\Valinor\MapperBuilder())
    ->registerConstructor(
        SomeClass::namedConstructor(...),
        // …or for PHP < 8.1:
        [SomeClass::class, 'namedConstructor'],
    )
    ->mapper()
    ->map(SomeClass::class, [
        // …
    ]);
```
2022-03-24 13:03:55 +01:00
Romain Canon
e5b5157eaf fix: properly handle alias types for function reflection 2022-03-17 22:03:29 +01:00
Romain Canon
b646ccecf2 fix: handle variadic arguments in callable constructors 2022-03-17 21:58:38 +01:00
Romain Canon
e2451df2c1 misc: handle class name in function definition 2022-03-17 21:41:08 +01:00
Romain Canon
1a599b0bdf misc!: change Attributes::ofType return type to array
There was no benefits having the return type as `iterable`, but it would
make it harder to use the result of the method.
2022-03-17 21:15:11 +01:00
Romain Canon
fd11177b06 misc: introduce functions container to wrap definition handling 2022-03-17 21:12:16 +01:00
Romain Canon
fdef93074c fix: handle parameter default object value compilation 2022-03-09 10:33:40 +01:00
Lucian Olariu
e3592e18c5
test: strengthen datetime mapping tests 2022-03-03 13:02:01 +01:00
Baptiste Lafontaine
b8a18feadc
fix: handle numeric key with camel case source key modifier
Co-authored-by: Nathan Boiron <nathan.boiron@gmail.com>
2022-03-01 18:37:58 +01:00
Nathan Boiron
b7a7d22993
feat: introduce a path-mapping source modifier
This modifier can be used to change paths in the source data using a dot
notation.

The mapping is done using an associative array of path mappings. This
array must have the source path as key and the target path as value.

The source path uses the dot notation (eg `A.B.C`) and can contain one
`*` for array paths (eg `A.B.*.C`).

```php
final class Country
{
    /** @var City[] */
    public readonly array $cities;
}

final class City
{
    public readonly string $name;
}

$source = new \CuyZ\Valinor\Mapper\Source\Modifier\PathMapping([
    'towns' => [
        ['label' => 'Ankh Morpork'],
        ['label' => 'Minas Tirith'],
    ],
], [
    'towns' => 'cities',
    'towns.*.label' => 'name',
]);

// After modification this is what the source will look like:
[
    'cities' => [
        ['name' => 'Ankh Morpork'],
        ['name' => 'Minas Tirith'],
    ],
];

(new \CuyZ\Valinor\MapperBuilder())
    ->mapper()
    ->map(Country::class, $source);
```
2022-02-26 11:33:50 +01:00
Romain Canon
79c7a4563d misc: add Striker HTML report when running infection 2022-02-26 11:20:37 +01:00
Romain Canon
cc208facb8 release: version 0.6.0 2022-02-24 14:32:15 +01:00
Romain Canon
359e32d03d
fix: transform exception thrown during object binding into a message 2022-02-24 11:27:27 +01:00
Romain Canon
cbf4e11154 fix: remove string keys when unpacking variadic parameter values 2022-02-24 11:23:26 +01:00
Romain Canon
1eb6e61913 feat!: improve interface inferring API
The method `MapperBuilder::infer()` can be used to infer an
implementation for a given interface.

The callback given to this method must return the name of a class that
implements the interface. Any arguments can be required by the callback;
they will be mapped properly using the given source.

```php
$mapper = (new \CuyZ\Valinor\MapperBuilder())
    ->infer(UuidInterface::class, fn () => MyUuid::class)
    ->infer(SomeInterface::class, fn (string $type) => match($type) {
        'first' => FirstImplementation::class,
        'second' => SecondImplementation::class,
        default => throw new DomainException("Unhandled type `$type`.")
    })->mapper();

// Will return an instance of `FirstImplementation`
$mapper->map(SomeInterface::class, [
    'type' => 'first',
    'uuid' => 'a6868d61-acba-406d-bcff-30ecd8c0ceb6',
    'someString' => 'foo',
]);

// Will return an instance of `SecondImplementation`
$mapper->map(SomeInterface::class, [
    'type' => 'second',
    'uuid' => 'a6868d61-acba-406d-bcff-30ecd8c0ceb6',
    'someInt' => 42,
]);

interface SomeInterface {}

final class FirstImplementation implements SomeInterface
{
    public readonly UuidInterface $uuid;

    public readonly string $someString;
}

final class SecondImplementation implements SomeInterface
{
    public readonly UuidInterface $uuid;

    public readonly int $someInt;
}
```
2022-02-24 10:48:49 +01:00
Romain Canon
6fdd62dfc2 fix: change license in composer.json 2022-02-21 17:05:28 +01:00
Romain Canon
b6b3296638 feat: handle variadic parameters in constructors
Using variadic parameters is now handled properly by the library,
meaning the following example will run:

```php
final class SomeClass
{
    /** @var string[] */
    private array $values;

    public function __construct(string ...$values)
    {
        $this->values = $values;
    }
}

(new \CuyZ\Valinor\MapperBuilder())
    ->mapper()
    ->map(SomeClass::class, ['foo', 'bar', 'baz']);
```
2022-02-19 20:17:03 +01:00
Romain Canon
c1a884fadd qa: require and include phpstan/phpstan-phpunit rules 2022-02-19 19:58:28 +01:00
Romain Canon
6d427088f7 feat!: improve object binding API
The method `MapperBuilder::bind()` can be used to define a custom way to
build an object during the mapping.

The return type of the callback will be resolved by the mapping to know
when to use it.

The callback can take any arguments, that will automatically be mapped
using the given source. These arguments can then be used to instantiate
the object in the desired way.

Example:

```php
(new \CuyZ\Valinor\MapperBuilder())
    ->bind(function(string $string, OtherClass $otherClass): SomeClass {
        $someClass = new SomeClass($string);
        $someClass->addOtherClass($otherClass);

        return $someClass;
    })
    ->mapper()
    ->map(SomeClass::class, [
        // …
    ]);
```
2022-02-19 19:58:28 +01:00
Romain Canon
422e6a8b27 feat: improve value altering API 2022-02-19 19:58:28 +01:00
Romain Canon
380961247e feat: introduce method to get parameter by index 2022-02-19 19:58:28 +01:00
Romain Canon
d6e778aff7 refactor: regroup object inferring strategies
Inferring object unions and named constructor are now done using the
same algorithm — in class `ObjectBuilderFilterer` — which is called from
a unique entry point in `ClassNodeBuilder`.
2022-02-19 19:58:28 +01:00
Romain Canon
30d4479aef misc: check value acceptance in separate node builder 2022-02-19 19:58:28 +01:00
Romain Canon
06e9dedfd8 misc: narrow union types during node build 2022-02-19 19:58:28 +01:00
Romain Canon
b49ebf37be feat: introduce function definition repository 2022-02-19 19:58:28 +01:00
Nathan Boiron
d9465222f4
feat: introduce a camel case source key modifier 2022-02-19 19:47:04 +01:00
mtouellette
18ccbebb9a
fix: ensure native mixed types remain valid 2022-02-16 15:50:14 +01:00
Marek Mikula
1b80a1df9d
fix: write temporary cache file inside cache subdirectory
Prevents potential issues with permissions on the system default 
temporary directory returned by `sys_get_temp_dir()`.
2022-02-15 23:24:15 +01:00
Romain Canon
810c1abc6f release: version 0.5.0 2022-01-26 19:57:43 +01:00
Fred-Jan van der Eijken
66aa4d688a
fix: return indexed list of attributes when filtering on type 2022-01-26 18:59:57 +01:00
Romain Canon
718d3c1bc2 feat: introduce automatic named constructor resolution
An object may have several ways of being created — in such cases it is
common to use so-called named constructors, also known as static factory
methods. If one or more are found, they can be called during the mapping
to create an instance of the object.

What defines a named constructor is a method that:

1. is public
2. is static
3. returns an instance of the object
4. has one or more arguments

```php
final class Color
{
    /**
     * @param int<0, 255> $red
     * @param int<0, 255> $green
     * @param int<0, 255> $blue
     */
    private function __construct(
        public readonly int $red,
        public readonly int $green,
        public readonly int $blue
    ) {}

    /**
     * @param int<0, 255> $red
     * @param int<0, 255> $green
     * @param int<0, 255> $blue
     */
    public static function fromRgb(
        int $red,
        int $green,
        int $blue,
    ): self {
        return new self($red, $green, $blue);
    }

    /**
     * @param non-empty-string $hex
     */
    public static function fromHex(string $hex): self
    {
        if (strlen($hex) !== 6) {
            throw new DomainException('Must be 6 characters long');
        }

        /** @var int<0, 255> $red */
        $red = hexdec(substr($hex, 0, 2));
        /** @var int<0, 255> $green */
        $green = hexdec(substr($hex, 2, 2));
        /** @var int<0, 255> $blue */
        $blue = hexdec(substr($hex, 4, 2));

        return new self($red, $green, $blue);
    }
}
```
2022-01-25 18:32:28 +01:00
Romain Canon
7869cbd09c refactor: remove unnecessary ClassSignature
This abstraction layer was not useful, so it is removed to simplify the
API around `ClassDefinition`.

A new method `ClassDefinition::type()` is also added, giving access to
the `ClassType` instance when working with a class definition.
2022-01-25 18:32:28 +01:00
Marco Pivetta
e280033941 feat: simplify type signature of TreeMapper#map()
This also brings `TreeMapper#map()` closer to PHPStorm support, whilst
still benefiting from more precise `vimeo/psalm:4.18.x` types thanks to
the conditional return specification.

Ref: https://github.com/CuyZ/Valinor/pull/59#discussion_r782362428
2022-01-13 19:55:20 +01:00