Skip to content

Instantly share code, notes, and snippets.

@Djourdain
Created July 21, 2023 13:01
Show Gist options
  • Save Djourdain/07b78d2211138e76979cbbeaccdb0d8e to your computer and use it in GitHub Desktop.
Save Djourdain/07b78d2211138e76979cbbeaccdb0d8e to your computer and use it in GitHub Desktop.
<?php
namespace Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class Int4Range extends Type
{
const INT_4_RANGE = "int4range";
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return "int4range";
}
public function canRequireSQLConversion(): bool
{
return true;
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
$separatorPosition = strpos($value, ",");
$min = (int)substr($value, 1, $separatorPosition);
$isMinIncluded = "[" == substr($value, 0, 1 );
$max = (int)substr($value, $separatorPosition+1,-1);
$isMaxIncluded = "]" == substr($value, -1);
return [
$isMinIncluded ? $min : $min + 1,
$isMaxIncluded ? $max : $max - 1,
];
}
public function getName(): string
{
return self::INT_4_RANGE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment