22 private const TYPE_TO_CLASS_MAP = [
23 'integer' => IntegerField::class,
24 'float' => FloatField::class,
25 'boolean' => BooleanField::class,
26 'string' => StringField::class,
27 'text' => TextField::class,
28 'enum' => EnumField::class,
29 'date' => DateField::class,
30 'datetime' => DatetimeField::class,
33 private const TYPE_SUPPORT_LENGTH_VALIDATOR = [
38 private array $usedClasses;
52 private readonly
string $columnName,
53 private readonly
string $typeOrm,
54 private readonly
bool $isPrimary,
55 private readonly
bool $isUnique,
56 private readonly
bool $isIncrement,
57 private readonly
bool $isNullable,
59 private readonly ?
int $size,
60 private readonly ?
int $maxLength,
70 return $this->usedClasses;
75 [$className, $fullClassName] = $this->getFieldClassName();
76 $this->usedClasses[] = $fullClassName;
82 $configureCode[] =
"->configurePrimary()";
84 elseif (!$this->isNullable)
86 $configureCode[] =
"->configureRequired()";
90 $configureCode[] =
"->configureNullable()";
93 if ($this->isIncrement)
95 $configureCode[] =
"->configureAutocomplete()";
100 $configureCode[] =
"->configureUnique()";
103 if (isset($this->defaultValue))
105 $configureCode[] =
"->configureDefaultValue({$this->getDefaultValueCode()})";
108 if (isset($this->
size))
110 $configureCode[] =
"->configureSize({$this->size})";
113 if (isset($this->maxLength) && in_array($this->typeOrm, self::TYPE_SUPPORT_LENGTH_VALIDATOR))
115 $min = $this->isNullable ? 0 : 1;
116 $max = $this->maxLength;
118 $configureCode[] =
"->addValidator(new LengthValidator(min:{$min}, max:{$max}))";
119 $this->usedClasses[] = LengthValidator::class;
122 if ($this->typeOrm ===
'boolean')
124 if (in_array($this->defaultValue, [
'Y',
'N']))
126 $configureCode[] =
"->configureValues('N', 'Y')";
131 "\t\t\t" .
"(new {$className}('{$this->columnName}'))",
133 static fn($line) =>
"\t\t\t\t" . $line,
139 return join(
"\n", $resultCode);
142 private function getFieldClassName():
array
144 $className = self::TYPE_TO_CLASS_MAP[$this->typeOrm] ??
null;
145 if (empty($className))
147 throw new ArgumentException(
"Invalid type '{$this->typeOrm}' for field '{$this->columnName}'");
150 $sortClassName = (
new ReflectionClass($className))->getShortName();
152 return [$sortClassName, $className];
155 private function getDefaultValueCode(): string
157 if ($this->typeOrm ===
'datetime' || $this->typeOrm ===
'date')
161 'current_date' =>
true,
162 'current_time' =>
true,
163 'current_timestamp' =>
true,
166 'localtimestamp' =>
true,
170 if (!is_numeric($this->defaultValue))
172 $prepareValue = mb_strtolower($this->defaultValue);
174 mb_strlen($prepareValue) > 2
175 && substr_compare($prepareValue,
'()', -2, 2,
true) === 0
178 $prepareValue = mb_substr($prepareValue, 0, -2);
181 if (isset($dateFunctions[$prepareValue]))
183 if ($this->typeOrm ==
'date')
185 $this->usedClasses[] = Date::class;
187 return "static fn() => Date()";
191 $this->usedClasses[] = DateTime::class;
193 return "static fn() => DateTime()";
199 return "'" . addslashes($this->defaultValue) .
"'";
__construct(private readonly string $columnName, private readonly string $typeOrm, private readonly bool $isPrimary, private readonly bool $isUnique, private readonly bool $isIncrement, private readonly bool $isNullable, private readonly mixed $defaultValue, private readonly ?int $size, private readonly ?int $maxLength,)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)