1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Api\JsonRpc\Adapters\JMS; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
7
|
|
|
use JMS\Serializer\Context; |
8
|
|
|
use JMS\Serializer\JsonDeserializationVisitor; |
9
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
10
|
|
|
|
11
|
|
|
final class RelationsHandler |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var EntityManagerInterface |
15
|
|
|
*/ |
16
|
|
|
private $manager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* RelationsHandler constructor. |
20
|
|
|
* |
21
|
|
|
* @param EntityManagerInterface $manager |
22
|
|
|
*/ |
23
|
|
|
public function __construct(EntityManagerInterface $manager) |
24
|
|
|
{ |
25
|
|
|
$this->manager = $manager; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function serializeRelation(JsonSerializationVisitor $visitor, $relation, array $type, Context $context) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
if ($relation instanceof \Traversable) { |
31
|
|
|
$relation = iterator_to_array($relation); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (is_array($relation)) { |
35
|
|
|
return array_map([$this, 'getSingleEntityRelation'], $relation); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->getSingleEntityRelation($relation); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function deserializeRelation(JsonDeserializationVisitor $visitor, $relation, array $type, Context $context) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$className = isset($type['params'][0]['name']) ? $type['params'][0]['name'] : null; |
44
|
|
|
|
45
|
|
|
if (!class_exists($className)) { |
46
|
|
|
throw new \InvalidArgumentException('Class name should be explicitly set for deserialization'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @var ClassMetadata $metadata */ |
50
|
|
|
$metadata = $this->manager->getClassMetadata($className); |
51
|
|
|
|
52
|
|
|
if (!is_array($relation)) { |
53
|
|
|
return $this->manager->getReference($className, $relation); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$single = false; |
57
|
|
|
if ($metadata->isIdentifierComposite) { |
58
|
|
|
$single = true; |
59
|
|
|
foreach ($metadata->getIdentifierFieldNames() as $idName) { |
60
|
|
|
$single = $single && array_key_exists($idName, $relation); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($single) { |
65
|
|
|
return $this->manager->getReference($className, $relation); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$objects = []; |
69
|
|
|
foreach ($relation as $idSet) { |
70
|
|
|
$objects[] = $this->manager->getReference($className, $idSet); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $objects; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $relation |
78
|
|
|
* |
79
|
|
|
* @return array|mixed |
80
|
|
|
*/ |
81
|
|
|
private function getSingleEntityRelation($relation) |
82
|
|
|
{ |
83
|
|
|
$metadata = $this->manager->getClassMetadata(get_class($relation)); |
84
|
|
|
|
85
|
|
|
$ids = $metadata->getIdentifierValues($relation); |
86
|
|
|
if (!$metadata->isIdentifierComposite) { |
|
|
|
|
87
|
|
|
$ids = array_shift($ids); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $ids; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.