Skip to content

feat: add support for range types #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/BaseRangeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidRangeForDatabaseException;
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidRangeForPHPException;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Range;

/**
* Base class for PostgreSQL range types.
*
* @template T of Range
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
abstract class BaseRangeType extends BaseType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return null;
}

if (!$value instanceof Range) {
throw InvalidRangeForDatabaseException::forInvalidType($value);
}

return (string) $value;
}

/**
* @param mixed $value
*
* @return T|null
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?Range
{
if ($value === null) {
return null;
}

if (!\is_string($value)) {
throw InvalidRangeForPHPException::forInvalidType($value);
}

if ($value === '') {
return null;
}

try {
return $this->createFromString($value);
} catch (\InvalidArgumentException) {
throw InvalidRangeForPHPException::forInvalidFormat($value);
}
}

/**
* @return T
*/
abstract protected function createFromString(string $value): Range;
}
27 changes: 27 additions & 0 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/DateRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\DateRange as DateRangeValueObject;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Range;

/**
* Implementation of PostgreSQL DATERANGE type.
*
* @extends BaseRangeType<DateRangeValueObject>
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class DateRange extends BaseRangeType
{
protected const TYPE_NAME = 'daterange';

protected function createFromString(string $value): Range
{
return DateRangeValueObject::fromString($value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types\Exceptions;

/**
* Exception thrown when an invalid range value is provided for database conversion.
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
final class InvalidRangeForDatabaseException extends \InvalidArgumentException
{
public static function forInvalidType(mixed $value): self
{
return new self(
\sprintf(
'Invalid type for range. Expected Range object or string, got %s',
\get_debug_type($value)
)
);
}

public static function forInvalidFormat(string $value): self
{
return new self(
\sprintf('Invalid range format: %s', $value)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types\Exceptions;

use Doctrine\DBAL\Types\ConversionException;

/**
* Exception thrown when an invalid PHP value is provided for range creation.
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class InvalidRangeForPHPException extends ConversionException
{
private static function create(string $message, mixed $value): self
{
return new self(\sprintf($message, \var_export($value, true)));
}

public static function forInvalidNumericBound(mixed $value): self
{
return self::create('Range bound must be numeric, %s given', $value);
}

public static function forInvalidIntegerBound(mixed $value): self
{
return self::create('Range bound must be an integer, %s given', $value);
}

public static function forInvalidDateTimeBound(mixed $value): self
{
return self::create('Range bound must be a DateTimeInterface instance, %s given', $value);
}

public static function forInvalidType(mixed $value): self
{
return self::create('Invalid database value type for range conversion. Expected string, %s given', $value);
}

public static function forInvalidFormat(string $value): self
{
return new self(\sprintf('Invalid range format from database: %s', $value));
}
}
27 changes: 27 additions & 0 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/Int4Range.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Int4Range as Int4RangeValueObject;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Range;

/**
* Implementation of PostgreSQL INT4RANGE type.
*
* @extends BaseRangeType<Int4RangeValueObject>
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Int4Range extends BaseRangeType
{
protected const TYPE_NAME = 'int4range';

protected function createFromString(string $value): Range
{
return Int4RangeValueObject::fromString($value);
}
}
27 changes: 27 additions & 0 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/Int8Range.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Int8Range as Int8RangeValueObject;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Range;

/**
* Implementation of PostgreSQL INT8RANGE type.
*
* @extends BaseRangeType<Int8RangeValueObject>
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class Int8Range extends BaseRangeType
{
protected const TYPE_NAME = 'int8range';

protected function createFromString(string $value): Range
{
return Int8RangeValueObject::fromString($value);
}
}
27 changes: 27 additions & 0 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/NumRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\NumericRange;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Range;

/**
* Implementation of PostgreSQL NUMRANGE type.
*
* @extends BaseRangeType<NumericRange>
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class NumRange extends BaseRangeType
{
protected const TYPE_NAME = 'numrange';

protected function createFromString(string $value): Range
{
return NumericRange::fromString($value);
}
}
27 changes: 27 additions & 0 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/TsRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Range;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\TsRange as TsRangeValueObject;

/**
* Implementation of PostgreSQL TSRANGE type.
*
* @extends BaseRangeType<TsRangeValueObject>
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class TsRange extends BaseRangeType
{
protected const TYPE_NAME = 'tsrange';

protected function createFromString(string $value): Range
{
return TsRangeValueObject::fromString($value);
}
}
27 changes: 27 additions & 0 deletions src/MartinGeorgiev/Doctrine/DBAL/Types/TstzRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types;

use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Range;
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\TstzRange as TstzRangeValueObject;

/**
* Implementation of PostgreSQL TSTZRANGE type.
*
* @extends BaseRangeType<TstzRangeValueObject>
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
class TstzRange extends BaseRangeType
{
protected const TYPE_NAME = 'tstzrange';

protected function createFromString(string $value): Range
{
return TstzRangeValueObject::fromString($value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace MartinGeorgiev\Doctrine\DBAL\Types\ValueObject;

use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidRangeForPHPException;

/**
* Base class for PostgreSQL integer range types.
*
* @extends Range<int>
*
* @since 3.3
*
* @author Martin Georgiev <martin.georgiev@gmail.com>
*/
abstract class BaseIntegerRange extends Range
{
public function __construct(
?int $lower,
?int $upper,
bool $isLowerBracketInclusive = true,
bool $isUpperBracketInclusive = false,
bool $isExplicitlyEmpty = false,
) {
parent::__construct($lower, $upper, $isLowerBracketInclusive, $isUpperBracketInclusive, $isExplicitlyEmpty);
}

protected function compareBounds(mixed $a, mixed $b): int
{
return $a <=> $b;
}

protected function formatValue(mixed $value): string
{
if (!\is_int($value)) {
throw InvalidRangeForPHPException::forInvalidIntegerBound($value);
}

return (string) $value;
}

protected static function parseValue(string $value): int
{
$intValue = (int) $value;
if ((string) $intValue !== $value) {
throw new \InvalidArgumentException(
\sprintf('Value %s is not a valid integer', $value)
);
}

return $intValue;
}
}
Loading
Loading