Commit Graph

216 Commits

Author SHA1 Message Date
Romain Canon
892f3831c2 feat: introduce composite types
Composite types are composed of other types and must now implement a
method to recursively traverse all sub-types.
2022-05-09 18:35:03 +02:00
Romain Canon
5f9d41cf35 test: require and make use of vfsStream library 2022-05-08 16:48:15 +02:00
Romain Canon
17d646ef51 test: rename test 2022-05-08 16:48:15 +02:00
Romain Canon
8443847cb8 misc: bump dev-dependencies 2022-05-06 14:00:43 +02:00
Romain Canon
c08fe5a3c5 misc: ignore Polyfill coverage 2022-05-05 21:16:35 +02:00
Romain Canon
3ef4276649 test: add tests to check parsing of types followed by description 2022-05-05 19:49:45 +02:00
Maximilian Bösing
64e0a2d5ac
feat: allow psalm and phpstan prefix in docblocks
The following annotations are now properly handled: `@psalm-param`, 
`@phpstan-param`, `@psalm-return` and `@phpstan-return`.

If one of those found along with a basic `@param` or `@return` 
annotation, it will override the basic value.
2022-05-05 19:40:11 +02:00
dependabot[bot]
34f519b583 chore(deps): bump actions/cache from 3.0.1 to 3.0.2
Bumps [actions/cache](https://github.com/actions/cache) from 3.0.1 to 3.0.2.
- [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/v3.0.1...v3.0.2)

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

Signed-off-by: dependabot[bot] <support@github.com>
2022-05-04 19:50:41 +02:00
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