-
-
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
Open
martin-georgiev
wants to merge
17
commits into
main
Choose a base branch
from
range-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b971ee0
feat: add support for range types
martin-georgiev 31c9707
Drop AI noise that is not strictly required.
martin-georgiev 978996e
Drop AI noise that is not strictly required.
martin-georgiev 036661f
Address code duplication
martin-georgiev 22da0b0
Add the correct integration tests
martin-georgiev 7c94c0e
Simplify unit tests
martin-georgiev d937631
Improve error handling
martin-georgiev 692c233
Merge branch 'main' into range-types
martin-georgiev 6a4ab35
Narrow down interfaced method
martin-georgiev ecb779e
Use template for BaseRangeTestCase
martin-georgiev c1a439d
Accept errors as a known PHPStan limitation rather than worked around…
martin-georgiev 75bd932
Add basic documentation
martin-georgiev b70f8c3
Add examples for range's VO and DT usage
martin-georgiev 2f55bcb
Add more tests
martin-georgiev bf9b2c7
Fix broken tests
martin-georgiev 7e62726
YAGNI tests
martin-georgiev 3a48123
add PHPStan template annotations and suppress covariant generics
martin-georgiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
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; | ||
} |
This file contains hidden or 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
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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/MartinGeorgiev/Doctrine/DBAL/Types/Exceptions/InvalidRangeForDatabaseException.php
This file contains hidden or 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
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) | ||
); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/MartinGeorgiev/Doctrine/DBAL/Types/Exceptions/InvalidRangeForPHPException.php
This file contains hidden or 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
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)); | ||
} | ||
} |
This file contains hidden or 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
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); | ||
} | ||
} |
This file contains hidden or 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
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); | ||
} | ||
} | ||
This file contains hidden or 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
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); | ||
} | ||
} | ||
martin-georgiev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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
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); | ||
} | ||
} |
This file contains hidden or 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
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); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/BaseIntegerRange.php
This file contains hidden or 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
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.