1 | <?php |
||
17 | class SurveyNormalizer extends ObjectNormalizer |
||
18 | { |
||
19 | /** |
||
20 | * @var Registry |
||
21 | */ |
||
22 | protected $doctrine; |
||
23 | |||
24 | /** |
||
25 | * SurveyNormalizer constructor. |
||
26 | * |
||
27 | * @param ClassMetadataFactoryInterface|null $classMDF |
||
28 | * @param NameConverterInterface|null $nameCv |
||
29 | * @param PropertyAccessorInterface|null $propAs |
||
30 | * @param PropertyTypeExtractorInterface|null $propTE |
||
31 | * @param Registry $doctrine |
||
32 | */ |
||
33 | 5 | public function __construct($classMDF, $nameCv, $propAs, $propTE, Registry $doctrine) |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 5 | public function supportsNormalization($data, $format = null) |
|
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 2 | public function normalize($object, $format = null, array $context = []) |
|
50 | { |
||
51 | 2 | if (!$this->serializer instanceof NormalizerInterface) { |
|
52 | throw new LogicException('Cannot normalize attributes because injected serializer is not a normalizer'); |
||
53 | } |
||
54 | /** @var Survey $survey */ |
||
55 | 2 | $survey = &$object; |
|
56 | 2 | if (isset($context[ObjectNormalizer::GROUPS])) { |
|
57 | 2 | if ($context[ObjectNormalizer::GROUPS][0] == 'list') { |
|
58 | 2 | return $this->serializer->normalize(new \ArrayObject([ |
|
59 | 2 | 'id' => $survey->getId(), |
|
60 | 2 | 'type' => $survey->getType(), |
|
61 | 2 | 'status' => $survey->getStatus(), |
|
62 | 2 | 'createdAt' => $survey->getCreatedAt(), |
|
63 | 2 | 'updatedAt' => $survey->getUpdatedAt(), |
|
64 | 2 | ]), $format, $context); |
|
65 | } |
||
66 | } |
||
67 | |||
68 | return $this->serializer->normalize(new \ArrayObject([ |
||
69 | 'id' => $survey->getId(), |
||
70 | 'type' => $survey->getType(), |
||
71 | 'status' => $survey->getStatus(), |
||
72 | 'answers' => $survey->getAnswers(), |
||
73 | 'createdAt' => $survey->getCreatedAt(), |
||
74 | 'updatedAt' => $survey->getUpdatedAt(), |
||
75 | ]), $format, $context); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * {@inheritdoc} |
||
80 | */ |
||
81 | 1 | public function denormalize($data, $class, $format = null, array $context = array()) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 1 | public function supportsDenormalization($data, $type, $format = null) |
|
128 | } |
||
129 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.