Skip to content

[Serializer] Correct all implementations of the NormalizerInterface to have the correct method signature #59496

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

Merged
merged 1 commit into from
Feb 7, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ final class FlattenExceptionNormalizer implements DenormalizerInterface, Normali
{
use NormalizerAwareTrait;

public function normalize(mixed $object, ?string $format = null, array $context = []): array
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
return [
'message' => $object->getMessage(),
'code' => $object->getCode(),
'headers' => $object->getHeaders(),
'class' => $object->getClass(),
'file' => $object->getFile(),
'line' => $object->getLine(),
'previous' => null === $object->getPrevious() ? null : $this->normalize($object->getPrevious(), $format, $context),
'status' => $object->getStatusCode(),
'status_text' => $object->getStatusText(),
'trace' => $object->getTrace(),
'trace_as_string' => $object->getTraceAsString(),
'message' => $data->getMessage(),
'code' => $data->getCode(),
'headers' => $data->getHeaders(),
'class' => $data->getClass(),
'file' => $data->getFile(),
'line' => $data->getLine(),
'previous' => null === $data->getPrevious() ? null : $this->normalize($data->getPrevious(), $format, $context),
'status' => $data->getStatusCode(),
'status_text' => $data->getStatusText(),
'trace' => $data->getTrace(),
'trace_as_string' => $data->getTraceAsString(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public function getSupportedTypes(?string $format): array
return $this->normalizer->getSupportedTypes($format);
}

public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if (!$this->normalizer instanceof NormalizerInterface) {
throw new \BadMethodCallException(\sprintf('The "%s()" method cannot be called as nested normalizer doesn\'t implements "%s".', __METHOD__, NormalizerInterface::class));
}

$startTime = microtime(true);
$normalized = $this->normalizer->normalize($object, $format, $context);
$normalized = $this->normalizer->normalize($data, $format, $context);
$time = microtime(true) - $startTime;

if ($traceId = ($context[TraceableSerializer::DEBUG_TRACE_ID] ?? null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ public function deserialize(mixed $data, string $type, string $format, array $co
return $result;
}

public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
$context[self::DEBUG_TRACE_ID] = $traceId = bin2hex(random_bytes(4));

$startTime = microtime(true);
$result = $this->serializer->normalize($object, $format, $context);
$result = $this->serializer->normalize($data, $format, $context);
$time = microtime(true) - $startTime;

$caller = $this->getCaller(__FUNCTION__, NormalizerInterface::class);

$this->dataCollector->collectNormalize($traceId, $object, $format, $context, $time, $caller, $this->serializerName);
$this->dataCollector->collectNormalize($traceId, $data, $format, $context, $time, $caller, $this->serializerName);

return $result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function supportsNormalization(mixed $data, ?string $format = null, array
return \is_object($data) && !$data instanceof \Traversable;
}

public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
$context['_read_attributes'] = true;

Expand All @@ -164,14 +164,14 @@ public function normalize(mixed $object, ?string $format = null, array $context

$this->validateCallbackContext($context);

if ($this->isCircularReference($object, $context)) {
return $this->handleCircularReference($object, $format, $context);
if ($this->isCircularReference($data, $context)) {
return $this->handleCircularReference($data, $format, $context);
}

$data = [];
$normalizedData = [];
$stack = [];
$attributes = $this->getAttributes($object, $format, $context);
$class = ($this->objectClassResolver)($object);
$attributes = $this->getAttributes($data, $format, $context);
$class = ($this->objectClassResolver)($data);
$classMetadata = $this->classMetadataFactory?->getMetadataFor($class);
$attributesMetadata = $this->classMetadataFactory?->getMetadataFor($class)->getAttributesMetadata();
if (isset($context[self::MAX_DEPTH_HANDLER])) {
Expand All @@ -189,12 +189,12 @@ public function normalize(mixed $object, ?string $format = null, array $context
continue;
}

$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);
$attributeContext = $this->getAttributeNormalizationContext($data, $attribute, $context);

try {
$attributeValue = $attribute === $this->classDiscriminatorResolver?->getMappingForMappedObject($object)?->getTypeProperty()
? $this->classDiscriminatorResolver?->getTypeForMappedObject($object)
: $this->getAttributeValue($object, $attribute, $format, $attributeContext);
$attributeValue = $attribute === $this->classDiscriminatorResolver?->getMappingForMappedObject($data)?->getTypeProperty()
? $this->classDiscriminatorResolver?->getTypeForMappedObject($data)
: $this->getAttributeValue($data, $attribute, $format, $attributeContext);
} catch (UninitializedPropertyException|\Error $e) {
if (($context[self::SKIP_UNINITIALIZED_VALUES] ?? $this->defaultContext[self::SKIP_UNINITIALIZED_VALUES] ?? true) && $this->isUninitializedValueError($e)) {
continue;
Expand All @@ -203,17 +203,17 @@ public function normalize(mixed $object, ?string $format = null, array $context
}

if ($maxDepthReached) {
$attributeValue = $maxDepthHandler($attributeValue, $object, $attribute, $format, $attributeContext);
$attributeValue = $maxDepthHandler($attributeValue, $data, $attribute, $format, $attributeContext);
}

$stack[$attribute] = $this->applyCallbacks($attributeValue, $object, $attribute, $format, $attributeContext);
$stack[$attribute] = $this->applyCallbacks($attributeValue, $data, $attribute, $format, $attributeContext);
}

foreach ($stack as $attribute => $attributeValue) {
$attributeContext = $this->getAttributeNormalizationContext($object, $attribute, $context);
$attributeContext = $this->getAttributeNormalizationContext($data, $attribute, $context);

if (null === $attributeValue || \is_scalar($attributeValue)) {
$data = $this->updateData($data, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
$normalizedData = $this->updateData($normalizedData, $attribute, $attributeValue, $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
continue;
}

Expand All @@ -223,15 +223,15 @@ public function normalize(mixed $object, ?string $format = null, array $context

$childContext = $this->createChildContext($attributeContext, $attribute, $format);

$data = $this->updateData($data, $attribute, $this->serializer->normalize($attributeValue, $format, $childContext), $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
$normalizedData = $this->updateData($normalizedData, $attribute, $this->serializer->normalize($attributeValue, $format, $childContext), $class, $format, $attributeContext, $attributesMetadata, $classMetadata);
}

$preserveEmptyObjects = $context[self::PRESERVE_EMPTY_OBJECTS] ?? $this->defaultContext[self::PRESERVE_EMPTY_OBJECTS] ?? false;
if ($preserveEmptyObjects && !$data) {
if ($preserveEmptyObjects && !$normalizedData) {
return new \ArrayObject();
}

return $data;
return $normalizedData;
}

protected function instantiateObject(array &$data, string $class, array &$context, \ReflectionClass $reflectionClass, array|bool $allowedAttributes, ?string $format = null): object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public function getSupportedTypes(?string $format): array
];
}

public function normalize(mixed $object, ?string $format = null, array $context = []): int|string
public function normalize(mixed $data, ?string $format = null, array $context = []): int|string
{
if (!$object instanceof \BackedEnum) {
if (!$data instanceof \BackedEnum) {
throw new InvalidArgumentException('The data must belong to a backed enumeration.');
}

return $object->value;
return $data->value;
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getSupportedTypes(?string $format): array
];
}

public function normalize(mixed $object, ?string $format = null, array $context = []): array
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
if (\array_key_exists(self::PAYLOAD_FIELDS, $context)) {
$payloadFieldsToSerialize = $context[self::PAYLOAD_FIELDS];
Expand All @@ -59,7 +59,7 @@ public function normalize(mixed $object, ?string $format = null, array $context

$violations = [];
$messages = [];
foreach ($object as $violation) {
foreach ($data as $violation) {
$propertyPath = $this->nameConverter ? $this->nameConverter->normalize($violation->getPropertyPath(), null, $format, $context) : $violation->getPropertyPath();

$violationEntry = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function getSupportedTypes(?string $format): array
];
}

public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
return $object->normalize($this->serializer, $format, $context);
return $data->normalize($this->serializer, $format, $context);
}

public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ public function getSupportedTypes(?string $format): array
return self::SUPPORTED_TYPES;
}

public function normalize(mixed $object, ?string $format = null, array $context = []): string
public function normalize(mixed $data, ?string $format = null, array $context = []): string
{
if (!$object instanceof \SplFileInfo) {
if (!$data instanceof \SplFileInfo) {
throw new InvalidArgumentException('The object must be an instance of "\SplFileInfo".');
}

$mimeType = $this->getMimeType($object);
$splFileObject = $this->extractSplFileObject($object);
$mimeType = $this->getMimeType($data);
$splFileObject = $this->extractSplFileObject($data);

$data = '';
$splFileData = '';

$splFileObject->rewind();
while (!$splFileObject->eof()) {
$data .= $splFileObject->fgets();
$splFileData .= $splFileObject->fgets();
}

if ('text' === explode('/', $mimeType, 2)[0]) {
return \sprintf('data:%s,%s', $mimeType, rawurlencode($data));
return \sprintf('data:%s,%s', $mimeType, rawurlencode($splFileData));
}

return \sprintf('data:%s;base64,%s', $mimeType, base64_encode($data));
return \sprintf('data:%s;base64,%s', $mimeType, base64_encode($splFileData));
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ public function getSupportedTypes(?string $format): array
/**
* @throws InvalidArgumentException
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): string
public function normalize(mixed $data, ?string $format = null, array $context = []): string
{
if (!$object instanceof \DateInterval) {
if (!$data instanceof \DateInterval) {
throw new InvalidArgumentException('The object must be an instance of "\DateInterval".');
}

return $object->format($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]);
return $data->format($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]);
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,24 @@ public function getSupportedTypes(?string $format): array
/**
* @throws InvalidArgumentException
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): int|float|string
public function normalize(mixed $data, ?string $format = null, array $context = []): int|float|string
{
if (!$object instanceof \DateTimeInterface) {
if (!$data instanceof \DateTimeInterface) {
throw new InvalidArgumentException('The object must implement the "\DateTimeInterface".');
}

$dateTimeFormat = $context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY];
$timezone = $this->getTimezone($context);

if (null !== $timezone) {
$object = clone $object;
$object = $object->setTimezone($timezone);
$data = clone $data;
$data = $data->setTimezone($timezone);
}

return match ($context[self::CAST_KEY] ?? $this->defaultContext[self::CAST_KEY] ?? false) {
'int' => (int) $object->format($dateTimeFormat),
'float' => (float) $object->format($dateTimeFormat),
default => $object->format($dateTimeFormat),
'int' => (int) $data->format($dateTimeFormat),
'float' => (float) $data->format($dateTimeFormat),
default => $data->format($dateTimeFormat),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function getSupportedTypes(?string $format): array
/**
* @throws InvalidArgumentException
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): string
public function normalize(mixed $data, ?string $format = null, array $context = []): string
{
if (!$object instanceof \DateTimeZone) {
if (!$data instanceof \DateTimeZone) {
throw new InvalidArgumentException('The object must be an instance of "\DateTimeZone".');
}

return $object->getName();
return $data->getName();
}

public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ final class FormErrorNormalizer implements NormalizerInterface
public const TYPE = 'type';
public const CODE = 'status_code';

public function normalize(mixed $object, ?string $format = null, array $context = []): array
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
$data = [
$error = [
'title' => $context[self::TITLE] ?? 'Validation Failed',
'type' => $context[self::TYPE] ?? 'https://symfony.com/errors/form',
'code' => $context[self::CODE] ?? null,
'errors' => $this->convertFormErrorsToArray($object),
'errors' => $this->convertFormErrorsToArray($data),
];

if (0 !== \count($object->all())) {
$data['children'] = $this->convertFormChildrenToArray($object);
if (0 !== \count($data->all())) {
$error['children'] = $this->convertFormChildrenToArray($data);
}

return $data;
return $error;
}

public function getSupportedTypes(?string $format): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@
*/
final class JsonSerializableNormalizer extends AbstractNormalizer
{
public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if ($this->isCircularReference($object, $context)) {
return $this->handleCircularReference($object, $format, $context);
if ($this->isCircularReference($data, $context)) {
return $this->handleCircularReference($data, $format, $context);
}

if (!$object instanceof \JsonSerializable) {
if (!$data instanceof \JsonSerializable) {
throw new InvalidArgumentException(\sprintf('The object must implement "%s".', \JsonSerializable::class));
}

if (!$this->serializer instanceof NormalizerInterface) {
throw new LogicException('Cannot normalize object because injected serializer is not a normalizer.');
}

return $this->serializer->normalize($object->jsonSerialize(), $format, $context);
return $this->serializer->normalize($data->jsonSerialize(), $format, $context);
}

public function getSupportedTypes(?string $format): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,25 @@ public function setSerializer(SerializerInterface $serializer): void
$this->normalizer->setSerializer($serializer);
}

public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
public function normalize(mixed $data, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if ($object instanceof Headers) {
if ($data instanceof Headers) {
$ret = [];
foreach ($this->headersProperty->getValue($object) as $name => $header) {
foreach ($this->headersProperty->getValue($data) as $name => $header) {
$ret[$name] = $this->serializer->normalize($header, $format, $context);
}

return $ret;
}

$ret = $this->normalizer->normalize($object, $format, $context);
$ret = $this->normalizer->normalize($data, $format, $context);

if ($object instanceof AbstractPart) {
$ret['class'] = $object::class;
if ($data instanceof AbstractPart) {
$ret['class'] = $data::class;
unset($ret['seekable'], $ret['cid'], $ret['handle']);
}

if ($object instanceof RawMessage && \array_key_exists('message', $ret) && null === $ret['message']) {
if ($data instanceof RawMessage && \array_key_exists('message', $ret) && null === $ret['message']) {
unset($ret['message']);
}

Expand Down
Loading
Loading