8000 [AstGenerator] [WIP] New Component, add normalizer generator by joelwurtz · Pull Request #17516 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[AstGenerator] [WIP] New Component, add normalizer generator #17516

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
Prev Previous commit
Next Next commit
Create NormalizableObjectTypeGenerator
  • Loading branch information
GuilhemN authored and joelwurtz committed Aug 5, 2016
commit 7cbeab0ed481d2d455e272427fe14b0559f48ed8
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AstGenerator\Hydrate\Type;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\Node\Name;
use Symfony\Component\AstGenerator\AstGeneratorInterface;
use Symfony\Component\AstGenerator\Exception\MissingContextException;
use Symfony\Component\PropertyInfo\Type;

/**
* Generate hydration of normalizable object type.
*
* @author Guilhem N. <egetick@gmail.com>
*/
class NormalizableObjectTypeGenerator implements AstGeneratorInterface
{
/**
* {@inheritdoc}
*
* @param Type $object A type extracted with PropertyInfo component
*/
public function generate($object, array $context = [])
{
if (!isset($context['input']) || !($context['input'] instanceof Expr)) {
throw new MissingContextException('Input variable not defined or not an Expr in generation context');
}

if (!isset($context['output']) || !($context['output'] instanceof Expr)) {
throw new MissingContextException('Output variable not defined or not an Expr in generation context');
}

if (!isset($context['normalizer']) || !($context['normalizer'] instanceof Expr)) {
throw new MissingContextException('Denormalizer variable not defined or not an Expr in generation context');
}

$assign = [
new Expr\Assign($context['output'], $context['input'])
];

if (isset($context['condition']) && $context['condition']) {
return [new Stmt\If_(
new Expr\BinaryOp\LogicalAnd(
new Expr\FuncCall(
'is_object',
array(
new Arg($context['input']),
)
),
new Expr\MethodCall(
$context['normalizer'],
'supportsNormalization'
call_user_func(function() use ($context) {
$args = array(new Arg($context['input']));
if (isset($context['format'])) {
$args[] = new Arg($context['input']);
} else {
$args[] = new Arg(new Expr\ConstFetch(new Name('null')));
}
if (isset($context['context'])) {
$args[] = new Arg($context['context']);
}

return $args;
})
)
),
[
'stmts' => $assign
]
)];
}

return $assign;
}

/**
* {@inheritdoc}
*/
public function supportsGeneration($object)
{
return $object instanceof Type && Type::BUILTIN_TYPE_OBJECT === $object->getBuiltinType();
}
}
0