1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Saxulum\ElasticSearchQueryBuilder\Generator; |
6
|
|
|
|
7
|
|
|
use PhpParser\Node\Arg; |
8
|
|
|
use PhpParser\Node\Expr; |
9
|
|
|
use PhpParser\Node\Expr\Assign; |
10
|
|
|
use PhpParser\Node\Expr\ConstFetch; |
11
|
|
|
use PhpParser\Node\Expr\MethodCall; |
12
|
|
|
use PhpParser\Node\Expr\New_; |
13
|
|
|
use PhpParser\Node\Expr\Variable; |
14
|
|
|
use PhpParser\Node\Name; |
15
|
|
|
use PhpParser\Node\Scalar\DNumber; |
16
|
|
|
use PhpParser\Node\Scalar\LNumber; |
17
|
|
|
use PhpParser\Node\Scalar\String_; |
18
|
|
|
use PhpParser\PrettyPrinter\Standard as PhpGenerator; |
19
|
|
|
|
20
|
|
|
final class QueryBuilderGenerator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var PhpGenerator |
24
|
|
|
*/ |
25
|
|
|
private $phpGenerator; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param PhpGenerator $phpGenerator |
29
|
|
|
*/ |
30
|
13 |
|
public function __construct(PhpGenerator $phpGenerator) |
31
|
|
|
{ |
32
|
13 |
|
$this->phpGenerator = $phpGenerator; |
33
|
13 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $query |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
13 |
|
public function generateByJson($query): string |
40
|
|
|
{ |
41
|
13 |
|
$data = json_decode($query, false); |
42
|
13 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
43
|
1 |
|
throw new \InvalidArgumentException(sprintf('Message: %s, query: %s', json_last_error_msg(), $query)); |
44
|
|
|
} |
45
|
|
|
|
46
|
12 |
|
$queryBuilder = new Variable('queryBuilder'); |
47
|
|
|
|
48
|
12 |
|
$stmts = []; |
49
|
|
|
|
50
|
12 |
|
$stmts[] = $this->createQueryBuilderNode(); |
51
|
12 |
|
$stmts[] = $this->appendChildrenToObjectNode($queryBuilder, $queryBuilder, $data); |
52
|
|
|
|
53
|
12 |
|
return $this->structureCode($this->phpGenerator->prettyPrint($stmts)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return Expr |
58
|
|
|
*/ |
59
|
12 |
|
private function createQueryBuilderNode(): Expr |
60
|
|
|
{ |
61
|
12 |
|
return new Assign(new Variable('queryBuilder'), new New_(new Name('QueryBuilder'))); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Expr $expr |
66
|
|
|
* @return Expr |
67
|
|
|
*/ |
68
|
12 |
|
private function createObjectNode(Expr $expr): Expr |
69
|
|
|
{ |
70
|
12 |
|
return new MethodCall($expr, 'objectNode'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param Expr $expr |
75
|
|
|
* @return Expr |
76
|
|
|
*/ |
77
|
2 |
|
private function createArrayNode(Expr $expr): Expr |
78
|
|
|
{ |
79
|
2 |
|
return new MethodCall($expr, 'arrayNode'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Expr $expr |
84
|
|
|
* @param string|float|int|bool|null $value |
85
|
|
|
* @return Expr |
86
|
|
|
*/ |
87
|
11 |
View Code Duplication |
private function createScalarNode(Expr $expr, $value): Expr |
|
|
|
|
88
|
|
|
{ |
89
|
11 |
|
if (is_string($value)) { |
90
|
10 |
|
$valueExpr = new String_($value); |
91
|
4 |
|
} elseif (is_int($value)) { |
92
|
4 |
|
$valueExpr = new LNumber($value); |
93
|
1 |
|
} elseif (is_float($value)) { |
94
|
1 |
|
$valueExpr = new DNumber($value); |
95
|
1 |
|
} elseif (is_bool($value)) { |
96
|
1 |
|
$valueExpr = new ConstFetch(new Name($value ? 'true' : 'false')); |
97
|
|
|
} else { |
98
|
1 |
|
$valueExpr = new ConstFetch(new Name('null')); |
99
|
|
|
} |
100
|
|
|
|
101
|
11 |
|
return new MethodCall($expr, 'scalarNode', [new Arg($valueExpr)]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Expr $queryBuilder |
106
|
|
|
* @param Expr $expr |
107
|
|
|
* @param array $data |
108
|
|
|
* @return Expr |
109
|
|
|
*/ |
110
|
2 |
|
private function appendChildrenToArrayNode(Expr $queryBuilder, Expr $expr, array $data) |
111
|
|
|
{ |
112
|
2 |
|
foreach ($data as $value) { |
113
|
2 |
View Code Duplication |
if ($value instanceof \stdClass) { |
|
|
|
|
114
|
1 |
|
$argument = $this->createObjectNode($queryBuilder); |
115
|
2 |
|
} elseif (is_array($value)) { |
116
|
1 |
|
$argument = $this->createArrayNode($queryBuilder); |
117
|
|
|
} else { |
118
|
1 |
|
$argument = $this->createScalarNode($queryBuilder, $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
2 |
|
$expr = new MethodCall($expr, 'addToArrayNode', [new Arg($argument)]); |
122
|
|
|
|
123
|
2 |
View Code Duplication |
if ($value instanceof \stdClass) { |
|
|
|
|
124
|
1 |
|
$expr = new MethodCall($this->appendChildrenToObjectNode($queryBuilder, $expr, $value), 'end'); |
125
|
2 |
|
} elseif (is_array($value)) { |
126
|
2 |
|
$expr = new MethodCall($this->appendChildrenToArrayNode($queryBuilder, $expr, $value), 'end'); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
return $expr; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Expr $queryBuilder |
135
|
|
|
* @param Expr $expr |
136
|
|
|
* @param \stdClass $data |
137
|
|
|
* @return Expr |
138
|
|
|
*/ |
139
|
12 |
|
private function appendChildrenToObjectNode(Expr $queryBuilder, Expr $expr, \stdClass $data) |
140
|
|
|
{ |
141
|
12 |
|
foreach ($data as $key => $value) { |
|
|
|
|
142
|
12 |
View Code Duplication |
if ($value instanceof \stdClass) { |
|
|
|
|
143
|
12 |
|
$argument = $this->createObjectNode($queryBuilder); |
144
|
11 |
|
} elseif (is_array($value)) { |
145
|
2 |
|
$argument = $this->createArrayNode($queryBuilder); |
146
|
|
|
} else { |
147
|
11 |
|
$argument = $this->createScalarNode($queryBuilder, $value); |
148
|
|
|
} |
149
|
|
|
|
150
|
12 |
|
$expr = new MethodCall($expr, 'addToObjectNode', [new Arg(new String_($key)), new Arg($argument)]); |
151
|
|
|
|
152
|
12 |
View Code Duplication |
if ($value instanceof \stdClass) { |
|
|
|
|
153
|
12 |
|
$expr = new MethodCall($this->appendChildrenToObjectNode($queryBuilder, $expr, $value), 'end'); |
154
|
11 |
|
} elseif (is_array($value)) { |
155
|
12 |
|
$expr = new MethodCall($this->appendChildrenToArrayNode($queryBuilder, $expr, $value), 'end'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
12 |
|
return $expr; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $code |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
12 |
|
private function structureCode(string $code): string |
167
|
|
|
{ |
168
|
12 |
|
$lines = $this->getLinesByCode($code); |
169
|
|
|
|
170
|
12 |
|
$position = 0; |
171
|
|
|
|
172
|
12 |
|
$structuredLines = []; |
173
|
|
|
|
174
|
12 |
|
foreach ($lines as $i => $line) { |
175
|
12 |
|
$lastLine = $lines[$i-1] ?? ''; |
176
|
12 |
|
$this->structuredLine($line, $lastLine, $position, $structuredLines); |
177
|
|
|
} |
178
|
|
|
|
179
|
12 |
|
return implode("\n", $structuredLines); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $code |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
12 |
|
private function getLinesByCode(string $code): array |
187
|
|
|
{ |
188
|
12 |
|
$codeWithLinebreaks = str_replace('->add', "\n->add", $code); |
189
|
12 |
|
$codeWithLinebreaks = str_replace('->end', "\n->end", $codeWithLinebreaks); |
190
|
|
|
|
191
|
12 |
|
return explode("\n", $codeWithLinebreaks); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $line |
196
|
|
|
* @param string $lastLine |
197
|
|
|
* @param int $position |
198
|
|
|
* @param array $structuredLines |
199
|
|
|
*/ |
200
|
12 |
|
private function structuredLine(string $line, string $lastLine, int &$position, array &$structuredLines) |
201
|
|
|
{ |
202
|
12 |
|
if (0 === strpos($line, '->add')) { |
203
|
12 |
|
if (false === strpos($lastLine, '->end') && false === strpos($lastLine, '->scalarNode')) { |
204
|
12 |
|
$position++; |
205
|
|
|
} |
206
|
|
|
|
207
|
12 |
|
$structuredLines[] = str_pad('', $position * 4) . $line; |
208
|
|
|
|
209
|
12 |
|
return; |
210
|
|
|
} |
211
|
|
|
|
212
|
12 |
|
if (0 === strpos($line, '->end')) { |
213
|
12 |
|
if (strpos($lastLine, '->objectNode') || strpos($lastLine, '->arrayNode')) { |
214
|
1 |
|
$structuredLines[count($structuredLines) - 1] .= '->end()'; |
215
|
|
|
|
216
|
1 |
|
return; |
217
|
|
|
} |
218
|
|
|
|
219
|
12 |
|
$position--; |
220
|
|
|
|
221
|
12 |
|
$structuredLines[] = str_pad('', $position * 4) . $line; |
222
|
|
|
|
223
|
12 |
|
return; |
224
|
|
|
} |
225
|
|
|
|
226
|
12 |
|
$structuredLines[] = $line; |
227
|
12 |
|
} |
228
|
|
|
} |
229
|
|
|
|
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.