Separate Doctrine\UuidTupe into an external library

Closes #51
This commit is contained in:
Ben Ramsey
2015-06-21 18:05:49 -05:00
parent 2d45a33475
commit de02e532b2
5 changed files with 2 additions and 254 deletions
-108
View File
@@ -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;
}
}