1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Darkilliant\ImportBundle\Serializer\JMS; |
6
|
|
|
|
7
|
|
|
use Darkilliant\ImportBundle\Resolver\EntityResolver; |
8
|
|
|
use JMS\Serializer\VisitorInterface; |
9
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
10
|
|
|
use JMS\Serializer\DeserializationContext; |
11
|
|
|
use JMS\Serializer\Construction\ObjectConstructorInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @internal |
15
|
|
|
* Doctrine object constructor for new (or existing) objects during deserialization |
16
|
|
|
*/ |
17
|
|
|
class DoctrineObjectConstructor implements ObjectConstructorInterface |
18
|
|
|
{ |
19
|
|
|
/** @var array */ |
20
|
|
|
protected $config; |
21
|
|
|
private $fallbackConstructor; |
22
|
|
|
|
23
|
|
|
/** @var EntityResolver */ |
24
|
|
|
private $resolver; |
25
|
|
|
|
26
|
4 |
|
public function __construct(ObjectConstructorInterface $fallbackConstructor, EntityResolver $resolver, array $config) |
27
|
|
|
{ |
28
|
4 |
|
$this->fallbackConstructor = $fallbackConstructor; |
29
|
4 |
|
$this->config = $config; |
30
|
4 |
|
$this->resolver = $resolver; |
31
|
4 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
* |
36
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
37
|
|
|
*/ |
38
|
4 |
|
public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context) |
39
|
|
|
{ |
40
|
4 |
|
if (!isset($this->config[$metadata->name])) { |
41
|
1 |
|
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Deprecated ignored for compatibilty with lower version of jms serializer |
45
|
3 |
|
$class = $metadata->name; |
46
|
3 |
|
$entity = $this->resolver->resolve($class, $data, /* @scrutinizer ignore-deprecated */ $context->attributes->all()['entity_resolver'][$class] ?? null); |
47
|
|
|
|
48
|
3 |
|
return (null === $entity) ? new $class() : $entity; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|