2727use Phalcon \Validation ;
2828use Phalcon \Validation \Validator \Email as EmailValidator ;
2929use ReflectionClass ;
30+ use ReflectionClassConstant ;
31+ use ReflectionProperty ;
3032
3133/**
3234 * Builder to generate models
@@ -111,23 +113,19 @@ public function build(): void
111113 require_once $ config ->devtools ->loader ;
112114 }
113115
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 ))) {
117120 $ namespace = 'namespace ' . $ this ->modelOptions ->getOption ('namespace ' ) . '; ' . PHP_EOL . PHP_EOL ;
118121 }
119122
120123 $ genDocMethods = $ this ->modelOptions ->getValidOptionOrDefault ('genDocMethods ' , false );
121124 $ useSettersGetters = $ this ->modelOptions ->getValidOptionOrDefault ('genSettersGetters ' , false );
122125
123- $ adapter = $ config ->database ->adapter ;
126+ $ adapter = $ config ->database ->adapter ?? ' Mysql ' ;
124127 $ this ->isSupportedAdapter ($ adapter );
125128
126- $ adapter = 'Mysql ' ;
127- if (isset ($ config ->database ->adapter )) {
128- $ adapter = $ config ->database ->adapter ;
129- }
130-
131129 if (is_object ($ config ->database )) {
132130 $ configArray = $ config ->database ->toArray ();
133131 } else {
@@ -167,40 +165,36 @@ public function build(): void
167165
168166 foreach ($ referenceList as $ tableName => $ references ) {
169167 foreach ($ references as $ reference ) {
170- if ($ reference ->getReferencedTable () != $ this ->modelOptions ->getOption ('name ' )) {
168+ if ($ reference ->getReferencedTable () !== $ this ->modelOptions ->getOption ('name ' )) {
171169 continue ;
172170 }
173171
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 ' )."\\" : '' ;
178174
179175 $ refColumns = $ reference ->getReferencedColumns ();
180176 $ columns = $ reference ->getColumns ();
181177 $ initialize [] = $ snippet ->getRelation (
182178 'hasMany ' ,
183- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ refColumns [0 ]) : $ refColumns [ 0 ] ,
179+ $ this ->getFieldName ( $ refColumns [0 ]),
184180 $ entityNamespace . Text::camelize ($ tableName , '_- ' ),
185- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ columns [0 ]) : $ columns [ 0 ] ,
181+ $ this ->getFieldName ( $ columns [0 ]),
186182 "['alias' => ' " . Text::camelize ($ tableName , '_- ' ) . "'] "
187183 );
188184 }
189185 }
190186
191187 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 ' ) : '' ;
196190
197191 $ refColumns = $ reference ->getReferencedColumns ();
198192 $ columns = $ reference ->getColumns ();
199193 $ initialize [] = $ snippet ->getRelation (
200194 'belongsTo ' ,
201- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ columns [0 ]) : $ columns [ 0 ] ,
195+ $ this ->getFieldName ( $ columns [0 ]),
202196 $ this ->getEntityClassName ($ reference , $ entityNamespace ),
203- $ this ->modelOptions -> getOption ( ' camelize ' ) ? Utils:: lowerCamelize ( $ refColumns [0 ]) : $ refColumns [ 0 ] ,
197+ $ this ->getFieldName ( $ refColumns [0 ]),
204198 "['alias' => ' " . Text::camelize ($ reference ->getReferencedTable (), '_- ' ) . "'] "
205199 );
206200 }
@@ -225,8 +219,6 @@ public function build(): void
225219 }
226220 }
227221
228- $ possibleMethods ['getSource ' ] = true ;
229-
230222 /** @noinspection PhpIncludeInspection */
231223 require_once $ modelPath ;
232224
@@ -237,7 +229,7 @@ public function build(): void
237229 }
238230 $ reflection = new ReflectionClass ($ fullClassName );
239231 foreach ($ reflection ->getMethods () as $ method ) {
240- if ($ method ->getDeclaringClass ()->getName () != $ fullClassName ) {
232+ if ($ method ->getDeclaringClass ()->getName () !== $ fullClassName ) {
241233 continue ;
242234 }
243235
@@ -285,117 +277,60 @@ public function build(): void
285277 }
286278 }
287279
288- $ possibleFields = $ possibleFieldsTransformed = [];
280+ $ possibleFieldsTransformed = [];
289281 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 ());
296283 $ possibleFieldsTransformed [$ fieldName ] = true ;
297284 }
298285
299286 if (method_exists ($ reflection , 'getReflectionConstants ' )) {
300287 foreach ($ reflection ->getReflectionConstants () as $ constant ) {
301- if ($ constant ->getDeclaringClass ()->getName () != $ fullClassName ) {
288+ if ($ constant ->getDeclaringClass ()->getName () !== $ fullClassName ) {
302289 continue ;
303290 }
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- }
321291
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 ;
333296 }
334297 }
335298 }
336299
337300 foreach ($ reflection ->getProperties () as $ property ) {
338301 $ 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+ ) {
344305 continue ;
345306 }
346307
347308 $ modifiersPreg = '' ;
348309 switch ($ property ->getModifiers ()) {
349- case \ ReflectionProperty::IS_PUBLIC :
310+ case ReflectionProperty::IS_PUBLIC :
350311 $ modifiersPreg = '^(\s*)public(\s+) ' ;
351312 break ;
352- case \ ReflectionProperty::IS_PRIVATE :
313+ case ReflectionProperty::IS_PRIVATE :
353314 $ modifiersPreg = '^(\s*)private(\s+) ' ;
354315 break ;
355- case \ ReflectionProperty::IS_PROTECTED :
316+ case ReflectionProperty::IS_PROTECTED :
356317 $ modifiersPreg = '^(\s*)protected(\s+) ' ;
357318 break ;
358- case \ ReflectionProperty::IS_STATIC + \ ReflectionProperty::IS_PUBLIC :
319+ case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PUBLIC :
359320 $ modifiersPreg = '^(\s*)(public?)(\s+)static(\s+) ' ;
360321 break ;
361- case \ ReflectionProperty::IS_STATIC + \ ReflectionProperty::IS_PROTECTED :
322+ case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PROTECTED :
362323 $ modifiersPreg = '^(\s*)protected(\s+)static(\s+) ' ;
363324 break ;
364- case \ ReflectionProperty::IS_STATIC + \ ReflectionProperty::IS_PRIVATE :
325+ case ReflectionProperty::IS_STATIC + ReflectionProperty::IS_PRIVATE :
365326 $ modifiersPreg = '^(\s*)private(\s+)static(\s+) ' ;
366327 break ;
367328 }
368329
369330 $ 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 ;
399334 }
400335 }
401336 } catch (\Exception $ e ) {
@@ -411,12 +346,9 @@ public function build(): void
411346
412347 $ validations = [];
413348 foreach ($ fields as $ field ) {
349+ $ fieldName = $ this ->getFieldName ($ field ->getName ());
350+
414351 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- }
420352 $ domain = [];
421353 if (preg_match ('/\((.*)\)/ ' , (string )$ field ->getType (), $ matches )) {
422354 foreach (explode (', ' , $ matches [1 ]) as $ item ) {
@@ -429,12 +361,7 @@ public function build(): void
429361 }
430362 }
431363
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 ' ) {
438365 $ validations [] = $ snippet ->getValidateEmail ($ fieldName );
439366 $ uses [] = $ snippet ->getUseAs (EmailValidator::class, 'EmailValidator ' );
440367 }
@@ -466,8 +393,7 @@ public function build(): void
466393 }
467394
468395 $ 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 ());
471397 $ attributes [] = $ snippet ->getAttributes (
472398 $ type ,
473399 $ useSettersGetters ? 'protected ' : 'public ' ,
@@ -572,6 +498,65 @@ public function build(): void
572498 }
573499 }
574500
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+
575560 /**
576561 * Set path to model
577562 *
0 commit comments