27
27
use Phalcon \Validation ;
28
28
use Phalcon \Validation \Validator \Email as EmailValidator ;
29
29
use ReflectionClass ;
30
+ use ReflectionClassConstant ;
31
+ use ReflectionProperty ;
30
32
31
33
/**
32
34
* Builder to generate models
@@ -111,23 +113,19 @@ public function build(): void
111
113
require_once $ config ->devtools ->loader ;
112
114
}
113
115
114
- $ namespace = '' ;
115
- if ($ this ->modelOptions ->hasOption ('namespace ' ) &&
116
- $ this ->checkNamespace ((string )$ this ->modelOptions ->getOption ('namespace ' ))) {
116
+ $ namespace = $ this ->modelOptions ->hasOption ('namespace ' )
117
+ ? (string ) $ this ->modelOptions ->getOption ('namespace ' ) : '' ;
118
+
119
+ if ($ this ->checkNamespace ($ namespace ) && !empty (trim ($ namespace ))) {
117
120
$ namespace = 'namespace ' . $ this ->modelOptions ->getOption ('namespace ' ) . '; ' . PHP_EOL . PHP_EOL ;
118
121
}
119
122
120
123
$ genDocMethods = $ this ->modelOptions ->getValidOptionOrDefault ('genDocMethods ' , false );
121
124
$ useSettersGetters = $ this ->modelOptions ->getValidOptionOrDefault ('genSettersGetters ' , false );
122
125
123
- $ adapter = $ config ->database ->adapter ;
126
+ $ adapter = $ config ->database ->adapter ?? ' Mysql ' ;
124
127
$ this ->isSupportedAdapter ($ adapter );
125
128
126
- $ adapter = 'Mysql ' ;
127
- if (isset ($ config ->database ->adapter )) {
128
- $ adapter = $ config ->database ->adapter ;
129
- }
130
-
131
129
if (is_object ($ config ->database )) {
132
130
$ configArray = $ config ->database ->toArray ();
133
131
} else {
@@ -167,40 +165,36 @@ public function build(): void
167
165
168
166
foreach ($ referenceList as $ tableName => $ references ) {
169
167
foreach ($ references as $ reference ) {
170
- if ($ reference ->getReferencedTable () != $ this ->modelOptions ->getOption ('name ' )) {
168
+ if ($ reference ->getReferencedTable () !== $ this ->modelOptions ->getOption ('name ' )) {
171
169
continue ;
172
170
}
173
171
174
- $ entityNamespace = '' ;
175
- if ($ this ->modelOptions ->getOption ('namespace ' )) {
176
- $ entityNamespace = $ this ->modelOptions ->getOption ('namespace ' )."\\" ;
177
- }
172
+ $ entityNamespace = $ this ->modelOptions ->hasOption ('namespace ' )
173
+ ? $ this ->modelOptions ->getOption ('namespace ' )."\\" : '' ;
178
174
179
175
$ refColumns = $ reference ->getReferencedColumns ();
180
176
$ columns = $ reference ->getColumns ();
181
177
$ initialize [] = $ snippet ->getRelation (
182
178
'hasMany ' ,
183
- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ refColumns [0 ]) : $ refColumns [ 0 ] ,
179
+ $ this ->getFieldName ( $ refColumns [0 ]),
184
180
$ entityNamespace . Text::camelize ($ tableName , '_- ' ),
185
- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ columns [0 ]) : $ columns [ 0 ] ,
181
+ $ this ->getFieldName ( $ columns [0 ]),
186
182
"['alias' => ' " . Text::camelize ($ tableName , '_- ' ) . "'] "
187
183
);
188
184
}
189
185
}
190
186
191
187
foreach ($ db ->describeReferences ($ this ->modelOptions ->getOption ('name ' ), $ schema ) as $ reference ) {
192
- $ entityNamespace = '' ;
193
- if ($ this ->modelOptions ->getOption ('namespace ' )) {
194
- $ entityNamespace = $ this ->modelOptions ->getOption ('namespace ' );
195
- }
188
+ $ entityNamespace = $ this ->modelOptions ->hasOption ('namespace ' )
189
+ ? $ this ->modelOptions ->getOption ('namespace ' ) : '' ;
196
190
197
191
$ refColumns = $ reference ->getReferencedColumns ();
198
192
$ columns = $ reference ->getColumns ();
199
193
$ initialize [] = $ snippet ->getRelation (
200
194
'belongsTo ' ,
201
- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ columns [0 ]) : $ columns [ 0 ] ,
195
+ $ this ->getFieldName ( $ columns [0 ]),
202
196
$ this ->getEntityClassName ($ reference , $ entityNamespace ),
203
- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ refColumns [0 ]) : $ refColumns [ 0 ] ,
197
+ $ this ->getFieldName ( $ refColumns [0 ]),
204
198
"['alias' => ' " . Text::camelize ($ reference ->getReferencedTable (), '_- ' ) . "'] "
205
199
);
206
200
}
@@ -225,8 +219,6 @@ public function build(): void
225
219
}
226
220
}
227
221
228
- $ possibleMethods ['getSource ' ] = true ;
229
-
230
222
/** @noinspection PhpIncludeInspection */
231
223
require_once $ modelPath ;
232
224
@@ -237,7 +229,7 @@ public function build(): void
237
229
}
238
230
$ reflection = new ReflectionClass ($ fullClassName );
239
231
foreach ($ reflection ->getMethods () as $ method ) {
240
- if ($ method ->getDeclaringClass ()->getName () != $ fullClassName ) {
232
+ if ($ method ->getDeclaringClass ()->getName () !== $ fullClassName ) {
241
233
continue ;
242
234
}
243
235
@@ -285,117 +277,60 @@ public function build(): void
285
277
}
286
278
}
287
279
288
- $ possibleFields = $ possibleFieldsTransformed = [];
280
+ $ possibleFieldsTransformed = [];
289
281
foreach ($ fields as $ field ) {
290
- $ possibleFields [$ field ->getName ()] = true ;
291
- if ($ this ->modelOptions ->getOption ('camelize ' )) {
292
- $ fieldName = Utils::lowerCamelize (Utils::camelize ($ field ->getName (), '_- ' ));
293
- } else {
294
- $ fieldName = Utils::lowerCamelize (Utils::camelize ($ field ->getName (), '- ' ));
295
- }
282
+ $ fieldName = $ this ->getFieldName ($ field ->getName ());
296
283
$ possibleFieldsTransformed [$ fieldName ] = true ;
297
284
}
298
285
299
286
if (method_exists ($ reflection , 'getReflectionConstants ' )) {
300
287
foreach ($ reflection ->getReflectionConstants () as $ constant ) {
301
- if ($ constant ->getDeclaringClass ()->getName () != $ fullClassName ) {
288
+ if ($ constant ->getDeclaringClass ()->getName () !== $ fullClassName ) {
302
289
continue ;
303
290
}
304
- $ constantsPreg = '/^(\s*)const(\s+) ' .$ constant ->getName ().'([\s=;]+)/ ' ;
305
- $ endLine = $ startLine = 0 ;
306
- foreach ($ linesCode as $ line => $ code ) {
307
- if (preg_match ($ constantsPreg , $ code )) {
308
- $ startLine = $ line ;
309
- break ;
310
- }
311
- }
312
- if (!empty ($ startLine )) {
313
- $ countLines = count ($ linesCode );
314
- for ($ i = $ startLine ; $ i < $ countLines ; $ i ++) {
315
- if (preg_match ('/;(\s*)$/ ' , $ linesCode [$ i ])) {
316
- $ endLine = $ i ;
317
- break ;
318
- }
319
- }
320
- }
321
291
322
- if (!empty ($ startLine ) && !empty ($ endLine )) {
323
- $ constantDeclaration = join (
324
- '' ,
325
- array_slice (
326
- $ linesCode ,
327
- $ startLine ,
328
- $ endLine - $ startLine + 1
329
- )
330
- );
331
- $ attributes [] = PHP_EOL . " " . $ constant ->getDocComment () .
332
- PHP_EOL . $ constantDeclaration ;
292
+ $ constantsPreg = '/const(\s+) ' . $ constant ->getName () . '([\s=;]+)/ ' ;
293
+ $ attribute = $ this ->getAttribute ($ linesCode , $ constantsPreg , $ constant );
294
+ if (!empty ($ attribute )) {
295
+ $ attributes [] = $ attribute ;
333
296
}
334
297
}
335
298
}
336
299
337
300
foreach ($ reflection ->getProperties () as $ property ) {
338
301
$ propertyName = $ property ->getName ();
339
- /** @var null|string $possibleFieldsValue */
340
- $ possibleFieldsValue = $ possibleFieldsTransformed [$ propertyName ];
341
-
342
- if ($ property ->getDeclaringClass ()->getName () != $ fullClassName ||
343
- !empty ($ possibleFieldsValue )) {
302
+ if (!empty ($ possibleFieldsTransformed [$ propertyName ])
303
+ || $ property ->getDeclaringClass ()->getName () !== $ fullClassName
304
+ ) {
344
305
continue ;
345
306
}
346
307
347
308
$ modifiersPreg = '' ;
348
309
switch ($ property ->getModifiers ()) {
349
- case \ ReflectionProperty::IS_PUBLIC :
310
+ case ReflectionProperty::IS_PUBLIC :
350
311
$ modifiersPreg = '^(\s*)public(\s+) ' ;
351
312
break ;
352
- case \ ReflectionProperty::IS_PRIVATE :
313
+ case ReflectionProperty::IS_PRIVATE :
353
314
$ modifiersPreg = '^(\s*)private(\s+) ' ;
354
315
break ;
355
- case \ ReflectionProperty::IS_PROTECTED :
316
+ case ReflectionProperty::IS_PROTECTED :
356
317
$ modifiersPreg = '^(\s*)protected(\s+) ' ;
357
318
break ;
358
- case \ ReflectionProperty::IS_STATIC + \ ReflectionProperty::IS_PUBLIC :
319
+ case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PUBLIC :
359
320
$ modifiersPreg = '^(\s*)(public?)(\s+)static(\s+) ' ;
360
321
break ;
361
- case \ ReflectionProperty::IS_STATIC + \ ReflectionProperty::IS_PROTECTED :
322
+ case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PROTECTED :
362
323
$ modifiersPreg = '^(\s*)protected(\s+)static(\s+) ' ;
363
324
break ;
364
- case \ ReflectionProperty::IS_STATIC + \ ReflectionProperty::IS_PRIVATE :
325
+ case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PRIVATE :
365
326
$ modifiersPreg = '^(\s*)private(\s+)static(\s+) ' ;
366
327
break ;
367
328
}
368
329
369
330
$ modifiersPreg = '/ ' . $ modifiersPreg . '\$ ' . $ propertyName . '([\s=;]+)/ ' ;
370
- $ endLine = $ startLine = 0 ;
371
- foreach ($ linesCode as $ line => $ code ) {
372
- if (preg_match ($ modifiersPreg , $ code )) {
373
- $ startLine = $ line ;
374
- break ;
375
- }
376
- }
377
-
378
- if (!empty ($ startLine )) {
379
- $ countLines = count ($ linesCode );
380
- for ($ i = $ startLine ; $ i < $ countLines ; $ i ++) {
381
- if (preg_match ('/;(\s*)$/ ' , $ linesCode [$ i ])) {
382
- $ endLine = $ i ;
383
- break ;
384
- }
385
- }
386
- }
387
-
388
- if (!empty ($ startLine ) && !empty ($ endLine )) {
389
- $ propertyDeclaration = join (
390
- '' ,
391
- array_slice (
392
- $ linesCode ,
393
- $ startLine ,
394
- $ endLine - $ startLine + 1
395
- )
396
- );
397
- $ attributes [] = PHP_EOL . " " . $ property ->getDocComment () . PHP_EOL .
398
- $ propertyDeclaration ;
331
+ $ attribute = $ this ->getAttribute ($ linesCode , $ modifiersPreg , $ property );
332
+ if (!empty ($ attribute )) {
333
+ $ attributes [] = $ attribute ;
399
334
}
400
335
}
401
336
} catch (\Exception $ e ) {
@@ -411,12 +346,9 @@ public function build(): void
411
346
412
347
$ validations = [];
413
348
foreach ($ fields as $ field ) {
349
+ $ fieldName = $ this ->getFieldName ($ field ->getName ());
350
+
414
351
if ($ field ->getType () === Column::TYPE_CHAR ) {
415
- if ($ this ->modelOptions ->getOption ('camelize ' )) {
416
- $ fieldName = Utils::lowerCamelize (Utils::camelize ($ field ->getName (), '_- ' ));
417
- } else {
418
- $ fieldName = Utils::lowerCamelize (Utils::camelize ($ field ->getName (), '- ' ));
419
- }
420
352
$ domain = [];
421
353
if (preg_match ('/\((.*)\)/ ' , (string )$ field ->getType (), $ matches )) {
422
354
foreach (explode (', ' , $ matches [1 ]) as $ item ) {
@@ -429,12 +361,7 @@ public function build(): void
429
361
}
430
362
}
431
363
432
- if ($ field ->getName () == 'email ' ) {
433
- if ($ this ->modelOptions ->getOption ('camelize ' )) {
434
- $ fieldName = Utils::lowerCamelize (Utils::camelize ($ field ->getName (), '_- ' ));
435
- } else {
436
- $ fieldName = Utils::lowerCamelize (Utils::camelize ($ field ->getName (), '- ' ));
437
- }
364
+ if ($ field ->getName () === 'email ' ) {
438
365
$ validations [] = $ snippet ->getValidateEmail ($ fieldName );
439
366
$ uses [] = $ snippet ->getUseAs (EmailValidator::class, 'EmailValidator ' );
440
367
}
@@ -466,8 +393,7 @@ public function build(): void
466
393
}
467
394
468
395
$ type = $ this ->getPHPType ($ field ->getType ());
469
- $ fieldName = Utils::lowerCamelizeWithDelimiter ($ field ->getName (), '- ' , true );
470
- $ fieldName = $ this ->modelOptions ->getOption ('camelize ' ) ? Utils::lowerCamelize ($ fieldName ) : $ fieldName ;
396
+ $ fieldName = $ this ->getFieldName ($ field ->getName ());
471
397
$ attributes [] = $ snippet ->getAttributes (
472
398
$ type ,
473
399
$ useSettersGetters ? 'protected ' : 'public ' ,
@@ -572,6 +498,65 @@ public function build(): void
572
498
}
573
499
}
574
500
501
+ /**
502
+ * @param array $linesCode
503
+ * @param string $pattern
504
+ * @param ReflectionProperty|ReflectionClassConstant $attribute
505
+ *
506
+ * @return null|string
507
+ */
508
+ protected function getAttribute (array $ linesCode , string $ pattern , $ attribute ): ?string
509
+ {
510
+ $ endLine = $ startLine = 0 ;
511
+ foreach ($ linesCode as $ line => $ code ) {
512
+ if (preg_match ($ pattern , $ code )) {
513
+ $ startLine = $ line ;
514
+ break ;
515
+ }
516
+ }
517
+ if (!empty ($ startLine )) {
518
+ $ countLines = count ($ linesCode );
519
+ for ($ i = $ startLine ; $ i < $ countLines ; $ i ++) {
520
+ if (preg_match ('/;(\s*)$/ ' , $ linesCode [$ i ])) {
521
+ $ endLine = $ i ;
522
+ break ;
523
+ }
524
+ }
525
+ }
526
+
527
+ if (!empty ($ startLine ) && !empty ($ endLine )) {
528
+ $ attributeDeclaration = join (
529
+ '' ,
530
+ array_slice (
531
+ $ linesCode ,
532
+ $ startLine ,
533
+ $ endLine - $ startLine + 1
534
+ )
535
+ );
536
+ $ attributeFormatted = $ attributeDeclaration ;
537
+ if (!empty ($ attribute ->getDocComment ())) {
538
+ $ attributeFormatted = " " . $ attribute ->getDocComment () . PHP_EOL . $ attribute ;
539
+ }
540
+ return $ attributeFormatted ;
541
+ }
542
+
543
+ return null ;
544
+ }
545
+
546
+ /**
547
+ * @param string $fieldName
548
+ *
549
+ * @return string
550
+ */
551
+ protected function getFieldName (string $ fieldName ): string
552
+ {
553
+ if ($ this ->modelOptions ->getOption ('camelize ' )) {
554
+ return Utils::lowerCamelize (Utils::camelize ($ fieldName , '_- ' ));
555
+ }
556
+
557
+ return Utils::lowerCamelizeWithDelimiter ($ fieldName , '- ' , true );
558
+ }
559
+
575
560
/**
576
561
* Set path to model
577
562
*
0 commit comments