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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
37 changes: 26 additions & 11 deletions src/Normalizer/AbsolutePercentValueNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @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(
Expand All @@ -39,35 +39,50 @@

/**
* @param mixed $data
* @param array<mixed> $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<mixed> $context
*
* @throws NotNormalizableValueException
*/
public function denormalize($data, string $type, string $format = null, array $context = []): ?AbsolutePercentValue
{
public function denormalize(

Check failure on line 52 in src/Normalizer/AbsolutePercentValueNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.2)

Method AssoConnect\AbsolutePercentValueBundle\Normalizer\AbsolutePercentValueNormalizer::denormalize() cannot have ($type is class-string<TObject of object> ? TObject of object : mixed) as its return type - floats are not allowed.
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);
}
}

/**
* @param mixed $data
* @param array<mixed> $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<class-string, bool>
*/
public function getSupportedTypes(?string $format): array
{
return [AbsolutePercentValue::class => false];
}
}
15 changes: 4 additions & 11 deletions tests/Normalizer/AbsolutePercentValueNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,33 +65,26 @@ 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<mixed> */
public function providerTestDenormalize(): iterable
{
yield [''];
yield [null];
yield [['type' => AbsolutePercentValue::TYPE_ABSOLUTE]];
}

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);
}
}
Loading