1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\Cruds; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
|
7
|
|
|
abstract class AbstractReferenceProvider implements ReferenceProviderInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var ManagerRegistry |
11
|
|
|
*/ |
12
|
|
|
private $registry; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* PartialReferenceProvider constructor. |
16
|
|
|
* |
17
|
|
|
* @param ManagerRegistry $registry |
18
|
|
|
*/ |
19
|
10 |
|
public function __construct(ManagerRegistry $registry) |
20
|
|
|
{ |
21
|
10 |
|
$this->registry = $registry; |
22
|
10 |
|
} |
23
|
|
|
|
24
|
|
|
/** {@inheritdoc} */ |
25
|
8 |
|
final public function getEntityReference($fqcn, $property, $identifier) |
26
|
|
|
{ |
27
|
8 |
|
$metadata = $this->registry->getManagerForClass($fqcn)->getClassMetadata($fqcn); |
28
|
|
|
|
29
|
8 |
|
if (!$metadata->hasAssociation($property)) { |
30
|
6 |
|
return $identifier; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
$target = $metadata->getAssociationTargetClass($property); |
34
|
|
|
|
35
|
2 |
|
$targetMetadata = $this->registry->getManagerForClass($target)->getClassMetadata($target); |
36
|
|
|
|
37
|
2 |
|
if (is_array($identifier)) { |
38
|
|
|
$isNonCompositeIdentifier = count($targetMetadata->getIdentifier()) === 1; |
39
|
|
|
$isScalarArrayOfIdentifiers = array_keys($identifier) === range(0, count($identifier) - 1); |
40
|
|
|
if ($isNonCompositeIdentifier || $isScalarArrayOfIdentifiers) { |
41
|
|
|
$references = []; |
42
|
|
|
foreach ($identifier as $item) { |
43
|
|
|
$references[] = $this->getReference($target, $item); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $references; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
return $this->getReference($target, $identifier); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the reference of class |
55
|
|
|
* |
56
|
|
|
* @param string $fqcn |
57
|
|
|
* @param mixed $identifier |
58
|
|
|
* |
59
|
|
|
* @return object |
60
|
|
|
*/ |
61
|
|
|
abstract protected function getReference($fqcn, $identifier); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return ManagerRegistry |
65
|
|
|
*/ |
66
|
2 |
|
protected function getRegistry() |
67
|
|
|
{ |
68
|
2 |
|
return $this->registry; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|