@@ -300,6 +300,19 @@ private static function getDataType(\ReflectionProperty $property) : string
300
300
$ enum = new \ReflectionEnum ($ type ->getName ());
301
301
302
302
if ((string ) $ enum ->getBackingType () === 'string ' ) {
303
+ $ cases = $ enum ->getCases ();
304
+
305
+ // save large enums as varchar
306
+ if (\count ($ cases ) > 10 ) {
307
+ $ longestOption = 0 ;
308
+
309
+ foreach ($ enum ->getCases () as $ case ) {
310
+ $ longestOption = \max ($ longestOption , \strlen ($ case ->getBackingValue ()));
311
+ }
312
+
313
+ return 'VARCHAR( ' . $ longestOption . ') ' ;
314
+ }
315
+
303
316
$ options = [];
304
317
305
318
foreach ($ enum ->getCases () as $ case ) {
@@ -309,14 +322,11 @@ private static function getDataType(\ReflectionProperty $property) : string
309
322
return 'ENUM( \'' . \implode ('\', \'' , $ options ) . '\') ' ;
310
323
}
311
324
325
+ // max number of digits in backing of integer
312
326
$ longestOption = 0 ;
313
327
314
328
foreach ($ enum ->getCases () as $ case ) {
315
- $ length = \mb_strlen ((string ) $ case ->getBackingValue ());
316
-
317
- if ($ length > $ longestOption ) {
318
- $ longestOption = $ length ;
319
- }
329
+ $ longestOption = \max ($ longestOption , \strlen ((string ) $ case ->getBackingValue ()));
320
330
}
321
331
322
332
return 'TINYINT( ' . $ longestOption . ') ' ;
@@ -352,7 +362,7 @@ private static function getClassIndex(\ReflectionClass $bean) : array
352
362
{
353
363
$ return = [];
354
364
355
- foreach ($ bean-> getAttributes ( \CoolBeans \Attribute \ClassIndex::class) as $ i => $ attribute ) {
365
+ foreach (self :: getClassAttributes ( $ bean, \CoolBeans \Attribute \ClassIndex::class) as $ i => $ attribute ) {
356
366
$ indexName = 'index_ ' . $ bean ->getShortName () . '_ ' . $ i ;
357
367
$ columns = $ attribute ->newInstance ()->columns ;
358
368
self ::validateColumnsExists ($ bean , $ columns );
@@ -385,7 +395,7 @@ private static function getClassUnique(\ReflectionClass $bean) : array
385
395
{
386
396
$ return = [];
387
397
388
- foreach ($ bean-> getAttributes ( \CoolBeans \Attribute \ClassUniqueConstraint::class) as $ index => $ attribute ) {
398
+ foreach (self :: getClassAttributes ( $ bean, \CoolBeans \Attribute \ClassUniqueConstraint::class) as $ index => $ attribute ) {
389
399
$ constraintName = 'unique_ ' . $ bean ->getShortName () . '_ ' . $ index ;
390
400
$ columns = $ attribute ->newInstance ()->columns ;
391
401
self ::validateColumnsExists ($ bean , $ columns );
@@ -413,6 +423,7 @@ private static function getCheck(\ReflectionProperty $property, string $beanName
413
423
$ type ->getName () === 'string ' &&
414
424
\count ($ property ->getAttributes (\CoolBeans \Attribute \AllowEmptyString::class)) === 0 ) {
415
425
$ constraintName = 'check_ ' . $ beanName . '_ ' . $ property ->getName () . '_string_not_empty ' ;
426
+
416
427
$ return [] = self ::INDENTATION . 'CONSTRAINT ` ' . $ constraintName . '` CHECK (` ' . $ property ->name . '` != \'\') ' ;
417
428
}
418
429
@@ -423,7 +434,7 @@ private static function getClassCheck(\ReflectionClass $bean) : array
423
434
{
424
435
$ return = [];
425
436
426
- foreach ($ bean-> getAttributes ( \CoolBeans \Attribute \ClassCheckConstraint::class) as $ index => $ attribute ) {
437
+ foreach (self :: getClassAttributes ( $ bean, \CoolBeans \Attribute \ClassCheckConstraint::class) as $ index => $ attribute ) {
427
438
$ constraintName = 'check_ ' . $ bean ->getShortName () . '_ ' . $ index ;
428
439
429
440
$ return [] = self ::INDENTATION . 'CONSTRAINT ` ' . $ constraintName . '` CHECK ( ' . $ attribute ->newInstance ()->expression . ') ' ;
@@ -554,4 +565,17 @@ private function getBeans(string $destination) : array
554
565
555
566
return $ beans ;
556
567
}
568
+
569
+ private static function getClassAttributes (\ReflectionClass $ bean , string $ attributeClass ) : array
570
+ {
571
+ $ return = $ bean ->getAttributes ($ attributeClass );
572
+ $ parent = $ bean ->getParentClass ();
573
+
574
+ while ($ parent instanceof \ReflectionClass) {
575
+ $ return = \array_merge ($ return , $ parent ->getAttributes ($ attributeClass ));
576
+ $ parent = $ parent ->getParentClass ();
577
+ }
578
+
579
+ return $ return ;
580
+ }
557
581
}
0 commit comments