mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
@@ -1,108 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the Ramsey\Uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) 2012-2014 Ben Ramsey <http://benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
namespace Ramsey\Uuid\Doctrine;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Doctrine\DBAL\Types\ConversionException;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
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 Type
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
const NAME = 'uuid';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param array $fieldDeclaration
|
||||
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
|
||||
*/
|
||||
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
|
||||
{
|
||||
return $platform->getGuidTypeDeclarationSQL($fieldDeclaration);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param string|null $value
|
||||
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
|
||||
*/
|
||||
public function convertToPHPValue($value, AbstractPlatform $platform)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($value instanceof Uuid) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
try {
|
||||
$uuid = Uuid::fromString($value);
|
||||
} catch (InvalidArgumentException $e) {
|
||||
throw ConversionException::conversionFailed($value, self::NAME);
|
||||
}
|
||||
|
||||
return $uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param Uuid|null $value
|
||||
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
|
||||
*/
|
||||
public function convertToDatabaseValue($value, AbstractPlatform $platform)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($value instanceof Uuid || Uuid::isValid($value)) {
|
||||
return (string) $value;
|
||||
}
|
||||
|
||||
throw ConversionException::conversionFailed($value, self::NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return self::NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
|
||||
* @return boolean
|
||||
*/
|
||||
public function requiresSQLCommentHint(AbstractPlatform $platform)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user