diff --git a/composer.json b/composer.json index dd0012f..3954b86 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ "require": { "php": "^8.2", "symfony/framework-bundle": "^5.0|^6.0|^7.0", - "symfony/serializer": "^5.1.8|^6.0", + "symfony/serializer": "^5.1.8|^6.0|^7.0", "beberlei/assert": "^3.2.7" }, "config": { diff --git a/src/Normalizer/AbsolutePercentValueNormalizer.php b/src/Normalizer/AbsolutePercentValueNormalizer.php index 570967a..37039ca 100644 --- a/src/Normalizer/AbsolutePercentValueNormalizer.php +++ b/src/Normalizer/AbsolutePercentValueNormalizer.php @@ -22,7 +22,7 @@ class AbsolutePercentValueNormalizer implements NormalizerInterface, Denormalize * @return mixed[] * @throws InvalidArgumentException */ - public function normalize($object, string $format = null, array $context = []): array + public function normalize(mixed $object, ?string $format = null, array $context = []): array { if (!$object instanceof AbsolutePercentValue) { throw new InvalidArgumentException(sprintf( @@ -39,25 +39,27 @@ public function normalize($object, string $format = null, array $context = []): /** * @param mixed $data + * @param array $context */ - public function supportsNormalization($data, string $format = null): bool + public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool { return $data instanceof AbsolutePercentValue; } /** - * @param mixed $data - * @param array $context - * * @throws NotNormalizableValueException */ - public function denormalize($data, string $type, string $format = null, array $context = []): ?AbsolutePercentValue - { + public function denormalize( + mixed $data, + string $type, + ?string $format = null, + array $context = [] + ): mixed { try { if ('' === $data || null === $data) { - return null; + throw new NotNormalizableValueException(); } - return new AbsolutePercentValue($data['type'], $data['value']); + return new AbsolutePercentValue($data['type'], $data['value']); } catch (\Exception $e) { throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e); } @@ -65,9 +67,22 @@ public function denormalize($data, string $type, string $format = null, array $c /** * @param mixed $data + * @param array $context */ - public function supportsDenormalization($data, string $type, string $format = null): bool - { + public function supportsDenormalization( + mixed $data, + string $type, + ?string $format = null, + array $context = [] + ): bool { return $type === AbsolutePercentValue::class; } + + /** + * @return array + */ + public function getSupportedTypes(?string $format): array + { + return [AbsolutePercentValue::class => false]; + } } diff --git a/tests/Normalizer/AbsolutePercentValueNormalizerTest.php b/tests/Normalizer/AbsolutePercentValueNormalizerTest.php index 0c0e426..c3289bb 100644 --- a/tests/Normalizer/AbsolutePercentValueNormalizerTest.php +++ b/tests/Normalizer/AbsolutePercentValueNormalizerTest.php @@ -65,10 +65,10 @@ public function testSupportsDenormalization(string $type, bool $result): void /** * @dataProvider providerTestDenormalize */ - public function testDenormalizeNull(?string $data): void + public function testDenormalizeFailure(mixed $data): void { - $value = $this->valueNormalizer->denormalize($data, AbsolutePercentValue::class); - self::assertNull($value); + $this->expectException(NotNormalizableValueException::class); + $this->valueNormalizer->denormalize($data, AbsolutePercentValue::class); } /** @return iterable */ @@ -76,6 +76,7 @@ public function providerTestDenormalize(): iterable { yield ['']; yield [null]; + yield [['type' => AbsolutePercentValue::TYPE_ABSOLUTE]]; } public function testDenormalizeSuccess(): void @@ -83,15 +84,7 @@ public function testDenormalizeSuccess(): void $data = ['type' => AbsolutePercentValue::TYPE_ABSOLUTE, 'value' => '2000']; $value = $this->valueNormalizer->denormalize($data, AbsolutePercentValue::class); - self::assertNotNull($value); self::assertSame($data['type'], $value->getType()); self::assertSame($data['value'], $value->getValue()); } - - public function testDenormalizeFailure(): void - { - $data = ['type' => AbsolutePercentValue::TYPE_ABSOLUTE]; - $this->expectException(NotNormalizableValueException::class); - $this->valueNormalizer->denormalize($data, AbsolutePercentValue::class); - } }