1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Serializer; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Survey\SurveyQuestion; |
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
7
|
|
|
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
8
|
|
|
use Symfony\Component\Serializer\Exception\LogicException; |
9
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
10
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
11
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
12
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
13
|
|
|
|
14
|
|
View Code Duplication |
class QuestionNormalizer extends ObjectNormalizer |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* SurveyNormalizer constructor. |
18
|
|
|
* |
19
|
|
|
* @param ClassMetadataFactoryInterface|null $classMDF |
20
|
|
|
* @param NameConverterInterface|null $nameCv |
21
|
|
|
* @param PropertyAccessorInterface|null $propAs |
22
|
|
|
* @param PropertyTypeExtractorInterface|null $propTE |
23
|
|
|
*/ |
24
|
5 |
|
public function __construct($classMDF, $nameCv, $propAs, $propTE) |
25
|
|
|
{ |
26
|
5 |
|
parent::__construct($classMDF, $nameCv, $propAs, $propTE); |
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
5 |
|
public function supportsNormalization($data, $format = null) |
33
|
|
|
{ |
34
|
5 |
|
return $data instanceof SurveyQuestion; |
35
|
|
|
} |
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function normalize($object, $format = null, array $context = []) |
40
|
|
|
{ |
41
|
|
|
if (!$this->serializer instanceof NormalizerInterface) { |
42
|
|
|
throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer'); |
43
|
|
|
} |
44
|
|
|
/** @var SurveyQuestion $question */ |
45
|
|
|
$question = &$object; |
46
|
|
|
|
47
|
|
|
return $this->serializer->normalize(new \ArrayObject([ |
48
|
|
|
'id' => $question->getId(), |
49
|
|
|
'title' => $question->getTitle(), |
50
|
|
|
'orderNumber' => $question->getOrderNumber(), |
51
|
|
|
'variants' => $question->getVariants(), |
52
|
|
|
]), $format, $context); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.