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
|
|
|
private $map; |
12
|
|
|
|
13
|
|
|
private $entity; |
14
|
|
|
|
15
|
|
|
public function __construct( |
16
|
|
|
RelationDatamapper $mappaer |
17
|
|
|
) { |
18
|
|
|
$this->map = $mappaer->getMap(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setEntity(string $entity) |
22
|
|
|
{ |
23
|
|
|
$this->entity = $entity; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getFirstParentOf(string $innerEntity) |
27
|
|
|
{ |
28
|
|
|
return $this->keep( |
29
|
|
|
self::INDEX_ENTITY_PARENT, |
30
|
|
|
$innerEntity |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getSourceRelation(string $innerEntity) |
35
|
|
|
{ |
36
|
|
|
return $this->keep( |
37
|
|
|
self::INDEX_FK_RELATION_NAME, |
38
|
|
|
$innerEntity |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function clearMap(string $innerEntity) |
43
|
|
|
{ |
44
|
|
|
if (in_array($this->entity, $this->listOfParentsOf($innerEntity))) { |
45
|
|
|
foreach ($this->map as $root => $meta) { |
46
|
|
|
if ($this->entity != $root) { |
47
|
|
|
unset($this->map[$root]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getPathTo(string $innerEntity) |
54
|
|
|
{ |
55
|
|
|
$path = $this->getSourceRelation($innerEntity); |
56
|
|
|
|
57
|
|
|
if ($this->numberOfRealtionTo($innerEntity) != 1) { |
58
|
|
|
$this->clearMap($innerEntity); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($this->entity != $this->getFirstParentOf($innerEntity)) { |
62
|
|
|
if (!($relation = $this->getFirstParentOf($innerEntity))) { |
63
|
|
|
throw new \RuntimeException( |
64
|
|
|
'Oops! No one point to ' . $innerEntity . ';' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->getPathTo($relation) . '.' . $path; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $path; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function setQueryStartEntity(string $startEntity) |
75
|
|
|
{ |
76
|
|
|
$this->setEntity($startEntity); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getPathToEntity(string $entityToReach) |
80
|
|
|
{ |
81
|
|
|
return '_embedded.' . $this->getPathTo($entityToReach); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function keep($val, $innerEntity) |
85
|
|
|
{ |
86
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
87
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
88
|
|
|
if ($entity == $innerEntity) { |
89
|
|
|
return [$rootEntity, $name][$val]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function numberOfRealtionTo(string $entityToReach) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$numberOfRealtionTo = 0; |
98
|
|
|
|
99
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
100
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
101
|
|
|
if ($entity == $entityToReach) { |
102
|
|
|
$numberOfRealtionTo++; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $numberOfRealtionTo; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
View Code Duplication |
public function listOfParentsOf(string $entityToReach) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$parents = []; |
113
|
|
|
|
114
|
|
|
foreach ($this->map as $rootEntity => $meta) { |
115
|
|
|
foreach ($meta['relations'] as $name => $entity) { |
116
|
|
|
if ($entity == $entityToReach) { |
117
|
|
|
$parents[] = $rootEntity; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $parents; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
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.