45 lines
873 B
PHP
45 lines
873 B
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace App\Types;
|
|
|
|
use BackedEnum;
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
|
use Doctrine\DBAL\Types\Type;
|
|
|
|
abstract class AbstractEnumType extends Type
|
|
{
|
|
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
|
|
{
|
|
return 'TEXT';
|
|
}
|
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
|
|
{
|
|
if (is_scalar($value))
|
|
{
|
|
return $value;
|
|
}
|
|
|
|
if ($value instanceof BackedEnum)
|
|
{
|
|
return $value->value;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform): mixed
|
|
{
|
|
if (false === enum_exists($this::getEnumsClass(), true))
|
|
{
|
|
throw new \LogicException("This class should be an enum");
|
|
}
|
|
|
|
$enum = $this::getEnumsClass();
|
|
|
|
return $enum::from($value);
|
|
}
|
|
|
|
abstract public static function getEnumsClass(): string;
|
|
}
|