8000 [Serializer] Add support for auto generated custom normalizers by Nyholm · Pull Request #52905 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Add support for auto generated custom normalizers #52905

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

Open
wants to merge 13 commits into
base: 7.4
Choose a base branch
from
Prev Previous commit
Next Next commit
cs
  • Loading branch information
Nyholm committed Jan 15, 2024
commit d10be79ef4dd2325c68c6d27226ac4cd60fb055b
33 changes: 14 additions & 19 deletions src/Symfony/Component/Serializer/Builder/NormalizerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@

use PhpParser\Builder\Class_;
use PhpParser\Builder\Namespace_;
use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
use Symfony\Component\Serializer\Exception\DenormalizingUnionFailedException;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use PhpParser\BuilderFactory;
use PhpParser\PrettyPrinter;
use PhpParser\Node;

/**
* The main class to create a new Normalizer from a ClassDefinition.
Expand All @@ -42,7 +42,7 @@ public function __construct()
throw new \LogicException(sprintf('You cannot use "%s" as the "nikic/php-parser" package is not installed. Try running "composer require nikic/php-parser".', static::class));
}

$this->factory = new BuilderFactory;
$this->factory = new BuilderFactory();
$this->printer = new PrettyPrinter\Standard();
}

Expand Down Expand Up @@ -99,7 +99,6 @@ private function generateNormalizeChildMethod(Namespace_ $namespace, Class_ $cla
)
);


// private function normalizeChild(mixed $object, ?string $format, array $context, bool $canBeIterable): mixed;
$class->addStmt($this->factory->method('normalizeChild')
->makePrivate()
Expand Down Expand Up @@ -151,7 +150,7 @@ private function generateNormalizeChildMethod(Namespace_ $namespace, Class_ $cla
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name('true'))),
]
)
),
])
),
new Node\Arg(new Node\Expr\Variable('object')),
Expand Down Expand Up @@ -205,7 +204,6 @@ private function generateDenormalizeChildMethod(Namespace_ $namespace, Class_ $c
)
);


// private function denormalizeChild(mixed $data, string $type, ?string $format, array $context, bool $canBeIterable): mixed;
$class->addStmt($this->factory->method('denormalizeChild')
->makePrivate()
Expand Down Expand Up @@ -259,7 +257,7 @@ private function generateDenormalizeChildMethod(Namespace_ $namespace, Class_ $c
new Node\Arg(new Node\Expr\Variable('context')),
new Node\Arg(new Node\Expr\ConstFetch(new Node\Name('true'))),
]
)
),
])
),
new Node\Arg(new Node\Expr\Variable('data')),
Expand Down Expand Up @@ -359,7 +357,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
[new Node\Arg(new Node\Expr\ClassConstFetch(new Node\Name($definition->getSourceClassName()), 'class'))]
),
'newInstanceWithoutConstructor'
)));
)));
} else {
$constructorArguments = [];

Expand All @@ -369,10 +367,10 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
$canBeIterable = $propertyDefinition->isCollection();

$defaultValue = $propertyDefinition->getConstructorDefaultValue();
if (is_object($defaultValue)) {
if (\is_object($defaultValue)) {
// public function __construct($foo = new \stdClass());
// There is no support for parameters to the object.
$defaultValue = new Expr\New_(new Node\Name\FullyQualified(get_class($defaultValue)));
$defaultValue = new Expr\New_(new Node\Name\FullyQualified($defaultValue::class));
} else {
$defaultValue = $this->factory->val($defaultValue);
}
Expand Down Expand Up @@ -427,7 +425,7 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
$defaultValue
)),
],
'else' => new Node\Stmt\Else_($variableOutput)
'else' => new Node\Stmt\Else_($variableOutput),
]
)];
}
Expand Down Expand Up @@ -486,11 +484,9 @@ private function addDenormailizeMethod(ClassDefinition $definition, Namespace_ $
),
];
}


}

$result = $tempVariableName === null ? $variable : new Node\Expr\Variable($tempVariableName);
$result = null === $tempVariableName ? $variable : new Node\Expr\Variable($tempVariableName);
if (null !== $method = $propertyDefinition->getSetterName()) {
$variableOutput[] = new Node\Stmt\Expression(new Node\Expr\MethodCall(
new Node\Expr\Variable('output'),
Expand Down Expand Up @@ -567,7 +563,7 @@ private function addNormailizeMethod(ClassDefinition $definition, Namespace_ $na
->addParam($this->factory->param('format')->setType('string')->setDefault(null))
->addParam($this->factory->param('context')->setType('array')->setDefault([]))
->setReturnType('array|string|int|float|bool|\ArrayObject|null')
->setDocComment(sprintf('/**'.PHP_EOL.'* @param %s $object'.PHP_EOL.'*/', $definition->getSourceClassName()))
->setDocComment(sprintf('/**'.\PHP_EOL.'* @param %s $object'.\PHP_EOL.'*/', $definition->getSourceClassName()))
->addStmt(new Node\Stmt\Return_(new Node\Expr\Array_($bodyArrayItems))));

if ($needsChildNormalizer) {
Expand Down Expand Up @@ -644,7 +640,7 @@ private function generateCodeToDeserializeMultiplePossibleClasses(array $targetC
new Node\Expr\ArrayDimFetch(new Node\Expr\Variable('exceptions')),
new Node\Expr\Variable('e')
)
)
),
]
),
],
Expand All @@ -666,11 +662,10 @@ private function generateCodeToDeserializeMultiplePossibleClasses(array $targetC
]
)
),
)
),
],
]
),
];

}
}
0