<?php
/**
* This file is part of the ramsey/uuid-doctrine library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Ramsey\Uuid\Doctrine;
use InvalidArgumentException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\GuidType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Field type mapping for the Doctrine Database Abstraction Layer (DBAL).
*
* UUID fields will be stored as a string in the database and converted back to
* the Uuid value object when querying.
*/
class UuidType extends GuidType
{
/**
* @var string
*/
const NAME = 'uuid';
/**
* {@inheritdoc}
*
* @param string|UuidInterface|null $value
* @param AbstractPlatform $platform
*
* @return UuidInterface|null
*
* @throws ConversionException
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value === '') {
return null;
}
if ($value instanceof UuidInterface) {
return $value;
}
try {
$uuid = Uuid::fromString($value);
} catch (InvalidArgumentException $e) {
throw ConversionException::conversionFailed($value, static::NAME);
}
return $uuid;
}
/**
* {@inheritdoc}
*
* @param UuidInterface|string|null $value
* @param AbstractPlatform $platform
*
* @return string|null
*
* @throws ConversionException
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value === null || $value === '') {
return null;
}
if (
$value instanceof UuidInterface
|| (
(is_string($value)
|| (is_object($value) && method_exists($value, '__toString')))
&& Uuid::isValid((string) $value)
)
) {
return (string) $value;
}
throw ConversionException::conversionFailed($value, static::NAME);
}
/**
* {@inheritdoc}
*
* @return string
*/
public function getName()
{
return static::NAME;
}
/**
* {@inheritdoc}
*
* @param AbstractPlatform $platform
*
* @return bool
*/
public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}
/**
* @param AbstractPlatform $platform
*
* @return array
*/
public function getMappedDatabaseTypes(AbstractPlatform $platform)
{
return [self::NAME];
}
}