Created
June 29, 2018 09:13
-
-
Save rccc/73e7a4db9ce0838756ae485e9ba77d85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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