37 lines
1.3 KiB
PHP
37 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Adapter;
|
|
|
|
use Ulmus\Migration\FieldDefinition;
|
|
|
|
class MySQLFieldMapper extends SqlFieldMapper
|
|
{
|
|
public function map() : void
|
|
{
|
|
$type = $this->field->type;
|
|
$length = $this->field->length;
|
|
|
|
if ( enum_exists($type) ) {
|
|
# Haven't found a better way yet to check for BackendEnum without an instance of the object
|
|
if ( ! method_exists($type, 'tryFrom') ) {
|
|
throw new \Ulmus\Exception\BackedEnumRequired(sprintf("You must define your enum as a BackedEnum instead of an UnitEnum for field '%s'.", $this->field->getColumnName()));
|
|
}
|
|
|
|
$this->length = implode(',', array_map(fn($e) => MySQL::escapeIdentifier($e->value, MySQL::IDENTIFIER_VALUE) , $type::cases()));
|
|
$this->type = "ENUM";
|
|
}
|
|
else {
|
|
parent::map();
|
|
}
|
|
}
|
|
|
|
public function postProcess() : void
|
|
{
|
|
if (
|
|
in_array($this->type, [ 'BLOB', 'TINYBLOB', 'MEDIUMBLOB', 'LONGBLOB', 'JSON', 'TEXT', 'TINYTEXT', 'MEDIUMTEXT', 'LONGTEXT', 'GEOMETRY' ]) &&
|
|
! is_object($this->field->default ?? false) # Could be a functional default, which would now be valid
|
|
) {
|
|
unset($this->field->default);
|
|
}
|
|
}
|
|
} |