Skip to content

Instantly share code, notes, and snippets.

@ahonymous
Created March 22, 2017 17:57
Show Gist options
  • Save ahonymous/edfc0a4b95391ef2b2b1c165fd6e7d16 to your computer and use it in GitHub Desktop.
Save ahonymous/edfc0a4b95391ef2b2b1c165fd6e7d16 to your computer and use it in GitHub Desktop.
SurveyNormalizer
<?php
namespace AppBundle\Services;
use AppBundle\Entity\Survey\Survey;
use AppBundle\Entity\Survey\SurveyAnswer;
use AppBundle\Entity\Survey\SurveyQuestion;
use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
class SurveyNormalizer extends ObjectNormalizer
{
/**
* @var Registry
*/
protected $doctrine;
public function __construct(ClassMetadataFactory $classMetadataFactory, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, Registry $doctrine)
{
parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor);
$this->doctrine = $doctrine;
}
/**
* {@inheritdoc}
*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!$this->serializer instanceof DenormalizerInterface) {
throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer');
}
if (!isset($context[ObjectNormalizer::OBJECT_TO_POPULATE])) {
throw new LogicException('Not found object_to_populate');
}
/** @var Survey $survey */
$survey = $context[ObjectNormalizer::OBJECT_TO_POPULATE];
$survey->setStatus('submited');
$fields = $this->getAllowedAttributes($survey, $format, $context);
foreach ($data as $key => $val) {
if (in_array($key, $fields)) {
switch ($key) {
case 'answers':
foreach ($val as $item) {
$answer = $this->serializer->deserialize($item, $format, $context);
$survey->addSurveyAnswer($answer);
}
break;
}
}
}
if (isset($data['answers']) && is_array($data['answers'])) {
foreach ($survey->getQuestions() as $question) {
$questionsId[] = $question->getId();
}
foreach ($data['answers'] as $answer) {
$dataId[] = $answer['question']['id'];
}
if (array_diff($questionsId, $dataId)) {
return null;
}
foreach ($data['answers'] as $answer) {
$newAnswer = new SurveyAnswer();
$question = $this->em->getRepository(SurveyQuestion::class)
->find($answer['question']['id']);
if ($question->getVariants()) {
if (!in_array($answer['content'], $question->getVariants())) {
return null;
}
}
$content = $answer['content'];
$newAnswer->setSurvey($survey);
$newAnswer->setQuestion($question);
$newAnswer->setContent($content);
$this->em->persist($newAnswer);
}
$this->em->flush();
}
return $survey;
}
/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
if ($type != Survey::class) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment