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 $wrongPath = []; |
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 ( |
75
|
|
|
$this->numberOfRealtionTo($innerEntity) != 1 && |
76
|
|
|
$innerEntity != $this->entity |
77
|
|
|
) { |
78
|
|
|
$this->clearMap($innerEntity); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($this->entity != $this->getFirstParentOf($innerEntity)) { |
82
|
|
|
if (!($relation = $this->getFirstParentOf($innerEntity))) { |
83
|
|
|
throw new \RuntimeException(var_export([ |
84
|
|
|
'innerEntity' => $innerEntity, |
85
|
|
|
'relation' => $relation, |
86
|
|
|
], true)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->getPathTo($relation) . '.' . $path; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $path; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function setQueryStartEntity(string $startEntity) |
96
|
|
|
{ |
97
|
|
|
$this->setEntity($startEntity); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getPathToEntity(string $entityToReach) |
101
|
|
|
{ |
102
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
103
|
|
|
if (in_array($rootEntity, $this->wrongPath)) { |
104
|
|
|
unset($this->map[$rootEntity]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return '_embedded.' . $this->getPathTo($entityToReach); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function keep($val, $innerEntity) |
112
|
|
|
{ |
113
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
114
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
115
|
|
|
if (self::INDEX_ENTITY_FIRST_CHILD == $val) { |
116
|
|
|
return $entity; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($entity == $innerEntity) { |
120
|
|
|
$return = [ |
121
|
|
|
self::INDEX_ENTITY_PARENT => $rootEntity, |
122
|
|
|
self::INDEX_FK_RELATION_NAME => $name, |
123
|
|
|
][$val]; |
124
|
|
|
|
125
|
|
|
return $return; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
View Code Duplication |
public function numberOfRealtionTo(string $entityToReach) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$numberOfRealtionTo = 0; |
134
|
|
|
|
135
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
136
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
137
|
|
|
if ($entity == $entityToReach) { |
138
|
|
|
$numberOfRealtionTo++; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $numberOfRealtionTo; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
View Code Duplication |
public function listOfParentsOf(string $entityToReach) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$parents = []; |
149
|
|
|
|
150
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
151
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
152
|
|
|
if ($entity == $entityToReach) { |
153
|
|
|
$parents[] = $rootEntity; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $parents; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getEntitiesPath() |
162
|
|
|
{ |
163
|
|
|
return $this->entitiesPath; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function addWrongPathTo($parentToSkip) |
167
|
|
|
{ |
168
|
|
|
$this->wrongPath[] = $parentToSkip; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|