Created
May 31, 2018 07:39
-
-
Save B-Galati/d40e9e030c52ca5ee9de6bca0bd9e213 to your computer and use it in GitHub Desktop.
Simple example of denormalizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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