1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Component\Sherlock; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Mado\QueryBundle\Dictionary; |
7
|
|
|
use Mado\QueryBundle\Services\StringParser; |
8
|
|
|
|
9
|
|
|
class CurrentMetaData |
10
|
|
|
{ |
11
|
|
|
private $metadata; |
12
|
|
|
|
13
|
|
|
private $manager; |
14
|
|
|
|
15
|
|
|
private $currentMetadata; |
16
|
|
|
|
17
|
7 |
|
private function __construct(EntityManagerInterface $manager) |
18
|
|
|
{ |
19
|
7 |
|
$this->manager = $manager; |
20
|
|
|
|
21
|
7 |
|
$metadata = $this->manager |
22
|
7 |
|
->getMetaDataFactory() |
23
|
7 |
|
->getAllMetaData(); |
24
|
|
|
|
25
|
7 |
|
$this->metadata = $metadata; |
26
|
7 |
|
} |
27
|
|
|
|
28
|
7 |
|
public static function fromEntityManager(EntityManagerInterface $manager) : CurrentMetaData |
29
|
|
|
{ |
30
|
7 |
|
return new self($manager); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function justEntitiesMetadata() : array |
34
|
|
|
{ |
35
|
7 |
|
return array_map(function ($item) { |
36
|
7 |
|
return $item->rootEntityName; |
37
|
7 |
|
}, $this->metadata); |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
public function extractFields($entityClass) : array |
41
|
|
|
{ |
42
|
3 |
|
return $this->extraction( |
43
|
|
|
$entityClass, |
44
|
3 |
|
function ($item) { |
45
|
3 |
|
return Dictionary::getOperatorsFromDoctrineType($item); |
46
|
3 |
|
} |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public function extractFieldsType($entityClass) : array |
51
|
|
|
{ |
52
|
4 |
|
return $this->extraction( |
53
|
|
|
$entityClass, |
54
|
4 |
|
function ($item) { |
55
|
4 |
|
return $item; |
56
|
4 |
|
} |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
7 |
|
private function extraction($entityClass, callable $step) : array |
61
|
|
|
{ |
62
|
7 |
|
$this->currentMetadata = $this->manager->getClassMetadata($entityClass); |
63
|
|
|
|
64
|
7 |
|
return array_map(function ($item) use ($step) { |
65
|
7 |
|
return $step($item['type']); |
66
|
7 |
|
}, $this->currentMetadata->fieldMappings); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
public function hasRelations() : bool |
70
|
|
|
{ |
71
|
6 |
|
return count($this->getCurrentAssociationMapping()) > 0; |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
public function getCurrentAssociationMapping() |
75
|
|
|
{ |
76
|
6 |
|
return $this->currentMetadata->associationMappings; |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
public function getRelations() : array |
80
|
|
|
{ |
81
|
6 |
|
$relations = []; |
82
|
|
|
|
83
|
6 |
|
foreach ($this->getCurrentAssociationMapping() as $rel) { |
84
|
6 |
|
$relationName = $rel['inversedBy'] == '' |
85
|
6 |
|
? $rel['mappedBy'] |
86
|
6 |
|
: $rel['inversedBy']; |
87
|
|
|
|
88
|
6 |
|
$relations[$relationName] = StringParser::dotNotationFor( |
89
|
6 |
|
$rel['targetEntity'] |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
6 |
|
return $relations; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|