Skip to content

Instantly share code, notes, and snippets.

@podorozhny
Forked from Tom32i/CollectionNormalizer.php
Created December 28, 2016 16:02
Show Gist options
  • Save podorozhny/2838cdc83290317c4a772fe96d96d6c1 to your computer and use it in GitHub Desktop.
Save podorozhny/2838cdc83290317c4a772fe96d96d6c1 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\Serializer\Normalizer;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\SerializerAwareNormalizer;
/**
* Collection Normalizer
*/
class CollectionNormalizer extends SerializerAwareNormalizer implements NormalizerInterface, DenormalizerInterface
{
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && $data instanceof Collection;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return is_array($data) && in_array('\Doctrine\Common\Collections\Collection', class_implements($type));
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
return $object->map(function ($item) use ($format, $context) {
return $this->serializer->normalize($item, $format, $context);
})->getValues();
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
return new ArrayCollection(array_map(function ($item) use ($class, $format, $context) {
return $this->deserialize($item, $class, $format, $context);
}, $data));
}
}
<?php
namespace Acme\Serializer\Normalizer;
use DateTime;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
/**
* DateTime Normalizer
*/
class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface
{
/**
* Date format
*
* @var string
*/
protected $dateFormat = 'Y-m-d';
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && $data instanceof DateTime;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
return $type === 'DateTime';
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
return $object->format($this->dateFormat);
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
return new DateTime($data);
}
}
<?php
namespace Acme\Serializer\Normalizer;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Validator\ConstraintViolation;
/**
* Form Error Normalizer
*/
class FormErrorNormalizer implements NormalizerInterface
{
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && ($data instanceof FormError || $data instanceof FormErrorIterator);
}
/**
* {@inheritdoc}
*/
public function normalize($object, $format = null, array $context = array())
{
if ($object instanceof FormErrorIterator) {
$errors = [];
foreach ($object as $key => $error) {
$errors[] = $this->normalizeError($error);
}
return $errors;
}
return $this->normalizeError($object);
}
/**
* Normalize error
*
* @param FormError $error
*
* @return array
*/
private function normalizeError(FormError $error)
{
return [
'message' => $error->getMessage(),
'parameters' => $error->getMessageParameters(),
'plural' => $error->getMessagePluralization(),
'code' => $error->getMessageTemplate(),
'path' => $this->getPath($error),
];
}
/**
* Get path for the given error
*
* @param FormError $error
*
* @return string
*/
private function getPath(FormError $error)
{
if ($error->getCause() instanceof ConstraintViolation) {
return $error->getCause()->getPropertyPath();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment