Created
October 27, 2015 13:57
-
-
Save jdeniau/f3461c92e3376b8906db to your computer and use it in GitHub Desktop.
Symfony Serializer Serialize DateTime instance
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
services: | |
datetime_normalizer: | |
class: Acme\Foo\Normalizer\DateTimeNormalizer | |
tags: | |
- { name: serializer.normalizer } |
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 Acme\Foo\Normalizer; | |
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
/** | |
* Class DateTimeNormalizer | |
* @author Julien Deniau <[email protected]> | |
*/ | |
class DateTimeNormalizer implements NormalizerInterface | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function normalize($object, $format = null, array $context = array()) | |
{ | |
return $object->format(\DateTime::ISO8601); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function supportsNormalization($data, $format = null) | |
{ | |
return $data instanceof \DateTime; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment