Skip to content

Instantly share code, notes, and snippets.

@rccc
Created June 29, 2018 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rccc/73e7a4db9ce0838756ae485e9ba77d85 to your computer and use it in GitHub Desktop.
Save rccc/73e7a4db9ce0838756ae485e9ba77d85 to your computer and use it in GitHub Desktop.
<?php
namespace riccardonar\Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use riccardonar\IntRange;
class Int4RangeType extends Type
{
const INTRANGE = 'int4range';
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return "int4range";
}
/**
* @override
* @param mixed $value
* @param AbstractPlatform $platform
* @return mixed|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return (null === $value) ? null : IntRange::toString($value);
}
/**
* @override
* @param mixed $value
* @param AbstractPlatform $platform
* @return mixed|IntRange
* @throws ConversionException
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (null !== $value) {
if (false == preg_match('/^(\[|\()(\d+),(\d+)(\]|\))$/', $value)) {
throw ConversionException::conversionFailedFormat(
$value,
$this->getName(),
'(\[|\()(\d+),(\d+)(\]|\))$'
);
}
$value = IntRange::fromString($value);
}
return $value;
}
/**
* @override
* @return string
*/
public function getName()
{
return self::INTRANGE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment