1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Kévin Dunglas <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This source file is subject to the MIT license that is bundled |
7
|
|
|
* with this source code in the file LICENSE. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Dunglas\DoctrineJsonOdm; |
11
|
|
|
|
12
|
|
|
use Databrydge\Synchronizer\Domain\Connection\PackageType; |
13
|
|
|
use Symfony\Component\Serializer\Exception\InvalidArgumentException; |
14
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
18
|
|
|
use Symfony\Component\Serializer\Serializer as BaseSerializer; |
19
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
20
|
|
|
use Symfony\Component\Serializer\SerializerAwareTrait; |
21
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
22
|
|
|
|
23
|
|
|
final class Serializer extends BaseSerializer |
24
|
|
|
{ |
25
|
|
|
private const KEY_TYPE = '#type'; |
26
|
|
|
private const KEY_SCALAR = '#scalar'; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function normalize($data, $format = null, array $context = []) |
30
|
|
|
{ |
31
|
|
|
$normalizedData = parent::normalize($data, $format, $context); |
32
|
|
|
|
33
|
|
|
if (is_object($data)) { |
34
|
|
|
$typeData = [self::KEY_TYPE => get_class($data)]; |
35
|
|
|
$valueData = is_scalar($normalizedData) ? [self::KEY_SCALAR => $normalizedData] : $normalizedData; |
36
|
|
|
$normalizedData = array_merge($typeData, $valueData); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $normalizedData; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function denormalize($data, $class, $format = null, array $context = []) |
43
|
|
|
{ |
44
|
|
|
if (is_array($data) && (isset($data[self::KEY_TYPE]))) { |
45
|
|
|
$type = $data[self::KEY_TYPE]; |
46
|
|
|
unset($data[self::KEY_TYPE]); |
47
|
|
|
|
48
|
|
|
$data = $data[self::KEY_SCALAR] ?? $data; |
49
|
|
|
|
50
|
|
|
return parent::denormalize($data, $type, $format, $context); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (is_iterable($data)) { |
54
|
|
|
$class = ($class === '') ? 'stdClass' : $class; |
55
|
|
|
$arrayOfDocuments = true; |
56
|
|
|
foreach ($data as $row) { |
|
|
|
|
57
|
|
|
if (!is_array($row)) { |
58
|
|
|
$arrayOfDocuments = false; |
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!isset($row[self::KEY_TYPE])) { |
63
|
|
|
$arrayOfDocuments = false; |
64
|
|
|
break; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if ($arrayOfDocuments) { |
69
|
|
|
return parent::denormalize($data, $class.'[]', $format, $context); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return parent::denormalize($data, $class, $format, $context); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.