Skip to content

Instantly share code, notes, and snippets.

@B-Galati
Created May 31, 2018 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save B-Galati/d40e9e030c52ca5ee9de6bca0bd9e213 to your computer and use it in GitHub Desktop.
Save B-Galati/d40e9e030c52ca5ee9de6bca0bd9e213 to your computer and use it in GitHub Desktop.
Simple example of denormalizer
<?php
namespace App\Normalizer;
use App\Entity\User;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class UserNormalizer implements DenormalizerInterface
{
public function denormalize($data, $class, $format = null, array $context = array())
{
$defaults = [
'emailAddress' => '',
'firstName' => '',
'lastName' => '',
'zipCode' => '',
];
$data = array_merge($defaults, $data);
return new User(
Uuid::fromString($data['uuid']),
$data['emailAddress'],
$data['firstName'],
$data['lastName'],
$data['zipCode']
);
}
public function supportsDenormalization($data, $type, $format = null)
{
return User::class === $type;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment