Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions src/Middleware/TransformMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Patchlevel\Hydrator\DenormalizationFailure;
use Patchlevel\Hydrator\Metadata\ClassMetadata;
use Patchlevel\Hydrator\NormalizationFailure;
use Patchlevel\Hydrator\Normalizer\ContextAwareNormalizer;
use Patchlevel\Hydrator\TypeMismatch;
use ReflectionParameter;
use Throwable;
Expand Down Expand Up @@ -62,13 +61,8 @@

if ($propertyMetadata->normalizer) {
try {
if ($propertyMetadata->normalizer instanceof ContextAwareNormalizer) {
/** @psalm-suppress MixedAssignment */
$value = $propertyMetadata->normalizer->denormalize($data[$propertyMetadata->fieldName], $context);
} else {
/** @psalm-suppress MixedAssignment */
$value = $propertyMetadata->normalizer->denormalize($data[$propertyMetadata->fieldName]);
}
/** @psalm-suppress MixedAssignment */
$value = $propertyMetadata->normalizer->denormalize($data[$propertyMetadata->fieldName], $context);
} catch (Throwable $e) {
throw new DenormalizationFailure(
$metadata->className,
Expand Down Expand Up @@ -113,24 +107,17 @@

$this->callStack[$objectId] = $object::class;

try {

Check warning on line 110 in src/Middleware/TransformMiddleware.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "UnwrapFinally": @@ @@ throw new CircularReference($references); } $this->callStack[$objectId] = $object::class; - try { - $data = []; - foreach ($metadata->properties as $propertyMetadata) { - if ($propertyMetadata->normalizer) { - try { - /** @psalm-suppress MixedAssignment */ - $data[$propertyMetadata->fieldName] = $propertyMetadata->normalizer->normalize($propertyMetadata->getValue($object), $context); - } catch (CircularReference $e) { - throw $e; - } catch (Throwable $e) { - throw new NormalizationFailure($object::class, $propertyMetadata->propertyName, $propertyMetadata->normalizer::class, $e); - } - } else { - $data[$propertyMetadata->fieldName] = $propertyMetadata->getValue($object); + $data = []; + foreach ($metadata->properties as $propertyMetadata) { + if ($propertyMetadata->normalizer) { + try { + /** @psalm-suppress MixedAssignment */ + $data[$propertyMetadata->fieldName] = $propertyMetadata->normalizer->normalize($propertyMetadata->getValue($object), $context); + } catch (CircularReference $e) { + throw $e; + } catch (Throwable $e) { + throw new NormalizationFailure($object::class, $propertyMetadata->propertyName, $propertyMetadata->normalizer::class, $e); } + } else { + $data[$propertyMetadata->fieldName] = $propertyMetadata->getValue($object); } - } finally { - unset($this->callStack[$objectId]); } + unset($this->callStack[$objectId]); return $data; } /** @return array<string, ReflectionParameter> */
$data = [];

foreach ($metadata->properties as $propertyMetadata) {
if ($propertyMetadata->normalizer) {
try {
if ($propertyMetadata->normalizer instanceof ContextAwareNormalizer) {
/** @psalm-suppress MixedAssignment */
$data[$propertyMetadata->fieldName] = $propertyMetadata->normalizer->normalize(
$propertyMetadata->getValue($object),
$context,
);
} else {
/** @psalm-suppress MixedAssignment */
$data[$propertyMetadata->fieldName] = $propertyMetadata->normalizer->normalize(
$propertyMetadata->getValue($object),
);
}
/** @psalm-suppress MixedAssignment */
$data[$propertyMetadata->fieldName] = $propertyMetadata->normalizer->normalize(
$propertyMetadata->getValue($object),
$context,
);
} catch (CircularReference $e) {
throw $e;
} catch (Throwable $e) {
Expand Down
20 changes: 14 additions & 6 deletions src/Normalizer/ArrayNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public function __construct(
) {
}

/** @return array<array-key, mixed>|null */
public function normalize(mixed $value): array|null
/**
* @param array<string, mixed> $context
*
* @return array<array-key, mixed>|null
*/
public function normalize(mixed $value, array $context): array|null
{
if ($value === null) {
return null;
Expand All @@ -32,14 +36,18 @@ public function normalize(mixed $value): array|null
}

foreach ($value as &$item) {
$item = $this->normalizer->normalize($item);
$item = $this->normalizer->normalize($item, $context);
}

return $value;
}

/** @return array<array-key, mixed>|null */
public function denormalize(mixed $value): array|null
/**
* @param array<string, mixed> $context
*
* @return array<array-key, mixed>|null
*/
public function denormalize(mixed $value, array $context): array|null
{
if ($value === null) {
return null;
Expand All @@ -50,7 +58,7 @@ public function denormalize(mixed $value): array|null
}

foreach ($value as &$item) {
$item = $this->normalizer->denormalize($item);
$item = $this->normalizer->denormalize($item, $context);
}

return $value;
Expand Down
20 changes: 14 additions & 6 deletions src/Normalizer/ArrayShapeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ public function __construct(
) {
}

/** @return array<array-key, mixed>|null */
public function normalize(mixed $value): array|null
/**
* @param array<string, mixed> $context
*
* @return array<array-key, mixed>|null
*/
public function normalize(mixed $value, array $context): array|null
{
if ($value === null) {
return null;
Expand All @@ -39,14 +43,18 @@ public function normalize(mixed $value): array|null
continue;
}

$result[$field] = $normalizer->normalize($value[$field]);
$result[$field] = $normalizer->normalize($value[$field], $context);
}

return $result;
}

/** @return array<array-key, mixed>|null */
public function denormalize(mixed $value): array|null
/**
* @param array<string, mixed> $context
*
* @return array<array-key, mixed>|null
*/
public function denormalize(mixed $value, array $context): array|null
{
if ($value === null) {
return null;
Expand All @@ -63,7 +71,7 @@ public function denormalize(mixed $value): array|null
continue;
}

$result[$field] = $normalizer->denormalize($value[$field]);
$result[$field] = $normalizer->denormalize($value[$field], $context);
}

return $result;
Expand Down
22 changes: 0 additions & 22 deletions src/Normalizer/ContextAwareNormalizer.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/Normalizer/DateTimeImmutableNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct(
) {
}

public function normalize(mixed $value): string|null
/** @param array<string, mixed> $context */
public function normalize(mixed $value, array $context): string|null
{
if ($value === null) {
return null;
Expand All @@ -30,7 +31,8 @@ public function normalize(mixed $value): string|null
return $value->format($this->format);
}

public function denormalize(mixed $value): DateTimeImmutable|null
/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context): DateTimeImmutable|null
{
if ($value === null) {
return null;
Expand Down
6 changes: 4 additions & 2 deletions src/Normalizer/DateTimeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct(
) {
}

public function normalize(mixed $value): string|null
/** @param array<string, mixed> $context */
public function normalize(mixed $value, array $context): string|null
{
if ($value === null) {
return null;
Expand All @@ -30,7 +31,8 @@ public function normalize(mixed $value): string|null
return $value->format($this->format);
}

public function denormalize(mixed $value): DateTime|null
/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context): DateTime|null
{
if ($value === null) {
return null;
Expand Down
6 changes: 4 additions & 2 deletions src/Normalizer/DateTimeZoneNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
#[Attribute(Attribute::TARGET_PROPERTY)]
final class DateTimeZoneNormalizer implements Normalizer
{
public function normalize(mixed $value): string|null
/** @param array<string, mixed> $context */
public function normalize(mixed $value, array $context): string|null
{
if ($value === null) {
return null;
Expand All @@ -25,7 +26,8 @@ public function normalize(mixed $value): string|null
return $value->getName();
}

public function denormalize(mixed $value): DateTimeZone|null
/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context): DateTimeZone|null
{
if ($value === null) {
return null;
Expand Down
6 changes: 4 additions & 2 deletions src/Normalizer/EnumNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function __construct(
) {
}

public function normalize(mixed $value): mixed
/** @param array<string, mixed> $context */
public function normalize(mixed $value, array $context): mixed
{
if ($value === null) {
return null;
Expand All @@ -38,7 +39,8 @@ public function normalize(mixed $value): mixed
return $value->value;
}

public function denormalize(mixed $value): BackedEnum|null
/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context): BackedEnum|null
{
if ($value === null) {
return null;
Expand Down
16 changes: 12 additions & 4 deletions src/Normalizer/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@

interface Normalizer
{
/** @throws InvalidArgument */
public function normalize(mixed $value): mixed;
/**
* @param array<string, mixed> $context
*
* @throws InvalidArgument
*/
public function normalize(mixed $value, array $context): mixed;

/** @throws InvalidArgument */
public function denormalize(mixed $value): mixed;
/**
* @param array<string, mixed> $context
*
* @throws InvalidArgument
*/
public function denormalize(mixed $value, array $context): mixed;
}
6 changes: 3 additions & 3 deletions src/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use function is_array;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
final class ObjectNormalizer implements ContextAwareNormalizer, TypeAwareNormalizer, HydratorAwareNormalizer
final class ObjectNormalizer implements Normalizer, TypeAwareNormalizer, HydratorAwareNormalizer
{
private Hydrator|null $hydrator = null;

Expand All @@ -30,7 +30,7 @@ public function __construct(
*
* @return array<string, mixed>|null
*/
public function normalize(mixed $value, array $context = []): array|null
public function normalize(mixed $value, array $context): array|null
{
if (!$this->hydrator) {
throw new MissingHydrator();
Expand All @@ -50,7 +50,7 @@ public function normalize(mixed $value, array $context = []): array|null
}

/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context = []): object|null
public function denormalize(mixed $value, array $context): object|null
{
if (!$this->hydrator) {
throw new MissingHydrator();
Expand Down
6 changes: 4 additions & 2 deletions tests/Benchmark/Fixture/ProfileIdNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#[Attribute(Attribute::TARGET_PROPERTY)]
final class ProfileIdNormalizer implements Normalizer
{
public function normalize(mixed $value): string
/** @param array<string, mixed> $context */
public function normalize(mixed $value, array $context): string
{
if (!$value instanceof ProfileId) {
throw new InvalidArgumentException();
Expand All @@ -22,7 +23,8 @@ public function normalize(mixed $value): string
return $value->toString();
}

public function denormalize(mixed $value): ProfileId|null
/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context): ProfileId|null
{
if ($value === null) {
return null;
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Fixture/EmailNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#[Attribute(Attribute::TARGET_PROPERTY)]
final class EmailNormalizer implements Normalizer
{
public function normalize(mixed $value): string
/** @param array<string, mixed> $context */
public function normalize(mixed $value, array $context): string
{
if (!$value instanceof Email) {
throw new InvalidArgumentException();
Expand All @@ -23,7 +24,8 @@ public function normalize(mixed $value): string
return $value->toString();
}

public function denormalize(mixed $value): Email|null
/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context): Email|null
{
if ($value === null) {
return null;
Expand Down
6 changes: 4 additions & 2 deletions tests/Unit/Fixture/IdNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(
) {
}

public function normalize(mixed $value): string|null
/** @param array<string, mixed> $context */
public function normalize(mixed $value, array $context): string|null
{
if ($value === null) {
return null;
Expand All @@ -39,7 +40,8 @@ public function normalize(mixed $value): string|null
return $value->toString();
}

public function denormalize(mixed $value): Id|null
/** @param array<string, mixed> $context */
public function denormalize(mixed $value, array $context): Id|null
{
if ($value === null) {
return null;
Expand Down
Loading
Loading