0

I am trying to add my own normalizer since I need to convert (denormalize) some raw values to it's related entities. This is what I have done:

namespace MMI\IntegrationBundle\Serializer\Normalizer;

use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class RelationshipNormalizer implements NormalizerInterface, DenormalizerInterface
{
    public function normalize($object, $format = null, array $context = [])
    {
        // @TODO implement this method
    }

    public function supportsNormalization($data, $format = null): bool
    {
        return $data instanceof \AgreementType;
    }

    public function denormalize($data, $class, $format = null, array $context = [])
    {
        // @TODO implement this method
    }

    public function supportsDenormalization($data, $type, $format = null): bool
    {
        $supportedTypes = [
            \AgreementType::class   => true
        ];

        return isset($supportedTypes[$type]);
    }
}

And this is how I am using it from the controller:

    $propertyNameConverter = new PropertyNameConverter();
    $encoder               = new JsonEncoder();

    $normalizer = new ObjectNormalizer(
        null,
        $propertyNameConverter,
        null,
        new ReflectionExtractor()
    );

    $serializer = new Serializer([
        new DateTimeNormalizer(),
        new RelationshipNormalizer(),
        $normalizer,
        new ArrayDenormalizer(),
    ], [$encoder]);

When the code reach this method:

private function getNormalizer($data, $format, array $context)
{
    foreach ($this->normalizers as $normalizer) {
        if ($normalizer instanceof NormalizerInterface && $normalizer->supportsNormalization($data, $format, $context)) {
            return $normalizer;
        }
    }
}

Using Xdebug and the IDE I can see how the condition $data instanceof \AgreementType is accomplish but then the code try again to check the Normalizer and then this function is executed:

public function supportsDenormalization($data, $type, $format = null)
{
    return class_exists($type);
}

And that's exactly where I get the wrong normalizer causing the following error:

Notice: Uninitialized string offset: 0 in vendor/symfony/symfony/src/Symfony/Component/Inflector/Inflector.php at line 179

UPDATE:

I have tried this other way and result is exactly the same as before meaning same error message:

$callback = function ($value) {
    $value = $this->em->getRepository('QuoteBundle:' . $this->table_mapping[$this->entity])->find($value);

    return $value;
};

$entityNormalizer = new GetSetMethodNormalizer();
$entityNormalizer->setCallbacks([
    'agreementType'  => $callback,
]);

$serializer = new Serializer([
    new DateTimeNormalizer(),
    $normalizer,
    $entityNormalizer,
    new ArrayDenormalizer(),
], [$encoder]);

What I am missing here?

ReynierPM
  • 17,594
  • 53
  • 193
  • 363

1 Answers1

0

After get some help on #symfony-devs channel on Slack I did found that order matters. Here is the solution to my issue (compare the piece of code below to the one on the OP and you'll see the difference):

$normalizer = new ObjectNormalizer(
    null,
    $propertyNameConverter,
    null,
    new ReflectionExtractor()
);

// Notice how ObjectNormalizer() is the last normalizer
$serializer = new Serializer([
    new ArrayDenormalizer(),
    new DateTimeNormalizer(),
    new RelationshipNormalizer($em),
    $normalizer,
], [$encoder]);
ReynierPM
  • 17,594
  • 53
  • 193
  • 363