diff --git a/src/DefaultValueValidator.php b/src/DefaultValueValidator.php new file mode 100644 index 000000000..87e1204f9 --- /dev/null +++ b/src/DefaultValueValidator.php @@ -0,0 +1,86 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MakerBundle; + +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; + +/** + * Validator made for default values. + */ +class DefaultValueValidator +{ + /** + * All currently supported types with default value. + */ + public const SUPPORTED_TYPES = [ + 'string', + 'ascii_string', + 'text', + 'boolean', + 'integer', + 'smallint', + 'bigint', + 'float', + ]; + + /** + * Gets the validator that belongs to type. + * + * @param string $type The type in doctrine format + * + * @return callable|null The validator that belongs to type + */ + public static function getValidator(string $type): ?callable + { + switch ($type) { + case 'boolean': + return self::validateBoolean(...); + case 'float': + return self::validateFloat(...); + case 'integer': + case 'smallint': + case 'bigint': + return self::validateInt(...); + } + + return null; + } + + public static function validateBoolean(mixed $value): bool + { + if (\in_array($value, ['true', 'false'])) { + return 'true' === $value; + } + throw new RuntimeCommandException(\sprintf('Value %s is invalid for type boolean', $value)); + } + + public static function validateFloat(mixed $value): float + { + if (is_numeric($value)) { + return (float) $value; + } + throw new RuntimeCommandException(\sprintf('Value %s is invalid for type float', $value)); + } + + public static function validateInt(mixed $value): int + { + if (is_numeric($value)) { + $val = (int) $value; + if ('0' === $value && 0 === $val) { + return $val; + } elseif (0 !== $val) { + return $val; + } + } + throw new RuntimeCommandException(\sprintf('Value %s is invalid for type int', $value)); + } +} diff --git a/src/Maker/MakeEntity.php b/src/Maker/MakeEntity.php index e0498f36b..b75dceb16 100644 --- a/src/Maker/MakeEntity.php +++ b/src/Maker/MakeEntity.php @@ -14,6 +14,7 @@ use ApiPlatform\Metadata\ApiResource; use Doctrine\DBAL\Types\Type; use Symfony\Bundle\MakerBundle\ConsoleStyle; +use Symfony\Bundle\MakerBundle\DefaultValueValidator; use Symfony\Bundle\MakerBundle\DependencyBuilder; use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper; use Symfony\Bundle\MakerBundle\Doctrine\EntityClassGenerator; @@ -441,6 +442,14 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity $classProperty->nullable = true; } + if (\in_array($classProperty->type, DefaultValueValidator::SUPPORTED_TYPES)) { + $defaultValue = $io->ask('Please enter your default value (press if no default value)', null, DefaultValueValidator::getValidator($classProperty->type)); + if (null !== $defaultValue) { + $classProperty->defaultValue = $defaultValue; + $classProperty->options['default'] = $classProperty->defaultValue; + } + } + return $classProperty; } diff --git a/src/Util/ClassSource/Model/ClassProperty.php b/src/Util/ClassSource/Model/ClassProperty.php index a57406d63..1b94792fa 100644 --- a/src/Util/ClassSource/Model/ClassProperty.php +++ b/src/Util/ClassSource/Model/ClassProperty.php @@ -33,6 +33,7 @@ public function __construct( public ?int $scale = null, public bool $needsTypeHint = true, public bool $unique = false, + public mixed $defaultValue = null, public ?string $enumType = null, ) { } @@ -79,6 +80,7 @@ public static function createFromObject(FieldMapping|array $data): self precision: $data->precision, scale: $data->scale, unique: $data->unique ?? false, + defaultValue: $data->defaultValue ?? null, enumType: $data->enumType, ); } @@ -99,6 +101,7 @@ enumType: $data->enumType, precision: $data['precision'] ?? null, scale: $data['scale'] ?? null, unique: $data['unique'] ?? false, + defaultValue: $data['defaultValue'] ?? null, enumType: $data['enumType'] ?? null, ); } diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 25b57bdbb..9cf7175f3 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -118,7 +118,6 @@ public function addEntityField(ClassProperty $mapping): void $nullable = $mapping->nullable ?? false; $attributes[] = $this->buildAttributeNode(Column::class, $mapping->getAttributes(), 'ORM'); - $defaultValue = null; $commentLines = []; @@ -140,6 +139,8 @@ public function addEntityField(ClassProperty $mapping): void $defaultValue = new Node\Expr\Array_([], ['kind' => Node\Expr\Array_::KIND_SHORT]); } elseif ($typeHint && '\\' === $typeHint[0] && false !== strpos($typeHint, '\\', 1)) { $typeHint = $this->addUseStatementIfNecessary(substr($typeHint, 1)); + } elseif ($mapping->defaultValue) { + $defaultValue = $mapping->defaultValue; } $propertyType = $typeHint; diff --git a/tests/Maker/MakeEntityTest.php b/tests/Maker/MakeEntityTest.php index 54059bad3..ef5061b33 100644 --- a/tests/Maker/MakeEntityTest.php +++ b/tests/Maker/MakeEntityTest.php @@ -83,6 +83,8 @@ public function getTestDetails(): \Generator '', // nullable '', + // no default value + '', // no more properties '', ]); @@ -220,6 +222,8 @@ public function getTestDetails(): \Generator '', // length (default 255) // nullable 'y', + // no default value + '', // finish adding fields '', ]); @@ -628,6 +632,8 @@ public function getTestDetails(): \Generator '', // length (default 255) // nullable '', + // no default value + '', // finish adding fields '', ], '--overwrite'); @@ -732,6 +738,8 @@ public function getTestDetails(): \Generator '', // nullable 'y', + // no default value + '', // finish adding fields '', ]);