-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
b971ee0
31c9707
978996e
036661f
22da0b0
7c94c0e
d937631
692c233
6a4ab35
ecb779e
c1a439d
75bd932
b70f8c3
2f55bcb
bf9b2c7
7e62726
3a48123
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\DBAL\Types; | ||
|
||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidRangeForDatabaseException; | ||
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 Type | ||
{ | ||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
{ | ||
return static::TYPE_NAME; | ||
Check failure on line 25 in src/MartinGeorgiev/Doctrine/DBAL/Types/BaseRangeType.php
|
||
} | ||
|
||
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 InvalidRangeForDatabaseException::forInvalidType($value); | ||
} | ||
|
||
if ($value === '') { | ||
return null; | ||
} | ||
|
||
try { | ||
return $this->createFromString($value); | ||
} catch (\InvalidArgumentException) { | ||
throw InvalidRangeForDatabaseException::forInvalidFormat($value); | ||
} | ||
} | ||
|
||
/** | ||
* @return T | ||
*/ | ||
abstract protected function createFromString(string $value): Range; | ||
} |
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 | ||
Check failure on line 19 in src/MartinGeorgiev/Doctrine/DBAL/Types/DateRange.php
|
||
{ | ||
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,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 | ||
Check failure on line 19 in src/MartinGeorgiev/Doctrine/DBAL/Types/Int4Range.php
|
||
{ | ||
protected const TYPE_NAME = 'int4range'; | ||
|
||
protected function createFromString(string $value): Range | ||
{ | ||
return Int4RangeValueObject::fromString($value); | ||
} | ||
} |
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 | ||
Check failure on line 19 in src/MartinGeorgiev/Doctrine/DBAL/Types/Int8Range.php
|
||
{ | ||
protected const TYPE_NAME = 'int8range'; | ||
|
||
protected function createFromString(string $value): Range | ||
{ | ||
return Int8RangeValueObject::fromString($value); | ||
} | ||
} | ||
martin-georgiev marked this conversation as resolved.
Show resolved
Hide resolved
|
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 | ||
Check failure on line 19 in src/MartinGeorgiev/Doctrine/DBAL/Types/NumRange.php
|
||
{ | ||
protected const TYPE_NAME = 'numrange'; | ||
|
||
protected function createFromString(string $value): Range | ||
{ | ||
return NumericRange::fromString($value); | ||
} | ||
} | ||
martin-georgiev marked this conversation as resolved.
Show resolved
Hide resolved
|
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 | ||
Check failure on line 19 in src/MartinGeorgiev/Doctrine/DBAL/Types/TsRange.php
|
||
{ | ||
protected const TYPE_NAME = 'tsrange'; | ||
|
||
protected function createFromString(string $value): Range | ||
{ | ||
return TsRangeValueObject::fromString($value); | ||
} | ||
} |
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 | ||
Check failure on line 19 in src/MartinGeorgiev/Doctrine/DBAL/Types/TstzRange.php
|
||
{ | ||
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,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\DBAL\Types\ValueObject; | ||
|
||
/** | ||
* Base class for PostgreSQL integer range types. | ||
* | ||
* @since 3.3 | ||
* | ||
* @author Martin Georgiev <martin.georgiev@gmail.com> | ||
*/ | ||
abstract class BaseIntegerRange extends Range | ||
Check failure on line 14 in src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/BaseIntegerRange.php
|
||
{ | ||
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 | ||
{ | ||
return (string) $value; | ||
Check failure on line 33 in src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/BaseIntegerRange.php
|
||
} | ||
|
||
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MartinGeorgiev\Doctrine\DBAL\Types\ValueObject; | ||
|
||
/** | ||
* Base class for PostgreSQL timestamp range types. | ||
* | ||
* @since 3.3 | ||
* | ||
* @author Martin Georgiev <martin.georgiev@gmail.com> | ||
*/ | ||
abstract class BaseTimestampRange extends Range | ||
Check failure on line 14 in src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/BaseTimestampRange.php
|
||
{ | ||
public function __construct( | ||
?\DateTimeInterface $lower, | ||
?\DateTimeInterface $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 | ||
{ | ||
$timestampComparison = $a->getTimestamp() <=> $b->getTimestamp(); | ||
Check failure on line 28 in src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/BaseTimestampRange.php
|
||
if ($timestampComparison !== 0) { | ||
return $timestampComparison; | ||
} | ||
|
||
// PHP's getTimestamp() only returns seconds, so we need to separate the microsecond comparison. | ||
return (int) $a->format('u') <=> (int) $b->format('u'); | ||
Check failure on line 34 in src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/BaseTimestampRange.php
|
||
} | ||
|
||
protected static function parseValue(string $value): \DateTimeImmutable | ||
{ | ||
try { | ||
return new \DateTimeImmutable($value); | ||
} catch (\Exception $exception) { | ||
throw new \InvalidArgumentException( | ||
\sprintf('Invalid timestamp value: %s. Error: %s', $value, $exception->getMessage()), | ||
0, | ||
$exception | ||
); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.