Skip to content

Commit

Permalink
misc: refactor normalizer integration
Browse files Browse the repository at this point in the history
Better architecture for upcoming JSON normalizer
  • Loading branch information
romm committed Dec 25, 2023
1 parent 776dec4 commit 7ea600a
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 180 deletions.
26 changes: 13 additions & 13 deletions src/Library/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@
use CuyZ\Valinor\Mapper\TreeMapper;
use CuyZ\Valinor\Mapper\TypeArgumentsMapper;
use CuyZ\Valinor\Mapper\TypeTreeMapper;
use CuyZ\Valinor\Normalizer\FormatNormalizer;
use CuyZ\Valinor\Normalizer\Formatter\Formatter;
use CuyZ\Valinor\Normalizer\Formatter\FormatterFactory;
use CuyZ\Valinor\Normalizer\ArrayNormalizer;
use CuyZ\Valinor\Normalizer\Format;
use CuyZ\Valinor\Normalizer\Normalizer;
use CuyZ\Valinor\Normalizer\RecursiveNormalizer;
use CuyZ\Valinor\Normalizer\Transformer\KeyTransformersHandler;
use CuyZ\Valinor\Normalizer\Transformer\RecursiveTransformer;
use CuyZ\Valinor\Normalizer\Transformer\ValueTransformersHandler;
use CuyZ\Valinor\Type\ClassType;
use CuyZ\Valinor\Type\Parser\Factory\LexingTypeParserFactory;
Expand Down Expand Up @@ -184,7 +183,7 @@ public function __construct(Settings $settings)
return new CacheObjectBuilderFactory($factory, $cache);
},

RecursiveNormalizer::class => fn () => new RecursiveNormalizer(
RecursiveTransformer::class => fn () => new RecursiveTransformer(
$this->get(ClassDefinitionRepository::class),
new ValueTransformersHandler(
$this->get(FunctionDefinitionRepository::class),
Expand All @@ -196,6 +195,10 @@ public function __construct(Settings $settings)
array_keys($settings->transformerAttributes),
),

ArrayNormalizer::class => fn () => new ArrayNormalizer(
$this->get(RecursiveTransformer::class),
),

ClassDefinitionRepository::class => fn () => new CacheClassDefinitionRepository(
new ReflectionClassDefinitionRepository(
$this->get(TypeParserFactory::class),
Expand Down Expand Up @@ -249,17 +252,14 @@ public function argumentsMapper(): ArgumentsMapper
}

/**
* @template T
* @template T of Normalizer
*
* @param FormatterFactory<Formatter<T>> $formatterFactory
* @return Normalizer<T>
* @param Format<T> $format
* @return T
*/
public function normalizer(FormatterFactory $formatterFactory): Normalizer
public function normalizer(Format $format): Normalizer
{
return new FormatNormalizer(
$formatterFactory,
$this->get(RecursiveNormalizer::class),
);
return $this->get($format->type());
}

public function cacheWarmupService(): RecursiveCacheWarmupService
Expand Down
16 changes: 6 additions & 10 deletions src/MapperBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
use CuyZ\Valinor\Mapper\ArgumentsMapper;
use CuyZ\Valinor\Mapper\Tree\Message\ErrorMessage;
use CuyZ\Valinor\Mapper\TreeMapper;
use CuyZ\Valinor\Normalizer\Formatter\Formatter;
use CuyZ\Valinor\Normalizer\Formatter\FormatterFactory;
use CuyZ\Valinor\Normalizer\Format;
use CuyZ\Valinor\Normalizer\Normalizer;
use Psr\SimpleCache\CacheInterface;
use Throwable;
Expand Down Expand Up @@ -552,17 +551,14 @@ public function argumentsMapper(): ArgumentsMapper
}

/**
* Returns a normalizer instance. To see list of available formats, check
* out: @see \CuyZ\Valinor\Normalizer\Format
* @template T of Normalizer
*
* @template T
*
* @param FormatterFactory<Formatter<T>> $formatterFactory
* @return Normalizer<T>
* @param Format<T> $format
* @return T
*/
public function normalizer(FormatterFactory $formatterFactory): Normalizer
public function normalizer(Format $format): Normalizer
{
return $this->container()->normalizer($formatterFactory);
return $this->container()->normalizer($format);
}

public function __clone()
Expand Down
49 changes: 49 additions & 0 deletions src/Normalizer/ArrayNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace CuyZ\Valinor\Normalizer;

use CuyZ\Valinor\Normalizer\Transformer\RecursiveTransformer;

use function array_map;
use function is_array;
use function is_iterable;
use function iterator_to_array;

/**
* @api
*
* @implements Normalizer<array<mixed>|scalar|null>
*/
final class ArrayNormalizer implements Normalizer
{
public function __construct(
private RecursiveTransformer $transformer,
) {}

public function normalize(mixed $value): mixed
{
$value = $this->transformer->transform($value);

return $this->normalizeIterator($value);
}

/**
* @param iterable<mixed>|scalar|null $value
* @return array<mixed>|scalar|null
*/
private function normalizeIterator(mixed $value): mixed
{
if (is_iterable($value)) {
if (! is_array($value)) {
$value = iterator_to_array($value);
}

// PHP8.1 First-class callable syntax
$value = array_map([$this, 'normalizeIterator'], $value);
}

return $value;
}
}
27 changes: 22 additions & 5 deletions src/Normalizer/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace CuyZ\Valinor\Normalizer;

use CuyZ\Valinor\Normalizer\Formatter\ArrayFormatterFactory;

/** @api */
/**
* @api
*
* @template T of Normalizer
*/
final class Format
{
/**
Expand Down Expand Up @@ -42,9 +44,24 @@ final class Format
* // ],
* // ];
* ```
*
* @return self<ArrayNormalizer>
*/
public static function array(): self
{
return new self(ArrayNormalizer::class);
}

/**
* @param class-string<T> $type
*/
private function __construct(private string $type) {}

/**
* @return class-string<T>
*/
public static function array(): ArrayFormatterFactory
public function type(): string
{
return new ArrayFormatterFactory();
return $this->type;
}
}
30 changes: 0 additions & 30 deletions src/Normalizer/FormatNormalizer.php

This file was deleted.

34 changes: 0 additions & 34 deletions src/Normalizer/Formatter/ArrayFormatter.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Normalizer/Formatter/ArrayFormatterFactory.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/Normalizer/Formatter/Formatter.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Normalizer/Formatter/FormatterFactory.php

This file was deleted.

Loading

0 comments on commit 7ea600a

Please sign in to comment.