1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchBundle\Generator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Document Generator |
16
|
|
|
*/ |
17
|
|
|
class DocumentGenerator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $spaces = ' '; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $getMethodTemplate = |
28
|
|
|
'/** |
29
|
|
|
* Returns <fieldName> |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function get<methodName>() |
34
|
|
|
{ |
35
|
|
|
<spaces>return $this-><fieldName>; |
36
|
|
|
}'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $setMethodTemplate = |
42
|
|
|
'/** |
43
|
|
|
* Sets <fieldName> |
44
|
|
|
* |
45
|
|
|
* @param string $<fieldName> |
46
|
|
|
*/ |
47
|
|
|
public function set<methodName>($<fieldName>) |
48
|
|
|
{ |
49
|
|
|
<spaces>$this-><fieldName> = $<fieldName>; |
50
|
|
|
}'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $metadata |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function generateDocumentClass(array $metadata) |
58
|
|
|
{ |
59
|
|
|
return implode( |
60
|
|
|
"\n", |
61
|
|
|
[ |
62
|
|
|
"<?php\n", |
63
|
|
|
sprintf('namespace %s;', substr($metadata['name'], 0, strrpos($metadata['name'], '\\'))) . "\n", |
64
|
|
|
"use ONGR\\ElasticsearchBundle\\Annotation as ES;\n", |
65
|
|
|
$this->generateDocumentDocBlock($metadata), |
66
|
|
|
'class ' . $this->getClassName($metadata), |
67
|
|
|
"{", |
68
|
|
|
str_replace('<spaces>', $this->spaces, $this->generateDocumentBody($metadata)), |
69
|
|
|
"}\n" |
70
|
|
|
] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Generates document body |
76
|
|
|
* |
77
|
|
|
* @param array $metadata |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
private function generateDocumentBody(array $metadata) |
82
|
|
|
{ |
83
|
|
|
$lines = []; |
84
|
|
|
|
85
|
|
|
if ($properties = $this->generateDocumentProperties($metadata)) { |
86
|
|
|
$lines[] = $properties; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($methods = $this->generateDocumentMethods($metadata)) { |
90
|
|
|
$lines[] = $methods; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return rtrim(implode("\n", $lines)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Generates document properties |
98
|
|
|
* |
99
|
|
|
* @param array $metadata |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
private function generateDocumentProperties(array $metadata) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$lines = []; |
106
|
|
|
|
107
|
|
|
foreach ($metadata['properties'] as $property) { |
108
|
|
|
$lines[] = $this->generatePropertyDocBlock($property); |
109
|
|
|
$lines[] = $this->spaces . 'private $' . $property['field_name'] . ";\n"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return implode("\n", $lines); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Generates document methods |
117
|
|
|
* |
118
|
|
|
* @param array $metadata |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
View Code Duplication |
private function generateDocumentMethods(array $metadata) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$lines = []; |
125
|
|
|
|
126
|
|
|
foreach ($metadata['properties'] as $property) { |
127
|
|
|
$lines[] = $this->generateDocumentMethod($property, $this->setMethodTemplate) . "\n"; |
128
|
|
|
$lines[] = $this->generateDocumentMethod($property, $this->getMethodTemplate) . "\n"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return implode("\n", $lines); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Generates document method |
136
|
|
|
* |
137
|
|
|
* @param array $metadata |
138
|
|
|
* @param string $template |
139
|
|
|
* |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
private function generateDocumentMethod(array $metadata, $template) |
143
|
|
|
{ |
144
|
|
|
return $this->prefixWithSpaces( |
145
|
|
|
str_replace( |
146
|
|
|
['<methodName>', '<fieldName>'], |
147
|
|
|
[ucfirst($metadata['field_name']), $metadata['field_name']], |
148
|
|
|
$template |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns property doc block |
155
|
|
|
* |
156
|
|
|
* @param array $metadata |
157
|
|
|
* |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private function generatePropertyDocBlock(array $metadata) |
161
|
|
|
{ |
162
|
|
|
$lines = [ |
163
|
|
|
$this->spaces . '/**', |
164
|
|
|
$this->spaces . ' * @var string', |
165
|
|
|
$this->spaces . ' *', |
166
|
|
|
]; |
167
|
|
|
|
168
|
|
|
$column = []; |
169
|
|
|
if (isset($metadata['property_name']) && $metadata['property_name'] != $metadata['field_name']) { |
170
|
|
|
$column[] = 'name="' . $metadata['property_name'] . '"'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if (isset($metadata['property_class'])) { |
174
|
|
|
$column[] = 'class="' . $metadata['property_class'] . '"'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (isset($metadata['property_multiple']) && $metadata['property_multiple']) { |
178
|
|
|
$column[] = 'multiple=true'; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if (isset($metadata['property_type']) && $metadata['annotation'] == 'Property') { |
182
|
|
|
$column[] = 'type="' . $metadata['property_type'] . '"'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if (isset($metadata['property_default'])) { |
186
|
|
|
$column[] = 'default="' . $metadata['property_default'] . '"'; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (isset($metadata['property_options']) && $metadata['property_options']) { |
190
|
|
|
$column[] = 'options={' . $metadata['property_options'] . '}'; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$lines[] = $this->spaces . ' * @ES\\' . $metadata['annotation'] . '(' . implode(', ', $column) . ')'; |
194
|
|
|
|
195
|
|
|
$lines[] = $this->spaces . ' */'; |
196
|
|
|
|
197
|
|
|
return implode("\n", $lines); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Generates document doc block |
202
|
|
|
* |
203
|
|
|
* @param array $metadata |
204
|
|
|
* |
205
|
|
|
* @return string |
206
|
|
|
*/ |
207
|
|
|
private function generateDocumentDocBlock(array $metadata) |
208
|
|
|
{ |
209
|
|
|
return str_replace( |
210
|
|
|
['<className>', '<annotation>', '<options>'], |
211
|
|
|
[ |
212
|
|
|
$this->getClassName($metadata), |
213
|
|
|
$metadata['annotation'], |
214
|
|
|
$metadata['type'] != lcfirst($this->getClassName($metadata)) |
215
|
|
|
? sprintf('type="%s"', $metadata['type']) : '', |
216
|
|
|
], |
217
|
|
|
'/** |
218
|
|
|
* <className> |
219
|
|
|
* |
220
|
|
|
* @ES\<annotation>(<options>) |
221
|
|
|
*/' |
222
|
|
|
); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $code |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
private function prefixWithSpaces($code) |
231
|
|
|
{ |
232
|
|
|
$lines = explode("\n", $code); |
233
|
|
|
|
234
|
|
|
foreach ($lines as $key => $value) { |
235
|
|
|
if ($value) { |
236
|
|
|
$lines[$key] = $this->spaces . $lines[$key]; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return implode("\n", $lines); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns class name |
245
|
|
|
* |
246
|
|
|
* @param array $metadata |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
private function getClassName(array $metadata) |
251
|
|
|
{ |
252
|
|
|
return ($pos = strrpos($metadata['name'], '\\')) |
253
|
|
|
? substr($metadata['name'], $pos + 1, strlen($metadata['name'])) : $metadata['name']; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
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.