Commit Graph

146 Commits

Author SHA1 Message Date
6889f95fed cs-fix 2022-11-08 12:31:37 +01:00
ba4d54e214 Add basic test 2022-11-08 12:31:37 +01:00
Romain Canon
752ad9d12e fix: handle scalar value casting in union types only in flexible mode 2022-11-07 01:15:46 +01:00
Romain Canon
92a41a1564 misc: transform missing source value to null in flexible mode 2022-11-07 01:15:46 +01:00
Romain Canon
034f1c51e1 fix: allow missing and null value for array node in flexible mode 2022-11-07 01:15:46 +01:00
Romain Canon
08fb0e17ba fix: allow missing value for shaped array nullable node in flexible mode 2022-11-07 01:15:46 +01:00
Romain Canon
25ce2188bb test: add test to check mapping with missing value for nullable node 2022-11-07 01:15:46 +01:00
Romain Canon
47ad4a1416 feat: add support for PHP 8.2 2022-10-19 13:44:19 +02:00
Romain Canon
c71d6aef94 fix: properly handle quote char in type definition 2022-10-19 13:36:36 +02:00
Nathan Boiron
0b8ca98a2c
misc: import namespace token parser inside library
The `TokenParser` is imported from the `doctrine/annotations` package in
order to reduce the coupling to this library, which will lead to its
removal from the dependencies in an upcoming version.
2022-10-06 13:40:11 +02:00
Romain Canon
1244c2d68f feat: add support for class constant type
This notation is mainly useful when several cases in constants of a
class share a common prefix.

```php
final class SomeClassWithConstants
{
    public const FOO = 1337;

    public const BAR = 'bar';

    public const BAZ = 'baz';
}

$mapper = (new MapperBuilder())->mapper();

$mapper->map('SomeClassWithConstants::BA*', 1337); // error
$mapper->map('SomeClassWithConstants::BA*', 'bar'); // ok
$mapper->map('SomeClassWithConstants::BA*', 'baz'); // ok
```
2022-10-04 22:15:02 +02:00
Romain Canon
37f96f101d fix: correctly handle type inferring during mapping
Solves an issue where a type was inferred for a union but would be
marked as invalid.
2022-10-04 22:15:02 +02:00
Romain Canon
69ebd19ee8 feat: add support for wildcard in enumeration type
This notation can be used when several cases in an enum share a common
prefix.

```php
enum SomeEnum: string
{
    case FOO = 'foo';
    case BAR = 'bar';
    case BAZ = 'baz';
}

$mapper = (new MapperBuilder())->mapper();

$mapper->map('SomeEnum::BA*', 'foo'); // error
$mapper->map('SomeEnum::BA*', 'bar'); // ok
$mapper->map('SomeEnum::BA*', 'baz'); // ok
```
2022-10-04 20:44:55 +02:00
Romain Canon
ad0f8fee17 misc: save type token symbols during lexing 2022-10-04 20:44:55 +02:00
Romain Canon
212b77fd13 fix: improve scalar values casting
These changes lead to better performance, more precise casting in case
of "fixed types" and overall better error messages.
2022-09-29 13:52:06 +02:00
Romain Canon
cb87925aac feat: introduce utility class to build messages
The new class `\CuyZ\Valinor\Mapper\Tree\Message\MessageBuilder` can be
used to easily create an instance of (error) message.

This new straightforward way of creating messages leads to the
depreciation of `\CuyZ\Valinor\Mapper\Tree\Message\ThrowableMessage`.

```php
$message = MessageBuilder::newError('Some message / {some_parameter}.')
    ->withCode('some_code')
    ->withParameter('some_parameter', 'some_value')
    ->build();
```
2022-09-29 13:52:06 +02:00
Romain Canon
de8aa9f440 misc: remove unused code 2022-09-29 13:52:06 +02:00
Romain Canon
3ee526cb27 fix: fetch correct node value for children 2022-09-26 20:03:47 +02:00
Eduardo Dobay
c009ab98cc
fix: properly handle static anonymous functions
The `MethodObjectBuilder` was incorrectly used when a registered
constructor is a static anonymous functions — it was handled like a
static method closure `Class::method(...)` and would yield errors like
this:

```
Error: Call to undefined method 
stdClass::CuyZ\Valinor\Tests\Integration\Mapping\{closure}()
```

PHP Reflection does not provide any way of telling static functions and
closures of static methods apart, other than checking for the name
`{closure}`. We check that `{closure}` is actually the last part of the
fully-qualified name, instead of just checking that the string ends with
`{closure}`.
2022-09-24 20:01:53 +02:00
Lukáš Unger
0e8f12e5f7
fix: add return types for cache implementations
Fixes the following message reported by `symfony/error-handler`:

`User Deprecated: Method "Psr\SimpleCache\CacheInterface::get()" might
add "mixed" as a native return type declaration in the future. Do the
same in implementation "CuyZ\Valinor\Cache\..." now to avoid errors or
add an explicit @return annotation to suppress this message.`
2022-09-24 19:29:02 +02:00
Romain Canon
48208c1ed1 fix: correctly fetch file system cache entries
Method `\CuyZ\Valinor\Cache\FileSystemCache::get()` was not properly
looping on all delegates, leading to the values not being fetched from
the cache files and resulting in `null` (the default value) being
returned in some cases. Because of the following algorithm, the cache
entry was populated again, so the cache was not really working here.

```php
if ($this->cache->has($key)) {
    $entry = $this->cache->get($key);

    if ($entry) {
        return $entry;
    }
}

$class = $this->delegate->for($type);

$this->cache->set($key, $class);

return $class;
```
2022-09-01 12:26:32 +02:00
Romain Canon
11a7ea7252 feat: introduce helper method to describe supported date formats
```php
(new \CuyZ\Valinor\MapperBuilder())
    // Both `Cookie` and `ATOM` formats will be accepted
    ->supportDateFormats(DATE_COOKIE, DATE_ATOM)
    ->mapper()
    ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC');
```
2022-09-01 12:24:24 +02:00
Romain Canon
f232cc0636 feat!: introduce constructor for custom date formats
A new constructor can be registered to declare which format(s) are
supported during the mapping of a date object. By default, any valid
timestamp or ATOM-formatted value will be accepted.

```php
(new \CuyZ\Valinor\MapperBuilder())
    // Both COOKIE and ATOM formats will be accepted
    ->registerConstructor(
        new \CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor(DATE_COOKIE, DATE_ATOM)
    )
    ->mapper()
    ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC');
```

The previously very opinionated behaviour has been removed, but can be
temporarily used to help with the migration.

```php
(new \CuyZ\Valinor\MapperBuilder())
    ->registerConstructor(
        new \CuyZ\Valinor\Mapper\Object\BackwardCompatibilityDateTimeConstructor()
    )
    ->mapper()
    ->map(DateTimeInterface::class, 'Monday, 08-Nov-1971 13:37:42 UTC');
```
2022-09-01 12:24:24 +02:00
Romain Canon
3c4d29901a fix: prevent illegal characters in PSR-16 cache keys
@see https://www.php-fig.org/psr/psr-16/#12-definitions
2022-08-31 01:01:16 +02:00
Romain Canon
546fa5c6cf test: add missing test assertion 2022-08-31 01:01:16 +02:00
mtouellette
fd39aea2a7
fix: handle concurrent cache file creation 2022-08-30 22:06:53 +02:00
Romain Canon
bf445b5364 fix: allow trailing comma in shaped array
Allows the following syntax:

```php
/**
 * @var array{
 * 	   foo: string,
 *     bar: int,
 * }
 */
 ```
2022-08-30 21:20:28 +02:00
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
Romain Canon
4bc50e3e42 misc: add singleton usage of ClassStringType 2022-08-29 23:09:15 +02:00
Romain Canon
ec494cec48 misc: fetch attributes for function definition 2022-08-29 23:09:15 +02:00
Romain Canon
c37ac1e259 feat: handle abstract constructor registration
It is now possible to register a static method constructor that can be
inherited by a child class. The constructor will then be used correctly
to map the child class.

```php
abstract class ClassWithStaticConstructor
{
    public string $value;

    final private function __construct(string $value)
    {
        $this->value = $value;
    }

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

final class ChildClass extends ClassWithStaticConstructor {}

(new MapperBuilder())
    // The constructor can be used for every child of the parent class
    ->registerConstructor(ClassWithStaticConstructor::from(...))
    ->mapper()
    ->map(ChildClass::class, 'foo');
```
2022-08-29 23:09:15 +02:00
Romain Canon
73b62241b6 fix: handle inherited private constructor in class definition 2022-08-29 23:09:15 +02:00
Romain Canon
2b46a60f37 misc: extract native constructor object builder 2022-08-29 23:09:15 +02:00
Romain Canon
57849c92e7 misc: change ObjectBuilderFactory::for return signature 2022-08-29 23:09:15 +02:00
Romain Canon
a401c2a2d6 fix: handle invalid nodes recursively 2022-08-29 23:09:15 +02:00
Romain Canon
2540741171 fix: handle classes in a case-sensitive way in type parser 2022-08-29 23:09:15 +02:00
Romain Canon
6414e9cf14 misc: refactor arguments instantiation 2022-08-29 23:09:15 +02:00
Romain Canon
b3cb5927e9 fix: detect invalid constructor handle type
An exception will now be thrown when a constructor is registered for
an invalid type: something other than a class.
2022-08-29 23:09:15 +02:00
Romain Canon
ae7ddcf3ca fix: properly handle callable objects of the same class
Using two instances of the same class implementing the `__invoke()`
method in one of the mapper builder methods will now be properly handled
by the library
2022-08-29 23:09:15 +02:00
Radhi Guennichi
897ca9b65e
fix: handle native attribute on promoted parameter
Handles race condition when the attribute is affected to a property or 
parameter that was promoted, in this case the attribute will be applied
to both `ParameterReflection` and `PropertyReflection`, but the target
argument inside the attribute class is configured to support only one of
them (parameter or property).

More details: https://wiki.php.net/rfc/constructor_promotion#attributes
2022-07-31 15:42:58 +02:00
Romain Canon
17328d86db test: use exception expectation API for mapping error test 2022-07-27 08:18:41 +02:00
Filippo Tessarotto
9c1e7c928b
feat: display more information in mapping error message
The message will now display the source and the number of errors, and
even the original error message if only one error was encountered.
2022-07-26 22:55:32 +02:00
Romain Canon
2c1c7cf38a feat: make MessagesFlattener countable 2022-07-26 19:39:14 +02:00
Romain Canon
0b37b48c60 misc: add fixed value for root node path
The path is no longer an empty string, the string `*root*` is now
returned.
2022-07-26 19:28:46 +02:00
Romain Canon
f61eb553fa feat: allow to declare parameter for message
The parameter can then be used with a placeholder inside the body of the
message.
2022-07-26 19:23:44 +02:00
sergkash7
96a493469c
feat: handle numeric string type
The new `numeric-string` type can be used in docblocks.

It will accept any string value that is also numeric.
2022-07-25 22:34:05 +02:00
Romain Canon
ad1207153e feat!: rework messages body and parameters features
The `\CuyZ\Valinor\Mapper\Tree\Message\Message` interface is no longer
a `Stringable`, however it defines a new method `body` that must return
the body of the message, which can contain placeholders that will be
replaced by parameters.

These parameters can now be defined by implementing the interface
`\CuyZ\Valinor\Mapper\Tree\Message\HasParameters`.

This leads to the deprecation of the no longer needed interface
`\CuyZ\Valinor\Mapper\Tree\Message\TranslatableMessage` which had a
confusing name.

```php
final class SomeException
    extends DomainException
    implements ErrorMessage, HasParameters, HasCode
{
    private string $someParameter;

    public function __construct(string $someParameter)
    {
        parent::__construct();

        $this->someParameter = $someParameter;
    }

    public function body() : string
    {
        return 'Some message / {some_parameter} / {source_value}';
    }

    public function parameters(): array
    {
        return [
            'some_parameter' => $this->someParameter,
        ];
    }

    public function code() : string
    {
        // A unique code that can help to identify the error
        return 'some_unique_code';
    }
}
```
2022-07-25 22:05:31 +02:00
Romain Canon
703ba55ada test: use FakeReflector instead of anonymous class 2022-07-25 22:05:31 +02:00
Romain Canon
b47a1bbb5d misc: remove types stringable behavior 2022-07-13 21:44:07 +02:00
Romain Canon
d3b1dcb64e feat!: refactor tree node API
The class `\CuyZ\Valinor\Mapper\Tree\Node` has been refactored to remove
access to unwanted methods that were not supposed to be part of the
public API. Below are a list of all changes:

- New methods `$node->sourceFilled()` and `$node->sourceValue()` allow
  accessing the source value.

- The method `$node->value()` has been renamed to `$node->mappedValue()`
  and will throw an exception if the node is not value.

- The method `$node->type()` now returns a string.

- The methods `$message->name()`, `$message->path()`, `$message->type()`
  and `$message->value()` have been deprecated in favor of the new
  method `$message->node()`.

- The message parameter `{original_value}` has been deprecated in favor
  of `{source_value}`.
2022-07-10 19:28:36 +02:00