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 $mapper |
23
|
|
|
) { |
24
|
|
|
$this->map = $mapper->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
|
|
|
|
71
|
|
|
$path = $this->getSourceRelation($innerEntity); |
72
|
|
|
|
73
|
|
|
if ($this->numberOfRelationsToEntity($innerEntity) != 1) { |
74
|
|
|
$this->clearMap($innerEntity); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($this->entity != $this->getFirstParentOf($innerEntity)) { |
78
|
|
|
if (!($relation = $this->getFirstParentOf($innerEntity))) { |
79
|
|
|
throw new Exceptions\UnreachablePathException(var_export([ |
80
|
|
|
'innerEntity' => $innerEntity, |
81
|
|
|
'relation' => $relation, |
82
|
|
|
], true)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->getPathTo($relation) . '.' . $path; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $path; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function setQueryStartEntity(string $startEntity) |
92
|
|
|
{ |
93
|
|
|
$this->setEntity($startEntity); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getPathToEntity(string $entityToReach) |
97
|
|
|
{ |
98
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
99
|
|
|
if (in_array($rootEntity, $this->wrongPath)) { |
100
|
|
|
unset($this->map[$rootEntity]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return '_embedded.' . $this->getPathTo($entityToReach); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function keep($val, $innerEntity) |
108
|
|
|
{ |
109
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
110
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
111
|
|
|
if (self::INDEX_ENTITY_FIRST_CHILD == $val) { |
112
|
|
|
return $entity; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($entity == $innerEntity) { |
116
|
|
|
$return = [ |
117
|
|
|
self::INDEX_ENTITY_PARENT => $rootEntity, |
118
|
|
|
self::INDEX_FK_RELATION_NAME => $name, |
119
|
|
|
][$val]; |
120
|
|
|
|
121
|
|
|
return $return; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
View Code Duplication |
public function numberOfRelationsToEntity(string $entityToReach) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$numberOfRelationsToEntity = 0; |
130
|
|
|
|
131
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
132
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
133
|
|
|
if ($entity == $entityToReach) { |
134
|
|
|
$numberOfRelationsToEntity++; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $numberOfRelationsToEntity; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
View Code Duplication |
public function listOfParentsOf(string $entityToReach) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$parents = []; |
145
|
|
|
|
146
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
147
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
148
|
|
|
if ($entity == $entityToReach) { |
149
|
|
|
$parents[] = $rootEntity; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $parents; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getEntitiesPath() |
158
|
|
|
{ |
159
|
|
|
return $this->entitiesPath; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function removeStep($parentToSkip) |
163
|
|
|
{ |
164
|
|
|
$this->wrongPath[] = $parentToSkip; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.