1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tpg\ExtjsBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\Annotation; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
7
|
|
|
use Doctrine\ORM\Mapping\JoinColumn; |
8
|
|
|
use Doctrine\ORM\Mapping\JoinColumns; |
9
|
|
|
use Doctrine\ORM\Mapping\OneToMany; |
10
|
|
|
use JMS\Serializer\Annotation\Exclude; |
11
|
|
|
use JMS\Serializer\Annotation\ExclusionPolicy; |
12
|
|
|
use JMS\Serializer\Annotation\Expose; |
13
|
|
|
use Symfony\Bundle\TwigBundle\TwigEngine; |
14
|
|
|
use Symfony\Component\Finder\Finder; |
15
|
|
|
use Tpg\ExtjsBundle\Annotation\Direct; |
16
|
|
|
use Tpg\ExtjsBundle\Annotation\Model; |
17
|
|
|
use Tpg\ExtjsBundle\Annotation\ModelProxy; |
18
|
|
|
|
19
|
|
|
class GeneratorService { |
20
|
|
|
|
21
|
|
|
/** @var AnnotationReader */ |
22
|
|
|
protected $annoReader; |
23
|
|
|
|
24
|
|
|
/** @var $twig TwigEngine */ |
25
|
|
|
protected $twig; |
26
|
|
|
|
27
|
|
|
protected $remotingBundles = array(); |
28
|
|
|
protected $fieldsParams = array(); |
29
|
|
|
|
30
|
12 |
|
public function setAnnotationReader($reader) { |
31
|
12 |
|
$this->annoReader = $reader; |
32
|
12 |
|
} |
33
|
|
|
|
34
|
12 |
|
public function setTwigEngine($engine) { |
35
|
12 |
|
$this->twig = $engine; |
36
|
12 |
|
} |
37
|
|
|
|
38
|
2 |
|
public function setRemotingBundles($bundles) { |
39
|
2 |
|
$this->remotingBundles = $bundles; |
40
|
2 |
|
} |
41
|
8 |
|
public function setModelFieldsParameters($fieldsParams) { |
42
|
8 |
|
$this->fieldsParams = $fieldsParams; |
43
|
8 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Generate Remote API from a list of controllers |
47
|
|
|
*/ |
48
|
1 |
|
public function generateRemotingApi() { |
49
|
1 |
|
$list = array(); |
50
|
1 |
|
foreach($this->remotingBundles as $bundle) { |
51
|
1 |
|
$bundleRef = new \ReflectionClass($bundle); |
52
|
1 |
|
$controllerDir = new Finder(); |
53
|
1 |
|
$controllerDir->files()->in(dirname($bundleRef->getFileName()).'/Controller/')->name('/.*Controller\.php$/'); |
54
|
1 |
|
foreach($controllerDir as $controllerFile) { |
55
|
1 |
|
$controller = $bundleRef->getNamespaceName() . "\\Controller\\" . substr($controllerFile->getFilename(), 0, -4); |
56
|
1 |
|
$controllerRef = new \ReflectionClass($controller); |
57
|
1 |
|
foreach ($controllerRef->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
58
|
|
|
/** @var $methodDirectAnnotation Direct */ |
59
|
1 |
|
$methodDirectAnnotation = $this->annoReader->getMethodAnnotation($method, 'Tpg\ExtjsBundle\Annotation\Direct'); |
60
|
1 |
|
if ($methodDirectAnnotation !== null) { |
61
|
1 |
|
$nameSpace = str_replace("\\", ".", $bundleRef->getNamespaceName()); |
62
|
1 |
|
$className = str_replace("Controller", "", $controllerRef->getShortName()); |
63
|
1 |
|
$methodName = str_replace("Action", "", $method->getName()); |
64
|
1 |
|
$list[$nameSpace][$className][] = array( |
65
|
1 |
|
'name'=>$methodName, |
66
|
1 |
|
'len' => count($method->getParameters()) |
67
|
1 |
|
); |
68
|
1 |
|
} |
69
|
1 |
|
} |
70
|
1 |
|
} |
71
|
1 |
|
} |
72
|
1 |
|
return $list; |
73
|
|
|
} |
74
|
|
|
|
75
|
10 |
|
public function generateMarkupForEntity($entity) { |
76
|
10 |
|
$classRef = new \ReflectionClass($entity); |
77
|
|
|
/** @var $classModelAnnotation Model */ |
78
|
10 |
|
$classModelAnnotation = $this->annoReader->getClassAnnotation($classRef, 'Tpg\ExtjsBundle\Annotation\Model'); |
79
|
|
|
/** @var $classModelProxyAnnotation ModelProxy */ |
80
|
10 |
|
$classModelProxyAnnotation = $this->annoReader->getClassAnnotation($classRef, 'Tpg\ExtjsBundle\Annotation\ModelProxy'); |
81
|
10 |
|
if ($classModelAnnotation !== null) { |
82
|
10 |
|
$modelName = $classModelAnnotation->name; |
83
|
10 |
|
if (empty($modelName)) { |
84
|
|
|
$modelName = str_replace("\\", ".", $classRef->getName()); |
85
|
|
|
} |
86
|
10 |
|
if ($classModelAnnotation->generateAsBase === true) { |
87
|
1 |
|
$modelName = substr($modelName, 0, strrpos($modelName, '.')+1) . 'Base' . substr($modelName, strrpos($modelName, '.')+1); |
88
|
1 |
|
} |
89
|
|
|
$structure = array( |
90
|
10 |
|
'name' => $modelName, |
91
|
10 |
|
'extend' => $classModelAnnotation->extend, |
92
|
10 |
|
'fields' => array(), |
93
|
10 |
|
'associations' => array(), |
94
|
10 |
|
'validators' => array(), |
95
|
|
|
'idProperty' => 'id' |
96
|
10 |
|
); |
97
|
10 |
|
if ($classModelProxyAnnotation !== null) { |
98
|
6 |
|
$structure['proxy'] = array( |
99
|
6 |
|
'type'=>$classModelProxyAnnotation->name, |
100
|
6 |
|
) + $classModelProxyAnnotation->option; |
101
|
6 |
|
if ($classModelProxyAnnotation->reader != array()) $structure['proxy']['reader'] = $classModelProxyAnnotation->reader; |
102
|
6 |
|
if ($classModelProxyAnnotation->writer != array()) $structure['proxy']['writer'] = $classModelProxyAnnotation->writer; |
103
|
6 |
|
} |
104
|
|
|
/** @var $classExclusionPolicy ExclusionPolicy */ |
105
|
10 |
|
$classExclusionPolicy = $this->annoReader->getClassAnnotation($classRef, 'JMS\Serializer\Annotation\ExclusionPolicy'); |
106
|
10 |
|
foreach ($classRef->getProperties() as $property) { |
107
|
|
|
/** @var $propertyExclude Exclude */ |
108
|
10 |
|
$propertyExclude = $this->annoReader->getPropertyAnnotation($property, 'JMS\Serializer\Annotation\Exclude'); |
109
|
|
|
/** @var $propertyExpose Expose */ |
110
|
10 |
|
$propertyExpose = $this->annoReader->getPropertyAnnotation($property, 'JMS\Serializer\Annotation\Expose'); |
111
|
10 |
|
if ($classExclusionPolicy === null || strtolower($classExclusionPolicy->policy) == "none") { |
112
|
10 |
|
if ($propertyExclude !== null) { |
113
|
6 |
|
continue; |
114
|
|
|
} |
115
|
10 |
|
} else if (strtolower($classExclusionPolicy->policy) == "all") { |
116
|
|
|
if ($propertyExpose === null) { |
117
|
|
|
continue; |
118
|
|
|
} |
119
|
|
|
} |
120
|
10 |
|
$this->buildPropertyAnnotation($property, $structure); |
121
|
10 |
|
} |
122
|
10 |
|
$this->removeDuplicate($structure['validators']); |
123
|
10 |
|
return $this->twig->render('TpgExtjsBundle:ExtjsMarkup:model.js.twig', $structure); |
124
|
|
|
} else { |
125
|
|
|
return ""; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
10 |
|
protected function convertNaming($name) { |
130
|
10 |
|
return $name; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param \ReflectionProperty $property |
135
|
|
|
* @param $structure |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
10 |
|
protected function buildPropertyAnnotation($property, &$structure) { |
140
|
|
|
$field = array( |
141
|
10 |
|
'name' => $this->convertNaming($property->getName()), |
142
|
10 |
|
'type' => 'string', |
143
|
10 |
|
); |
144
|
10 |
|
$association = array(); |
145
|
10 |
|
$validators = array(); |
146
|
10 |
|
$skipValidator = false; |
147
|
10 |
|
$saveField = false; |
148
|
10 |
|
$fieldIsId = false; |
149
|
10 |
|
$annotations = $this->annoReader->getPropertyAnnotations($property); |
150
|
10 |
|
foreach ($annotations as $annotation) { |
151
|
10 |
|
$className = get_class($annotation); |
152
|
|
|
/** Get Constraints from Symfony Validator */ |
153
|
10 |
|
if (strpos(get_class($annotation), 'Symfony\Component\Validator\Constraints') === 0) { |
154
|
10 |
|
$validators[] = array_merge( |
155
|
10 |
|
array('field'=>$this->convertNaming($property->getName())), |
156
|
10 |
|
$this->getValidator(substr($className, 40),$annotation) |
157
|
10 |
|
); |
158
|
10 |
|
} |
159
|
10 |
|
switch(get_class($annotation)) { |
160
|
10 |
|
case 'Tpg\ExtjsBundle\Annotation\Model\Field': |
161
|
|
|
$field['type'] = $annotation->type; |
162
|
|
|
break; |
163
|
10 |
|
case 'Doctrine\ORM\Mapping\Id': |
164
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Id': |
165
|
10 |
|
$field['useNull'] = true; |
166
|
10 |
|
$field['persist'] = false; |
167
|
10 |
|
$skipValidator = true; |
168
|
10 |
|
$fieldIsId = true; |
169
|
10 |
|
break; |
170
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Timestamp': |
171
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Date': |
172
|
|
|
$field['type'] = "date"; |
173
|
|
|
break; |
174
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Float': |
175
|
4 |
|
$field['type'] = "float"; |
176
|
4 |
|
break; |
177
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Boolean': |
178
|
|
|
$field['type'] = "boolean"; |
179
|
|
|
break; |
180
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Hash': |
181
|
|
|
$field['type'] = "auto"; |
182
|
|
|
break; |
183
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Int': |
184
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Increment': |
185
|
|
|
$field['type'] = "int"; |
186
|
|
|
break; |
187
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\String': |
188
|
1 |
|
$field['type'] = "string"; |
189
|
1 |
|
break; |
190
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\Field': |
191
|
4 |
|
$field['type'] = $this->getColumnType($annotation->type); |
192
|
4 |
|
break; |
193
|
10 |
|
case 'Doctrine\ORM\Mapping\Column': |
194
|
6 |
|
$field['type'] = $this->getColumnType($annotation->type); |
195
|
6 |
|
if ($field['type'] === "date") { |
196
|
6 |
|
$field['dateFormat'] = \DateTime::ISO8601; |
197
|
6 |
|
} |
198
|
6 |
|
$validators[] = array('type'=>'presence', 'field'=>$this->convertNaming($property->getName())); |
199
|
6 |
|
break; |
200
|
10 |
|
case 'JMS\Serializer\Annotation\SerializedName': |
201
|
|
|
$field['name'] = $annotation->name; |
202
|
|
|
break; |
203
|
10 |
|
case 'JMS\Serializer\Annotation\Type': |
204
|
4 |
|
if (stripos($annotation->name, "DateTime") === 0) { |
205
|
|
|
$type = $annotation->name; |
206
|
|
|
if ($type === "DateTime") { |
207
|
|
|
$field['dateFormat'] = \DateTime::ISO8601; |
208
|
|
|
} else { |
209
|
|
|
$format = explode(',', |
210
|
|
|
substr( |
211
|
|
|
$type, |
212
|
|
|
stripos($type, '<')+2, |
213
|
|
|
-2 |
214
|
|
|
) |
215
|
|
|
); |
216
|
|
|
$field['dateFormat'] = $format[0]; |
217
|
|
|
} |
218
|
|
|
} |
219
|
4 |
|
break; |
220
|
10 |
|
case 'Doctrine\ORM\Mapping\OneToOne': |
221
|
|
|
$association['type'] = substr(get_class($annotation), 21); |
222
|
|
|
$association['name'] = $property->getName(); |
223
|
|
|
$association['model'] = $this->getModelName($annotation->targetEntity); |
224
|
|
|
$association['entity'] = $annotation->targetEntity; |
225
|
|
|
if (!isset($association['key']) || empty($association['key'])) |
226
|
|
|
$association['key'] = $this->tryToGetJoinColumnNameOfMappedBy($annotation); |
|
|
|
|
227
|
|
|
break; |
228
|
10 |
View Code Duplication |
case 'Doctrine\ORM\Mapping\OneToMany': |
|
|
|
|
229
|
6 |
|
$association['type'] = substr(get_class($annotation), 21); |
230
|
6 |
|
$association['name'] = $property->getName(); |
231
|
6 |
|
$association['model'] = $this->getModelName($annotation->targetEntity); |
232
|
6 |
|
$association['entity'] = $annotation->targetEntity; |
233
|
6 |
|
$association['key'] = $this->tryToGetJoinColumnNameOfMappedBy($annotation); |
234
|
6 |
|
break; |
235
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\EmbedMany': |
236
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\EmbedOne': |
237
|
10 |
|
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceMany': |
238
|
10 |
View Code Duplication |
case 'Doctrine\ODM\MongoDB\Mapping\Annotations\ReferenceOne': |
|
|
|
|
239
|
4 |
|
if ($annotation->targetDocument) { |
240
|
4 |
|
$association['type'] = substr(get_class($annotation), 41); |
241
|
4 |
|
$association['name'] = $property->getName(); |
242
|
4 |
|
$association['model'] = $this->getModelName($annotation->targetDocument); |
243
|
4 |
|
$association['entity'] = $annotation->targetDocument; |
244
|
4 |
|
} else { |
245
|
|
|
$field['type'] = "auto"; |
246
|
|
|
} |
247
|
4 |
|
break; |
248
|
10 |
View Code Duplication |
case 'Doctrine\ORM\Mapping\ManyToOne': |
|
|
|
|
249
|
1 |
|
$association['type'] = substr(get_class($annotation), 21); |
250
|
1 |
|
$association['name'] = $property->getName(); |
251
|
1 |
|
$association['model'] = $this->getModelName($annotation->targetEntity); |
252
|
1 |
|
$association['entity'] = $annotation->targetEntity; |
253
|
1 |
|
break; |
254
|
10 |
|
case 'Doctrine\ORM\Mapping\ManyToMany': |
255
|
|
|
$association['type'] = 'ManyToMany'; |
256
|
|
|
$association['name'] = $property->getName(); |
257
|
|
|
$association['model'] = $this->getModelName($annotation->targetEntity); |
258
|
|
|
$association['entity'] = $annotation->targetEntity; |
259
|
|
|
break; |
260
|
10 |
|
case 'Doctrine\ORM\Mapping\JoinColumns': |
261
|
|
|
if (count($annotation->value) > 1) { |
262
|
|
|
throw new \Exception('Multiple foreign key is not supported'); |
263
|
|
|
} |
264
|
|
|
$saveField = true; |
265
|
|
|
$field['name'] = $this->convertNaming($annotation->value[0]->name); |
266
|
|
|
$field['type'] = $this->getEntityColumnType($association['entity'], $annotation->value[0]->referencedColumnName); |
267
|
|
|
$field['useNull'] = true; |
268
|
|
|
$association['key'] = $this->convertNaming($annotation->value[0]->name); |
269
|
|
|
break; |
270
|
10 |
|
case 'Doctrine\ORM\Mapping\JoinColumn': |
271
|
1 |
|
$saveField = true; |
272
|
1 |
|
$field['name'] = $this->convertNaming($annotation->name); |
273
|
1 |
|
$field['type'] = $this->getEntityColumnType($association['entity'], $annotation->referencedColumnName); |
274
|
1 |
|
$field['useNull'] = true; |
275
|
1 |
|
$association['key'] = $this->convertNaming($annotation->name); |
276
|
1 |
|
$field['mapping'] = $property->getName() . '.' . $annotation->referencedColumnName; |
277
|
1 |
|
break; |
278
|
10 |
|
} |
279
|
10 |
|
} |
280
|
10 |
|
if($fieldIsId){ |
281
|
10 |
|
$structure['idProperty'] = $field['name']; |
282
|
10 |
|
} |
283
|
|
|
|
284
|
|
|
/** Add the ability to override field parameter */ |
285
|
10 |
|
if (isset($this->fieldsParams[$field['type']])) { |
286
|
6 |
|
$field = array_merge($field, $this->fieldsParams[$field['type']]); |
287
|
6 |
|
} |
288
|
|
|
|
289
|
10 |
|
if (!empty($association)) { |
290
|
10 |
|
if ($association['model'] === null) { |
291
|
|
|
/** Related model is not available, skip this field. */ |
292
|
|
|
return array(); |
293
|
|
|
} |
294
|
10 |
|
$structure['associations'][] = $association; |
295
|
10 |
|
} |
296
|
10 |
|
if ($saveField || empty($association)) { |
297
|
10 |
|
$structure['fields'][$field['name']] = $field; |
298
|
10 |
|
} |
299
|
10 |
|
if (!empty($validators) && !$skipValidator) { |
300
|
10 |
|
$structure['validators'] = array_merge($structure['validators'], $validators); |
301
|
10 |
|
} |
302
|
10 |
|
return $field; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Get the join column name. |
307
|
|
|
* |
308
|
|
|
* @param OneToMany $annotation |
309
|
|
|
* @throws \Exception |
310
|
|
|
* @return string |
311
|
|
|
*/ |
312
|
6 |
|
protected function tryToGetJoinColumnNameOfMappedBy($annotation){ |
313
|
6 |
|
$annotation = $this->tryToGetJoinColumnAnnotationOfMappedBy($annotation); |
314
|
6 |
|
if ($annotation !== null) { |
315
|
6 |
|
if ($annotation instanceof JoinColumn) { |
316
|
6 |
|
return $annotation->name; |
317
|
|
|
} else if ($annotation instanceof JoinColumns) { |
318
|
|
|
if (count($annotation->value) > 1) { |
319
|
|
|
throw new \Exception('Multiple foreign key is not supported'); |
320
|
|
|
} |
321
|
|
|
return $annotation->value[0]->name; |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
return ''; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Get the join column annotation |
329
|
|
|
* @param OneToMany $annotation |
330
|
|
|
* @return null|Annotation |
331
|
|
|
*/ |
332
|
6 |
|
protected function tryToGetJoinColumnAnnotationOfMappedBy($annotation){ |
333
|
6 |
|
$result = null; |
334
|
6 |
|
if($annotation->targetEntity && $annotation->mappedBy) { |
335
|
6 |
|
$result = $this->getAnnotation( |
336
|
6 |
|
$annotation->targetEntity, |
337
|
6 |
|
$annotation->mappedBy, |
338
|
|
|
'Doctrine\ORM\Mapping\JoinColumn' |
339
|
6 |
|
); |
340
|
6 |
|
if ($result === null) { |
341
|
|
|
$result = $this->getAnnotation( |
342
|
|
|
$annotation->targetEntity, |
343
|
|
|
$annotation->mappedBy, |
344
|
|
|
'Doctrine\ORM\Mapping\JoinColumns' |
345
|
|
|
); |
346
|
|
|
} |
347
|
6 |
|
} |
348
|
6 |
|
return $result; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Get Annotation |
353
|
|
|
* @param $entity |
354
|
|
|
* @param $property |
355
|
|
|
* @param $annotation |
356
|
|
|
* @return Annotation|null |
357
|
|
|
*/ |
358
|
6 |
|
protected function getAnnotation($entity, $property, $annotation) { |
359
|
6 |
|
$classRef = new \ReflectionClass($entity); |
360
|
6 |
|
$propertyRef = $classRef->getProperty($property); |
361
|
6 |
|
if ($propertyRef) return $this->annoReader->getPropertyAnnotation($propertyRef, $annotation); |
362
|
|
|
else return null; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Get Column Type of a model. |
367
|
|
|
* |
368
|
|
|
* @param $entity string Class name of the entity |
369
|
|
|
* @param $property string |
370
|
|
|
* @return string |
371
|
|
|
*/ |
372
|
1 |
|
public function getEntityColumnType($entity, $property) { |
373
|
1 |
|
$classRef = new \ReflectionClass($entity); |
374
|
1 |
|
if ($classRef->hasProperty($property)) { |
375
|
1 |
|
$propertyRef = $classRef->getProperty($property); |
376
|
1 |
|
$columnRef = $this->annoReader->getPropertyAnnotation($propertyRef, 'Doctrine\ORM\Mapping\Column'); |
377
|
1 |
|
} else { |
378
|
|
|
/** Can not find the property on the class. Check the all the Column annotation */ |
379
|
|
|
foreach ($classRef->getProperties() as $propertyRef) { |
380
|
|
|
$columnRef = $this->annoReader->getPropertyAnnotation($propertyRef, 'Doctrine\ORM\Mapping\Column'); |
381
|
|
|
if ($columnRef->name == $property) { |
382
|
|
|
break; |
383
|
|
|
} else { |
384
|
|
|
$columnRef = null; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
} |
388
|
1 |
|
if ($columnRef === null) { |
389
|
|
|
$idRef = $this->annoReader->getPropertyAnnotation($propertyRef, 'Doctrine\ORM\Mapping\Id'); |
|
|
|
|
390
|
|
|
if ($idRef !== null) { |
391
|
|
|
return "int"; |
392
|
|
|
} else { |
393
|
|
|
return "string"; |
394
|
|
|
} |
395
|
|
|
} else { |
396
|
1 |
|
return $this->getColumnType($columnRef->type); |
|
|
|
|
397
|
|
|
} |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Translate Mongo Field Type to ExtJs |
402
|
|
|
* |
403
|
|
|
* @param $type |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
|
|
protected function getFieldType($type) { |
407
|
|
|
switch ($type) { |
408
|
|
|
case 'hash': |
409
|
|
|
return "auto"; |
410
|
|
|
case 'timestamp': |
411
|
|
|
case 'date': |
412
|
|
|
return "date"; |
413
|
|
|
default: |
414
|
|
|
return $type; |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Translate Column Type from PHP to ExtJS |
420
|
|
|
* |
421
|
|
|
* @param $type |
422
|
|
|
* @return string |
423
|
|
|
*/ |
424
|
10 |
|
protected function getColumnType($type) { |
425
|
|
|
switch ($type) { |
426
|
10 |
|
case 'decimal': |
427
|
|
|
return 'float'; |
428
|
10 |
|
case 'integer': |
429
|
10 |
|
case 'smallint': |
430
|
10 |
|
case 'bigint': |
431
|
6 |
|
return 'int'; |
432
|
10 |
|
case 'datetime': |
433
|
10 |
|
case 'time': |
434
|
10 |
|
case 'date': |
435
|
10 |
|
case 'datetimetz': |
436
|
6 |
|
return 'date'; |
437
|
10 |
|
case 'text': |
438
|
10 |
|
case 'guid': |
439
|
|
|
return 'string'; |
440
|
10 |
|
case 'object': |
441
|
10 |
|
case 'array': |
442
|
10 |
|
case 'simple_array': |
443
|
10 |
|
case 'json_array': |
444
|
10 |
|
case 'blob': |
445
|
|
|
return 'auto'; |
446
|
10 |
|
default: |
447
|
10 |
|
return $type; |
448
|
10 |
|
} |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Get the Ext JS Validator |
453
|
|
|
* |
454
|
|
|
* @param string $name |
455
|
|
|
* @param array $annotation |
456
|
|
|
* @return array |
457
|
|
|
*/ |
458
|
10 |
|
protected function getValidator($name, $annotation) { |
459
|
10 |
|
$validate = array(); |
460
|
|
|
switch($name) { |
461
|
10 |
|
case 'NotBlank': |
462
|
10 |
|
case 'NotNull': |
463
|
10 |
|
$validate['type'] = "presence"; |
464
|
10 |
|
break; |
465
|
6 |
|
case 'Email': |
466
|
6 |
|
$validate['type'] = "email"; |
467
|
6 |
|
break; |
468
|
6 |
View Code Duplication |
case 'Length': |
|
|
|
|
469
|
6 |
|
$validate['type'] = "length"; |
470
|
6 |
|
$validate['max'] = (int)$annotation->max; |
471
|
6 |
|
$validate['min'] = (int)$annotation->min; |
472
|
6 |
|
break; |
473
|
6 |
|
case 'Regex': |
474
|
6 |
|
if ($annotation->match) { |
475
|
6 |
|
$validate['type'] = "format"; |
476
|
6 |
|
$validate['matcher']['skipEncode'] = true; |
477
|
6 |
|
$validate['matcher']['value'] = $annotation->pattern; |
478
|
6 |
|
} |
479
|
6 |
|
break; |
480
|
6 |
|
case 'MaxLength': |
481
|
6 |
View Code Duplication |
case 'MinLength': |
|
|
|
|
482
|
|
|
$validate['type'] = "length"; |
483
|
|
|
if ($name == "MaxLength") { |
484
|
|
|
$validate['max'] = (int)$annotation->limit; |
485
|
|
|
} else { |
486
|
|
|
$validate['min'] = (int)$annotation->limit; |
487
|
|
|
} |
488
|
|
|
break; |
489
|
6 |
|
case 'Choice': |
490
|
6 |
|
$validate['type'] = "inclusion"; |
491
|
6 |
|
$validate['list'] = $annotation->choices; |
492
|
6 |
|
break; |
493
|
|
|
default: |
494
|
|
|
$validate['type'] = strtolower($name); |
495
|
|
|
$validate += get_object_vars($annotation); |
496
|
|
|
break; |
497
|
|
|
} |
498
|
10 |
|
return $validate; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Get model name of an entity |
503
|
|
|
* @param $entity string Class name of the entity |
504
|
|
|
*/ |
505
|
10 |
|
public function getModelName($entity) { |
506
|
10 |
|
$classRef = new \ReflectionClass($entity); |
507
|
10 |
|
$classModelAnnotation = $this->annoReader->getClassAnnotation($classRef, 'Tpg\ExtjsBundle\Annotation\Model'); |
508
|
10 |
|
if ($classModelAnnotation !== null) { |
509
|
10 |
|
if ($classModelAnnotation->name) { |
510
|
10 |
|
return $classModelAnnotation->name; |
511
|
|
|
} else { |
512
|
|
|
return str_replace("\\", ".", $classRef->getName()); |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
return null; |
516
|
|
|
} |
517
|
|
|
|
518
|
10 |
|
protected function removeDuplicate(&$list) |
519
|
|
|
{ |
520
|
10 |
|
$secondList = $list; |
521
|
10 |
|
$duplicateList = array(); |
522
|
10 |
|
foreach ($list as $index => $row) { |
523
|
10 |
|
if (in_array($index, $duplicateList)) { |
524
|
6 |
|
continue; |
525
|
|
|
} |
526
|
10 |
|
foreach ($secondList as $index2 => $row2) { |
527
|
10 |
|
if ($index === $index2) { |
528
|
10 |
|
continue; |
529
|
|
|
} |
530
|
10 |
|
if ($row == $row2) { |
531
|
6 |
|
$duplicateList[] = $index2; |
532
|
6 |
|
} |
533
|
10 |
|
} |
534
|
10 |
|
} |
535
|
10 |
|
foreach (array_reverse($duplicateList) as $index) { |
536
|
6 |
|
unset($list[$index]); |
537
|
10 |
|
} |
538
|
10 |
|
} |
539
|
|
|
} |
540
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: