Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[make:entity] default value #1558

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
86 changes: 86 additions & 0 deletions src/DefaultValueValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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));
}
}
9 changes: 9 additions & 0 deletions src/Maker/MakeEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 <return> if no default value)', null, DefaultValueValidator::getValidator($classProperty->type));
if (null !== $defaultValue) {
$classProperty->defaultValue = $defaultValue;
$classProperty->options['default'] = $classProperty->defaultValue;
}
}

return $classProperty;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Util/ClassSource/Model/ClassProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}
Expand Down Expand Up @@ -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,
);
}
Expand All @@ -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,
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function getTestDetails(): \Generator
'',
// nullable
'',
// no default value
'',
// no more properties
'',
]);
Expand Down Expand Up @@ -220,6 +222,8 @@ public function getTestDetails(): \Generator
'', // length (default 255)
// nullable
'y',
// no default value
'',
// finish adding fields
'',
]);
Expand Down Expand Up @@ -628,6 +632,8 @@ public function getTestDetails(): \Generator
'', // length (default 255)
// nullable
'',
// no default value
'',
// finish adding fields
'',
], '--overwrite');
Expand Down Expand Up @@ -732,6 +738,8 @@ public function getTestDetails(): \Generator
'',
// nullable
'y',
// no default value
'',
// finish adding fields
'',
]);
Expand Down
Loading