Skip to content

Commit

Permalink
Merge branch 'master' into json
Browse files Browse the repository at this point in the history
  • Loading branch information
romm authored Sep 16, 2024
2 parents c549a44 + 304db39 commit 9dd9762
Show file tree
Hide file tree
Showing 46 changed files with 844 additions and 679 deletions.
1 change: 1 addition & 0 deletions docs/pages/project/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Below are listed the changelogs for all released versions of the library.

## Version 1

- [`1.13.0` — 2nd of September 2024](changelog/version-1.13.0.md)
- [`1.12.0` — 4th of April 2024](changelog/version-1.12.0.md)
- [`1.11.0` — 27th of March 2024](changelog/version-1.11.0.md)
- [`1.10.0` — 12th of March 2024](changelog/version-1.10.0.md)
Expand Down
114 changes: 114 additions & 0 deletions docs/pages/project/changelog/version-1.13.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Changelog 1.13.0 — 2nd of September 2024

!!! info inline end "[See release on GitHub]"
[See release on GitHub]: https://github.com/CuyZ/Valinor/releases/tag/1.13.0

## Notable changes

**Microseconds support for timestamp format**

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

```php
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:

```php
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:

```php
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](https://github.com/CuyZ/Valinor/commit/02bd2e5e0f0e7d4daf234852464085bcdd1a0eb2))
* Add support for `value-of<BackedEnum>` type ([b1017c](https://github.com/CuyZ/Valinor/commit/b1017ce55729f0698c7629d57a3d3a30c0f9bff3))
* Improve object constructors parameters types inferring ([2150dc](https://github.com/CuyZ/Valinor/commit/2150dcad4ce821bfe36c3718346ccc412e37832a))

### Bug Fixes

* Allow any constant in class constant type ([694275](https://github.com/CuyZ/Valinor/commit/6942755865f91c80af8ea97fde2faa390478a6b8))
* Allow docblock for transformer callable type ([69e0e3](https://github.com/CuyZ/Valinor/commit/69e0e3a5f1de6a5eedcfa4125d8639be91f0c303))
* Do not override invalid variadic parameter type ([c5860f](https://github.com/CuyZ/Valinor/commit/c5860f0e5b3f59f49900bfbb20ca4493916eca7a))
* Handle interface generics ([40e6fa](https://github.com/CuyZ/Valinor/commit/40e6fa340819961068b8be178e312a99c06cede2))
* Handle iterable objects as iterable during normalization ([436e3c](https://github.com/CuyZ/Valinor/commit/436e3c25532d5cf396b00354ec5459e812c2953e))
* Properly format empty object with JSON normalizer ([ba22b5](https://github.com/CuyZ/Valinor/commit/ba22b5233e80f0ffbbe9591a5099b9dd62715eb8))
* Properly handle nested local type aliases ([127839](https://github.com/CuyZ/Valinor/commit/1278392757a4e9dc9eee2ab642c5700e83ccf982))

### Other

* Exclude unneeded attributes in class/function definitions ([1803d0](https://github.com/CuyZ/Valinor/commit/1803d094f08b256c64535f4f86e32ab35a07bbf1))
* Improve mapping performance for nullable union type ([6fad94](https://github.com/CuyZ/Valinor/commit/6fad94a46785dfb853c11c241e3f60bcf6a85ede))
* Move "float type accepting integer value" logic in `Shell` ([047953](https://github.com/CuyZ/Valinor/commit/0479532fbc96fca35dcbfb4c1f5a9ef63e7625c5))
* Move setting values in shell ([84b1ff](https://github.com/CuyZ/Valinor/commit/84b1ffbc8190a709d752a9882f71f6f419ad0434))
* Reorganize type resolver services ([86fb7b](https://github.com/CuyZ/Valinor/commit/86fb7b6303b15b54da6ac02ca8a7008b23c8bcff))
29 changes: 11 additions & 18 deletions src/Library/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use CuyZ\Valinor\Definition\Repository\Reflection\ReflectionFunctionDefinitionRepository;
use CuyZ\Valinor\Mapper\ArgumentsMapper;
use CuyZ\Valinor\Mapper\Object\Factory\CacheObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\CollisionObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\SortingObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\ConstructorObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\DateTimeObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\DateTimeZoneObjectBuilderFactory;
Expand All @@ -37,7 +37,6 @@
use CuyZ\Valinor\Mapper\Tree\Builder\NodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\NullNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ObjectImplementations;
use CuyZ\Valinor\Mapper\Tree\Builder\FilteredObjectNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ScalarNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ShapedArrayNodeBuilder;
Expand Down Expand Up @@ -86,36 +85,36 @@ public function __construct(Settings $settings)
$this->factories = [
TreeMapper::class => fn () => new TypeTreeMapper(
$this->get(TypeParser::class),
$this->get(RootNodeBuilder::class)
$this->get(RootNodeBuilder::class),
$settings,
),

ArgumentsMapper::class => fn () => new TypeArgumentsMapper(
$this->get(FunctionDefinitionRepository::class),
$this->get(RootNodeBuilder::class)
$this->get(RootNodeBuilder::class),
$settings,
),

RootNodeBuilder::class => fn () => new RootNodeBuilder(
$this->get(NodeBuilder::class)
),

NodeBuilder::class => function () use ($settings) {
$listNodeBuilder = new ListNodeBuilder($settings->enableFlexibleCasting);
$arrayNodeBuilder = new ArrayNodeBuilder($settings->enableFlexibleCasting);
$listNodeBuilder = new ListNodeBuilder();
$arrayNodeBuilder = new ArrayNodeBuilder();

$builder = new CasterNodeBuilder([
ListType::class => $listNodeBuilder,
NonEmptyListType::class => $listNodeBuilder,
ArrayType::class => $arrayNodeBuilder,
NonEmptyArrayType::class => $arrayNodeBuilder,
IterableType::class => $arrayNodeBuilder,
ShapedArrayType::class => new ShapedArrayNodeBuilder($settings->allowSuperfluousKeys),
ScalarType::class => new ScalarNodeBuilder($settings->enableFlexibleCasting),
ShapedArrayType::class => new ShapedArrayNodeBuilder(),
ScalarType::class => new ScalarNodeBuilder(),
NullType::class => new NullNodeBuilder(),
ObjectType::class => new ObjectNodeBuilder(
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class),
$this->get(FilteredObjectNodeBuilder::class),
$settings->enableFlexibleCasting,
),
]);

Expand All @@ -125,14 +124,10 @@ public function __construct(Settings $settings)
$builder,
$this->get(ObjectImplementations::class),
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class),
$this->get(FilteredObjectNodeBuilder::class),
new FunctionsContainer(
$this->get(FunctionDefinitionRepository::class),
$settings->customConstructors
),
$settings->enableFlexibleCasting,
$settings->allowSuperfluousKeys,
);

$builder = new CasterProxyNodeBuilder($builder);
Expand All @@ -148,13 +143,11 @@ public function __construct(Settings $settings)
);
}

$builder = new StrictNodeBuilder($builder, $settings->allowPermissiveTypes, $settings->enableFlexibleCasting);
$builder = new StrictNodeBuilder($builder);

return new ErrorCatcherNodeBuilder($builder, $settings->exceptionFilter);
},

FilteredObjectNodeBuilder::class => fn () => new FilteredObjectNodeBuilder($settings->allowSuperfluousKeys),

ObjectImplementations::class => fn () => new ObjectImplementations(
new FunctionsContainer(
$this->get(FunctionDefinitionRepository::class),
Expand All @@ -173,7 +166,7 @@ public function __construct(Settings $settings)
$factory = new ConstructorObjectBuilderFactory($factory, $settings->nativeConstructors, $constructors);
$factory = new DateTimeZoneObjectBuilderFactory($factory, $this->get(FunctionDefinitionRepository::class));
$factory = new DateTimeObjectBuilderFactory($factory, $settings->supportedDateFormats, $this->get(FunctionDefinitionRepository::class));
$factory = new CollisionObjectBuilderFactory($factory);
$factory = new SortingObjectBuilderFactory($factory);

if (! $settings->allowPermissiveTypes) {
$factory = new StrictTypesObjectBuilderFactory($factory);
Expand Down
35 changes: 22 additions & 13 deletions src/Mapper/Object/Arguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use IteratorAggregate;
use Traversable;

use function array_keys;
use function array_map;
use function array_values;
use function count;

/**
* @internal
Expand All @@ -22,44 +24,51 @@
*/
final class Arguments implements IteratorAggregate, Countable
{
/** @var Argument[] */
private array $arguments;
/** @var array<string, Argument> */
private array $arguments = [];

public function __construct(Argument ...$arguments)
{
$this->arguments = $arguments;
foreach ($arguments as $argument) {
$this->arguments[$argument->name()] = $argument;
}
}

public static function fromParameters(Parameters $parameters): self
{
return new self(...array_map(
fn (ParameterDefinition $parameter) => Argument::fromParameter($parameter),
array_values([...$parameters])
[...$parameters],
));
}

public static function fromProperties(Properties $properties): self
{
return new self(...array_map(
fn (PropertyDefinition $property) => Argument::fromProperty($property),
array_values([...$properties])
[...$properties],
));
}

public function at(int $index): Argument
{
return $this->arguments[$index];
return array_values($this->arguments)[$index];
}

public function has(string $name): bool
/**
* @return list<string>
*/
public function names(): array
{
foreach ($this->arguments as $argument) {
if ($argument->name() === $name) {
return true;
}
}
return array_keys($this->arguments);
}

return false;
/**
* @return array<string, Argument>
*/
public function toArray(): array
{
return $this->arguments;
}

public function count(): int
Expand Down
Loading

0 comments on commit 9dd9762

Please sign in to comment.