Skip to content

[JsonEncoder] Add object to normalize method #59281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*/
class RangeNormalizer implements NormalizerInterface, DenormalizerInterface
{
public function normalize(mixed $denormalized, array $options = []): string
public function normalize(mixed $denormalized, object $object, array $options = []): string
{
return $denormalized[0].'..'.$denormalized[1];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/JsonEncoder/Encode/EncoderGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private function createDataModel(Type $type, DataAccessorInterface $accessor, ar
foreach ($propertyMetadata->getNormalizers() as $normalizer) {
if (\is_string($normalizer)) {
$normalizerServiceAccessor = new FunctionDataAccessor('get', [new ScalarDataAccessor($normalizer)], new VariableDataAccessor('normalizers'));
$propertyAccessor = new FunctionDataAccessor('normalize', [$propertyAccessor, new VariableDataAccessor('options')], $normalizerServiceAccessor);
$propertyAccessor = new FunctionDataAccessor('normalize', [$propertyAccessor, $accessor, new VariableDataAccessor('options')], $normalizerServiceAccessor);

continue;
}
Expand All @@ -168,7 +168,7 @@ private function createDataModel(Type $type, DataAccessorInterface $accessor, ar
$functionName = !$functionReflection->getClosureScopeClass()
? $functionReflection->getName()
: \sprintf('%s::%s', $functionReflection->getClosureScopeClass()->getName(), $functionReflection->getName());
$arguments = $functionReflection->isUserDefined() ? [$propertyAccessor, new VariableDataAccessor('options')] : [$propertyAccessor];
$arguments = $functionReflection->isUserDefined() ? [$propertyAccessor, $accessor, new VariableDataAccessor('options')] : [$propertyAccessor];

$propertyAccessor = new FunctionDataAccessor($functionName, $arguments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class DateTimeNormalizer implements NormalizerInterface
{
public const FORMAT_KEY = 'date_time_format';

public function normalize(mixed $denormalized, array $options = []): string
public function normalize(mixed $denormalized, object $object, array $options = []): string
{
if (!$denormalized instanceof \DateTimeInterface) {
throw new InvalidArgumentException('The denormalized data must implement the "\DateTimeInterface".');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ interface NormalizerInterface
/**
* @param array<string, mixed> $options
*/
public function normalize(mixed $denormalized, array $options = []): mixed;
public function normalize(mixed $denormalized, object $object, array $options = []): mixed;

public static function getNormalizedType(): Type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public function testNormalize()

$this->assertEquals(
'2023-07-26T00:00:00+00:00',
$normalizer->normalize(new \DateTimeImmutable('2023-07-26', new \DateTimeZone('UTC')), []),
$normalizer->normalize(new \DateTimeImmutable('2023-07-26', new \DateTimeZone('UTC')), new \stdClass(), []),
);

$this->assertEquals(
'26/07/2023 00:00:00',
$normalizer->normalize((new \DateTimeImmutable('2023-07-26', new \DateTimeZone('UTC')))->setTime(0, 0), [DateTimeNormalizer::FORMAT_KEY => 'd/m/Y H:i:s']),
$normalizer->normalize((new \DateTimeImmutable('2023-07-26', new \DateTimeZone('UTC')))->setTime(0, 0), new \stdClass(), [DateTimeNormalizer::FORMAT_KEY => 'd/m/Y H:i:s']),
);
}

Expand All @@ -37,6 +37,6 @@ public function testThrowWhenInvalidDenormalized()
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The denormalized data must implement the "\DateTimeInterface".');

(new DateTimeNormalizer())->normalize(true, []);
(new DateTimeNormalizer())->normalize(true, new \stdClass(), []);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class BooleanStringNormalizer implements NormalizerInterface
{
public function normalize(mixed $data, array $options = []): mixed
public function normalize(mixed $data, object $object, array $options = []): mixed
{
return $data ? 'true' : 'false';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

final class DoubleIntAndCastToStringNormalizer implements NormalizerInterface
{
public function normalize(mixed $data, array $options = []): mixed
public function normalize(mixed $data, object $object, array $options = []): mixed
{
return (string) (2 * $options['scale'] * $data);
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.