|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
7
|
|
|
use JMS\Serializer\Context; |
|
8
|
|
|
use JMS\Serializer\VisitorInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class JmsDoctrineHandler |
|
11
|
|
|
{ |
|
12
|
|
|
const TYPE = 'DoctrineAssociation'; |
|
13
|
|
|
|
|
14
|
|
|
/** @var ManagerRegistry */ |
|
15
|
|
|
private $registry; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* JmsDoctrineHandler constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* @param ManagerRegistry $registry |
|
21
|
|
|
*/ |
|
22
|
2 |
|
public function __construct(ManagerRegistry $registry) |
|
23
|
|
|
{ |
|
24
|
2 |
|
$this->registry = $registry; |
|
25
|
2 |
|
} |
|
26
|
|
|
|
|
27
|
2 |
|
public function serializeRelation(VisitorInterface $visitor, $relation, array $type, Context $context) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
2 |
|
if ($relation instanceof \Traversable) { |
|
30
|
2 |
|
$relation = iterator_to_array($relation); |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
2 |
|
if (is_array($relation)) { |
|
34
|
2 |
|
return array_map([$this, 'convertEntityToIds'], $relation); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
return $this->convertEntityToIds($relation); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function deserializeRelation(VisitorInterface $visitor, $data, array $type, Context $context) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
// fixme |
|
43
|
|
|
throw new \BadMethodCallException('Not supported at the moment'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
View Code Duplication |
private function convertEntityToIds($entity) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
1 |
|
$class = get_class($entity); |
|
49
|
1 |
|
$metadata = $this->registry->getManagerForClass($class)->getClassMetadata($class); |
|
50
|
|
|
|
|
51
|
1 |
|
$ids = $metadata->getIdentifierValues($entity); |
|
52
|
|
|
|
|
53
|
1 |
|
if (!$metadata instanceof ClassMetadata || $metadata->isIdentifierComposite) { |
|
54
|
|
|
return $ids; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return array_shift($ids); |
|
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.