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 Sherlock |
10
|
|
|
{ |
11
|
|
|
private $currentMetadata; |
|
|
|
|
12
|
|
|
|
13
|
|
|
private $manager; |
14
|
|
|
|
15
|
7 |
|
public function __construct( |
16
|
|
|
EntityManagerInterface $manager |
17
|
|
|
) { |
18
|
7 |
|
$this->manager = $manager; |
19
|
7 |
|
$this->metadata = CurrentMetaData::fromEntityManager($this->manager); |
|
|
|
|
20
|
7 |
|
} |
21
|
|
|
|
22
|
3 |
|
public function getShortOpList($entityPath) : array |
23
|
|
|
{ |
24
|
3 |
|
return $this->getAll([ |
25
|
3 |
|
'show_just_type' => true, |
26
|
|
|
'show_embedded' => true, |
27
|
3 |
|
])[$entityPath]; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
3 |
|
public function getOpList($entityPath) : array |
32
|
|
|
{ |
33
|
3 |
|
return $this->getAll()[$entityPath]; |
34
|
|
|
} |
35
|
|
|
|
36
|
6 |
|
public function getAll(array $options = []) : array |
37
|
|
|
{ |
38
|
6 |
|
$opList = []; |
39
|
|
|
|
40
|
6 |
|
foreach ($this->metadata->justEntitiesMetadata() as $entityClass) { |
41
|
6 |
|
if (isset($options['show_just_type'])) { |
42
|
3 |
|
$fields = $this->metadata->extractFieldsType($entityClass); |
43
|
|
|
} else { |
44
|
3 |
|
$fields = $this->metadata->extractFields($entityClass); |
45
|
|
|
} |
46
|
|
|
|
47
|
6 |
|
$entity = StringParser::dotNotationFor($entityClass); |
48
|
6 |
|
$opList[$entity]['fields'] = $fields; |
49
|
|
|
|
50
|
6 |
|
if (isset($options['show_embedded'])) { |
51
|
3 |
|
$relations = $this->metadata->getRelations(); |
52
|
3 |
|
$keys = array_keys($relations); |
53
|
3 |
|
$flip = array_flip($keys); |
54
|
3 |
|
foreach ($flip as $rel => $item) { |
55
|
3 |
|
$relationPath = $relations[$rel]; |
56
|
3 |
|
$newSherlock = new Sherlock($this->manager); |
57
|
3 |
|
$fields = $newSherlock->getFieldsType($relationPath)['fields']; |
58
|
3 |
|
$flip[$rel] = $fields; |
59
|
|
|
} |
60
|
3 |
|
$opList[$entity]['_embedded'] = $flip; |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
if ($this->metadata->hasRelations()) { |
64
|
6 |
|
$relations = $this->metadata->getRelations(); |
65
|
6 |
|
$opList[$entity]['relations'][] = $relations; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
return $opList; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
public function getRelations($entityPath) : array |
73
|
|
|
{ |
74
|
2 |
|
return current($this->getOpList($entityPath)['relations']); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
public function getFieldsType($entityPath) : array |
78
|
|
|
{ |
79
|
4 |
|
$fieldTypes = []; |
80
|
|
|
|
81
|
4 |
|
foreach ($this->metadata->justEntitiesMetadata() as $entityClass) { |
82
|
4 |
|
$fields = $this->metadata->extractFieldsType($entityClass); |
83
|
|
|
|
84
|
4 |
|
$entity = StringParser::dotNotationFor($entityClass); |
85
|
4 |
|
$fieldTypes[$entity]['fields'] = $fields; |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
return $fieldTypes[$entityPath]; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function willCall(string $fullyQualifiedClassName) : array |
92
|
|
|
{ |
93
|
1 |
|
return $this->getRelations($fullyQualifiedClassName); |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
public function getSearchable($entityPath) : array |
97
|
|
|
{ |
98
|
3 |
|
$raw = $this->getShortOpList($entityPath); |
99
|
|
|
|
100
|
3 |
|
$ultimateListOfSearchableFields = $raw['fields']; |
101
|
|
|
|
102
|
3 |
|
foreach ($raw['_embedded'] as $em => $bedded) { |
103
|
3 |
|
foreach ($raw['_embedded'][$em] as $foo => $bar) { |
104
|
3 |
|
$ultimateListOfSearchableFields[$em .'.'. $foo] = $bar; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
return $ultimateListOfSearchableFields; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|