1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Component\Meta; |
4
|
|
|
|
5
|
|
|
class JsonPathFinder |
6
|
|
|
{ |
7
|
|
|
const INDEX_ENTITY_PARENT = 0; |
8
|
|
|
|
9
|
|
|
const INDEX_FK_RELATION_NAME = 1; |
10
|
|
|
|
11
|
|
|
const INDEX_ENTITY_FIRST_CHILD = 2; |
12
|
|
|
|
13
|
|
|
private $map; |
14
|
|
|
|
15
|
|
|
private $entity; |
16
|
|
|
|
17
|
|
|
private $entitiesPath = []; |
18
|
|
|
|
19
|
|
|
private $skipSteps = []; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
RelationDatamapper $mappaer |
23
|
|
|
) { |
24
|
|
|
$this->map = $mappaer->getMap(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setEntity(string $entity) |
28
|
|
|
{ |
29
|
|
|
$this->entity = $entity; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getFirstParentOf(string $innerEntity) |
33
|
|
|
{ |
34
|
|
|
return $this->keep( |
35
|
|
|
self::INDEX_ENTITY_PARENT, |
36
|
|
|
$innerEntity |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getFirstChildOf(string $innerEntity) |
41
|
|
|
{ |
42
|
|
|
return $this->keep( |
43
|
|
|
self::INDEX_ENTITY_FIRST_CHILD, |
44
|
|
|
$innerEntity |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getSourceRelation(string $innerEntity) |
49
|
|
|
{ |
50
|
|
|
return $this->keep( |
51
|
|
|
self::INDEX_FK_RELATION_NAME, |
52
|
|
|
$innerEntity |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function clearMap(string $innerEntity) |
57
|
|
|
{ |
58
|
|
|
if (in_array($this->entity, $this->listOfParentsOf($innerEntity))) { |
59
|
|
|
foreach ($this->map as $root => $meta) { |
60
|
|
|
if ($this->entity != $root) { |
61
|
|
|
unset($this->map[$root]); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getPathTo(string $innerEntity = '') |
68
|
|
|
{ |
69
|
|
|
$this->entitiesPath[] = $innerEntity; |
70
|
|
|
$mem = $innerEntity; |
|
|
|
|
71
|
|
|
|
72
|
|
|
$path = $this->getSourceRelation($innerEntity); |
73
|
|
|
|
74
|
|
|
if ($this->numberOfRealtionTo($innerEntity) != 1) { |
75
|
|
|
foreach ($this->listOfParentsOf($innerEntity) as $parentEntityName) { |
76
|
|
|
//throw new \RuntimeException("\n" . var_export(json_encode(json_decode(json_encode([ |
77
|
|
|
//'innerEntity' => $innerEntity, |
78
|
|
|
//'numberOfRealtionTo' => $this->numberOfRealtionTo($innerEntity), |
79
|
|
|
//'listOfParentsOf ('.$innerEntity.')' => $this->listOfParentsOf($innerEntity), |
80
|
|
|
//'skipSteps' => $this->skipSteps, |
81
|
|
|
//'parentEntityName' => $parentEntityName, |
82
|
|
|
//])), JSON_PRETTY_PRINT), true)); |
83
|
|
|
|
84
|
|
|
if (isset($this->skipSteps[$parentEntityName])) { |
85
|
|
|
if ($this->skipSteps[$parentEntityName] == $innerEntity) { |
86
|
|
|
$this->clearMap($parentEntityName); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($innerEntity != $this->entity) { |
92
|
|
|
$this->clearMap($innerEntity); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($this->entity != $this->getFirstParentOf($innerEntity)) { |
97
|
|
|
if (!($relation = $this->getFirstParentOf($innerEntity))) { |
98
|
|
|
throw new \RuntimeException(var_export([ |
99
|
|
|
'innerEntity' => $innerEntity, |
100
|
|
|
'relation' => $relation, |
101
|
|
|
], true)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->getPathTo($relation) . '.' . $path; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $path; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function setQueryStartEntity(string $startEntity) |
111
|
|
|
{ |
112
|
|
|
$this->setEntity($startEntity); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getPathToEntity(string $entityToReach) |
116
|
|
|
{ |
117
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
118
|
|
|
if (isset($this->skipSteps[$rootEntity])) { |
119
|
|
|
unset($this->map[$rootEntity]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return '_embedded.' . $this->getPathTo($entityToReach); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function keep($val, $innerEntity) |
127
|
|
|
{ |
128
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
129
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
130
|
|
|
if (self::INDEX_ENTITY_FIRST_CHILD == $val) { |
131
|
|
|
return $entity; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($entity == $innerEntity) { |
135
|
|
|
$return = [ |
136
|
|
|
self::INDEX_ENTITY_PARENT => $rootEntity, |
137
|
|
|
self::INDEX_FK_RELATION_NAME => $name, |
138
|
|
|
][$val]; |
139
|
|
|
|
140
|
|
|
return $return; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
View Code Duplication |
public function numberOfRealtionTo(string $entityToReach) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$numberOfRealtionTo = 0; |
149
|
|
|
|
150
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
151
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
152
|
|
|
if ($entity == $entityToReach) { |
153
|
|
|
$numberOfRealtionTo++; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $numberOfRealtionTo; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
View Code Duplication |
public function listOfParentsOf(string $entityToReach) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
$parents = []; |
164
|
|
|
|
165
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
166
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
167
|
|
|
if ($entity == $entityToReach) { |
168
|
|
|
$parents[] = $rootEntity; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $parents; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getEntitiesPath() |
177
|
|
|
{ |
178
|
|
|
return $this->entitiesPath; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function skipStep($parent, $child) |
182
|
|
|
{ |
183
|
|
|
$this->skipSteps[$parent] = $child; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|