|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Darkilliant\ImportBundle\Serializer\Symfony; |
|
6
|
|
|
|
|
7
|
|
|
use Darkilliant\ImportBundle\Resolver\EntityResolver; |
|
8
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
9
|
|
|
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface; |
|
10
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
|
11
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
12
|
|
|
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @internal |
|
16
|
|
|
* Entity normalizer |
|
17
|
|
|
*/ |
|
18
|
|
|
class EntityNormalizer extends ObjectNormalizer |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
protected $config; |
|
22
|
|
|
|
|
23
|
|
|
/** @var EntityResolver */ |
|
24
|
|
|
private $resolver; |
|
25
|
|
|
|
|
26
|
8 |
|
public function __construct( |
|
27
|
|
|
array $config, |
|
28
|
|
|
EntityResolver $resolver, |
|
29
|
|
|
ClassMetadataFactoryInterface $classMetadataFactory = null, |
|
30
|
|
|
NameConverterInterface $nameConverter = null, |
|
31
|
|
|
PropertyAccessorInterface $propertyAccessor = null, |
|
32
|
|
|
PropertyTypeExtractorInterface $propertyTypeExtractor = null |
|
33
|
|
|
) { |
|
34
|
8 |
|
parent::__construct($classMetadataFactory, $nameConverter, $propertyAccessor, $propertyTypeExtractor); |
|
35
|
|
|
// Entity manager |
|
36
|
8 |
|
$this->resolver = $resolver; |
|
37
|
8 |
|
$this->config = $config; |
|
38
|
8 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function supportsDenormalization($data, $type, $format = null) |
|
44
|
|
|
{ |
|
45
|
4 |
|
return is_array($data) && !isset($data['_resolved']) && isset($this->config[$type]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
|
52
|
|
|
{ |
|
53
|
4 |
|
$entity = $this->resolver->resolve($class, $data, $context['entity_resolver'][$class] ?? null); |
|
54
|
|
|
|
|
55
|
4 |
|
$entity = $entity ?? new $class(); |
|
56
|
|
|
|
|
57
|
4 |
|
$context['object_to_populate'] = $entity; |
|
58
|
4 |
|
$data['_resolved'] = true; |
|
59
|
|
|
|
|
60
|
4 |
|
return $this->serializer->denormalize($data, $class, $format, $context); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|