Skip to content

Commit

Permalink
feature symfony#1557 [make:entity] Added prompt for default value if …
Browse files Browse the repository at this point in the history
…possible
  • Loading branch information
MathisBurger committed May 10, 2024
1 parent 6960c1c commit a4b0f45
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
77 changes: 77 additions & 0 deletions src/DefaultValueValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

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 $value === 'true';
}
throw new RuntimeCommandException(sprintf("Value %s is invalid for type boolean", $value));
}

public static function validateFloat(mixed $value): float
{
if (is_numeric($value)) {
return floatval($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 = intval($value);
if ($value === '0' && $val === 0) {
return $val;
} else if ($val !== 0) {
return $val;
}
}
throw new RuntimeCommandException(sprintf("Value %s is invalid for type int", $value));
}
}
10 changes: 10 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 @@ -436,6 +437,15 @@ 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 ($defaultValue !== null) {
$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,
) {
}

Expand Down Expand Up @@ -74,6 +75,7 @@ public static function createFromObject(FieldMapping|array $data): self
precision: $data->precision,
scale: $data->scale,
unique: $data->unique ?? false,
defaultValue: $data->defaultValue ?? null
);
}

Expand All @@ -93,6 +95,7 @@ public static function createFromObject(FieldMapping|array $data): self
precision: $data['precision'] ?? null,
scale: $data['scale'] ?? null,
unique: $data['unique'] ?? false,
defaultValue: $data['defaultValue'] ?? null
);
}
}
3 changes: 2 additions & 1 deletion src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ public function addEntityField(ClassProperty $mapping): void
$nullable = $mapping->nullable ?? false;

$attributes[] = $this->buildAttributeNode(Column::class, $mapping->getAttributes(), 'ORM');

$defaultValue = null;
if ('array' === $typeHint && !$nullable) {
$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));
} else if ($mapping->defaultValue) {
$defaultValue = $mapping->defaultValue;
}

$propertyType = $typeHint;
Expand Down

0 comments on commit a4b0f45

Please sign in to comment.