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