1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use JMS\Serializer\Context; |
7
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
8
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
9
|
|
|
use JMS\Serializer\VisitorInterface; |
10
|
|
|
use ScayTrase\Api\Cruds\Adaptors\DoctrineOrm\EntityToIdNormalizer; |
11
|
|
|
|
12
|
|
|
final class JmsDoctrineHandler |
13
|
|
|
{ |
14
|
|
|
const TYPE = 'DoctrineAssociation'; |
15
|
|
|
|
16
|
|
|
/** @var EntityToIdNormalizer */ |
17
|
|
|
private $converter; |
18
|
|
|
/** |
19
|
|
|
* @var ManagerRegistry |
20
|
|
|
*/ |
21
|
|
|
private $registry; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* JmsDoctrineHandler constructor. |
25
|
|
|
* |
26
|
|
|
* @param ManagerRegistry $registry |
27
|
|
|
*/ |
28
|
8 |
|
public function __construct(ManagerRegistry $registry) |
29
|
|
|
{ |
30
|
8 |
|
$this->registry = $registry; |
31
|
8 |
|
$this->converter = new EntityToIdNormalizer($this->registry); |
32
|
8 |
|
} |
33
|
|
|
|
34
|
8 |
|
public function serializeRelation(VisitorInterface $visitor, $relation, array $type, Context $context) |
|
|
|
|
35
|
|
|
{ |
36
|
8 |
|
if ($relation instanceof \Traversable) { |
37
|
8 |
|
$relation = iterator_to_array($relation); |
38
|
|
|
} |
39
|
|
|
|
40
|
8 |
|
if (is_array($relation)) { |
41
|
8 |
|
return array_map([$this->converter, 'normalize'], $relation); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
return $this->converter->normalize($relation); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function deserializeRelation(VisitorInterface $visitor, $data, array $type, Context $context) |
|
|
|
|
48
|
|
|
{ |
49
|
1 |
|
$metadatas = iterator_to_array($context->getMetadataStack()); |
50
|
|
|
/** @var PropertyMetadata $property */ |
51
|
1 |
|
$property = array_shift($metadatas); |
52
|
|
|
/** @var ClassMetadata $class */ |
53
|
1 |
|
$class = array_shift($metadatas); |
54
|
|
|
|
55
|
1 |
|
$metadata = $this->registry->getManagerForClass($class->name)->getClassMetadata($class->name); |
56
|
|
|
|
57
|
1 |
|
return $this->converter->denormalize($data, $metadata->getAssociationTargetClass($property->name)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.