1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Serializer; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Survey\SurveyType; |
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
|
|
|
class TypeNormalizer 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 SurveyType; |
35
|
|
|
} |
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function normalize($object, $format = null, array $context = []) |
40
|
|
|
{ |
41
|
2 |
|
if (!$this->serializer instanceof NormalizerInterface) { |
42
|
|
|
throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer'); |
43
|
|
|
} |
44
|
|
|
/** @var SurveyType $type */ |
45
|
2 |
|
$type = &$object; |
46
|
2 |
|
if (isset($context[ObjectNormalizer::GROUPS])) { |
47
|
2 |
|
if ($context[ObjectNormalizer::GROUPS][0] == 'list') { |
48
|
2 |
|
return $this->serializer->normalize(new \ArrayObject([ |
49
|
2 |
|
'name' => $type->getName(), |
50
|
2 |
|
]), $format, $context); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->serializer->normalize(new \ArrayObject([ |
55
|
|
|
'id' => $type->getId(), |
56
|
|
|
'name' => $type->getName(), |
57
|
|
|
'description' => $type->getDescription(), |
58
|
|
|
'section' => $type->getSections(), |
59
|
|
|
]), $format, $context); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|