<?php

namespace App\Tools\DBAL;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;

class EnumPasswordTokenTypeType extends Type
{
    const ENUM_NAME = 'EnumPasswordTokenTypeType';
    const TYPE_NEW = 'NEW';
    const TYPE_RESET = 'RESET';

    protected static $values = [
        self::TYPE_NEW,
        self::TYPE_RESET,
    ];

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return sprintf('ENUM('.str_repeat("'%s', ", count(self::$values) - 1)."'%s')", ...self::$values);
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if ($value && !in_array($value, self::$values)) {
            throw new \InvalidArgumentException('Invalid value for '.self::getName());
        }

        return $value;
    }

    public function getName()
    {
        return self::ENUM_NAME;
    }

    public function requiresSQLCommentHint(AbstractPlatform $platform)
    {
        return true;
    }
}
