1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the FiveLab Resource package |
7
|
|
|
* |
8
|
|
|
* (c) FiveLab |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FiveLab\Component\Resource\Serializer\Normalizer; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
17
|
|
|
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
18
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
19
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
20
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer as SymfonyObjectNormalizer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Extend default object normalizer for add ability for run custom code before and after normalization |
24
|
|
|
* and all ability for remove nullable attributes from structure. |
25
|
|
|
* |
26
|
|
|
* We should extend default symfony serializer for add ability for adding custom normalizers. |
27
|
|
|
*/ |
28
|
|
|
class ObjectNormalizer extends SymfonyObjectNormalizer |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $serializeNull; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor. |
37
|
|
|
* |
38
|
|
|
* @param ClassMetadataFactoryInterface $classMetadataFactory |
39
|
|
|
* @param NameConverterInterface $nameConverter |
40
|
|
|
* @param PropertyAccessorInterface $propertyAccessor |
41
|
|
|
* @param PropertyTypeExtractorInterface $propertyTypeExtractor |
42
|
|
|
* @param bool $serializeNull |
43
|
|
|
*/ |
44
|
15 |
|
public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, bool $serializeNull = true) |
45
|
|
|
{ |
46
|
15 |
|
parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor); |
47
|
|
|
|
48
|
15 |
|
$this->serializeNull = $serializeNull; |
49
|
15 |
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
* |
55
|
|
|
* @throws \InvalidArgumentException |
56
|
|
|
* @throws \LogicException |
57
|
|
|
*/ |
58
|
9 |
|
public function normalize($object, $format = null, array $context = []): array |
59
|
|
|
{ |
60
|
|
|
// Call to before normalization callback |
61
|
9 |
|
if (\array_key_exists('before_normalization', $context)) { |
62
|
2 |
|
$beforeNormalization = $context['before_normalization']; |
63
|
|
|
|
64
|
2 |
|
if (!\is_callable($beforeNormalization)) { |
65
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
66
|
1 |
|
'Invalid callable for before normalization. "%s" given.', |
67
|
1 |
|
\is_object($beforeNormalization) ? \get_class($beforeNormalization) : \gettype($beforeNormalization) |
68
|
|
|
)); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$beforeNormalization($object, $format, $context); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Normalize object |
75
|
8 |
|
$data = parent::normalize($object, $format, $context); |
76
|
|
|
|
77
|
8 |
|
if (!$this->serializeNull) { |
78
|
1 |
|
$data = $this->removeNullableAttributes($data); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Call to after normalization callback |
82
|
8 |
|
if (\array_key_exists('after_normalization', $context)) { |
83
|
3 |
|
$afterNormalization = $context['after_normalization']; |
84
|
|
|
|
85
|
3 |
|
if (!\is_callable($afterNormalization)) { |
86
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
87
|
1 |
|
'Invalid callable for after normalization. "%s" given.', |
88
|
1 |
|
\is_object($afterNormalization) ? \get_class($afterNormalization) : \gettype($afterNormalization) |
89
|
|
|
)); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$data = $afterNormalization($data, $object, $format, $context); |
93
|
|
|
|
94
|
2 |
|
if (!\is_array($data)) { |
95
|
1 |
|
throw new \LogicException(sprintf( |
96
|
1 |
|
'The after normalization callback should return array, but "%s" given.', |
97
|
1 |
|
\is_object($data) ? \get_class($data) : \gettype($data) |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
return $data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Remove nullable attributes |
107
|
|
|
* |
108
|
|
|
* @param array $data |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
1 |
|
private function removeNullableAttributes(array $data): array |
113
|
|
|
{ |
114
|
|
|
return \array_filter($data, static function ($value) { |
115
|
1 |
|
return (bool) $value; |
116
|
1 |
|
}); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|