|
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
|
3 |
|
private function __construct(EntityManagerInterface $manager) |
|
18
|
|
|
{ |
|
19
|
3 |
|
$this->manager = $manager; |
|
20
|
|
|
|
|
21
|
3 |
|
$metadata = $this->manager |
|
22
|
3 |
|
->getMetaDataFactory() |
|
23
|
3 |
|
->getAllMetaData(); |
|
24
|
|
|
|
|
25
|
3 |
|
$this->metadata = $metadata; |
|
26
|
3 |
|
} |
|
27
|
|
|
|
|
28
|
3 |
|
public static function fromEntityManager(EntityManagerInterface $manager) : CurrentMetaData |
|
29
|
|
|
{ |
|
30
|
3 |
|
return new self($manager); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function justEntitiesMetadata() : array |
|
34
|
|
|
{ |
|
35
|
3 |
|
return array_map(function ($item) { |
|
36
|
3 |
|
return $item->rootEntityName; |
|
37
|
3 |
|
}, $this->metadata); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function extractFields($entityClass) : array |
|
41
|
|
|
{ |
|
42
|
2 |
|
$this->currentMetadata = $this->manager->getClassMetadata($entityClass); |
|
43
|
|
|
|
|
44
|
2 |
|
return array_map(function ($item) { |
|
45
|
2 |
|
return Dictionary::getOperatorsFromDoctrineType($item['type']); |
|
46
|
2 |
|
}, $this->currentMetadata->fieldMappings); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function extractFieldsType($entityClass) : array |
|
50
|
|
|
{ |
|
51
|
1 |
|
$this->currentMetadata = $this->manager->getClassMetadata($entityClass); |
|
52
|
|
|
|
|
53
|
1 |
|
return array_map(function ($item) { |
|
54
|
1 |
|
return $item['type']; |
|
55
|
1 |
|
}, $this->currentMetadata->fieldMappings); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
2 |
|
public function hasRelations() : bool |
|
59
|
|
|
{ |
|
60
|
2 |
|
return count($this->currentMetadata->associationMappings) > 0; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function getRelations() : array |
|
64
|
|
|
{ |
|
65
|
2 |
|
$relations = []; |
|
66
|
|
|
|
|
67
|
2 |
|
foreach ($this->currentMetadata->associationMappings as $rel) { |
|
68
|
|
|
$relations[ |
|
69
|
2 |
|
$rel['inversedBy'] |
|
70
|
2 |
|
] = StringParser::dotNotationFor($rel['targetEntity']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
return $relations; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|