Skip to content

1.13.0

Latest
Compare
Choose a tag to compare
@romm romm released this 02 Sep 12:59
· 6 commits to master since this release

Notable changes

Microseconds support for timestamp format

Prior to this patch, this would require a custom constructor in the form of:

static fn(float | int $timestamp): DateTimeImmutable => new
    DateTimeImmutable(sprintf("@%d", $timestamp)),

This bypasses the datetime format support of Valinor entirely. This is required because the library does not support floats as valid DateTimeInterface input values.

This commit adds support for floats and registers timestamp.microseconds (U.u) as a valid default format.

Support for value-of<BackedEnum> type

This type can be used as follows:

enum Suit: string
{
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

$suit = (new \CuyZ\Valinor\MapperBuilder())
    ->mapper()
    ->map('value-of<Suit>', 'D');

// $suit === 'D'

Object constructors parameters types inferring improvements

The collision system that checks object constructors parameters types is now way more clever, as it no longer checks for parameters' names only. Types are now also checked, and only true collision will be detected, for instance when two constructors share a parameter with the same name and type.

Note that when two parameters share the same name, the following type priority operates:

  1. Non-scalar type
  2. Integer type
  3. Float type
  4. String type
  5. Boolean type

With this change, the code below is now valid:

final readonly class Money
{
    private function __construct(
        public int $value,
    ) {}

    #[\CuyZ\Valinor\Mapper\Object\Constructor]
    public static function fromInt(int $value): self
    {
        return new self($value);
    }

    #[\CuyZ\Valinor\Mapper\Object\Constructor]
    public static function fromString(string $value): self
    {
        if (! preg_match('/^\d+€$/', $value)) {
            throw new \InvalidArgumentException('Invalid money format');
        }

        return new self((int)rtrim($value, ''));
    }
}

$mapper = (new \CuyZ\Valinor\MapperBuilder())->mapper();

$mapper->map(Money::class, 42); // ✅
$mapper->map(Money::class, '42€'); // ✅

Features

  • Add microseconds support to timestamp format (02bd2e)
  • Add support for value-of<BackedEnum> type (b1017c)
  • Improve object constructors parameters types inferring (2150dc)

Bug Fixes

  • Allow any constant in class constant type (694275)
  • Allow docblock for transformer callable type (69e0e3)
  • Do not override invalid variadic parameter type (c5860f)
  • Handle interface generics (40e6fa)
  • Handle iterable objects as iterable during normalization (436e3c)
  • Properly format empty object with JSON normalizer (ba22b5)
  • Properly handle nested local type aliases (127839)

Other

  • Exclude unneeded attributes in class/function definitions (1803d0)
  • Improve mapping performance for nullable union type (6fad94)
  • Move "float type accepting integer value" logic in Shell (047953)
  • Move setting values in shell (84b1ff)
  • Reorganize type resolver services (86fb7b)