github CuyZ/Valinor 0.13.0

latest releases: 1.14.1, 1.14.0, 1.13.0...
2 years ago

Notable changes

Reworking of 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.

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';
    }
}

Handle numeric-string type

Kudos to @sergkash7 for this new feature!

The new numeric-string type can be used in docblocks.

It will accept any string value that is also numeric.

(new MapperBuilder())->mapper()->map('numeric-string', '42'); // ✅
(new MapperBuilder())->mapper()->map('numeric-string', 'foo'); // ❌

Better mapping error message

Kudos to @Slamdunk for this!

The message of the exception will now contain more information, especially the total number of errors and the source that was given to the mapper. This change aims to have a better understanding of what is wrong when debugging.

Before:

Could not map type `array{foo: string, bar: int}` with the given source.

After:

Could not map type `array{foo: string, bar: int}`. An error occurred at path bar: Value 'some other string' does not match type `int`.

Full list of changes

⚠ BREAKING CHANGES

  • Rework messages body and parameters features (ad1207)

Features

  • Allow to declare parameter for message (f61eb5)
  • Display more information in mapping error message (9c1e7c)
  • Handle numeric string type (96a493)
  • Make MessagesFlattener countable (2c1c7c)

Bug Fixes

  • Handle native attribute on promoted parameter (897ca9)

Other

  • Add fixed value for root node path (0b37b4)
  • Remove types stringable behavior (b47a1b)

Don't miss a new Valinor release

NewReleases is sending notifications on new releases.