Skip to content

Commit 5d1a8d5

Browse files
committed
fix: supports DBAL 2, and fix convertToDatabaseValue to accept string values
1 parent 6782cdf commit 5d1a8d5

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/MartinGeorgiev/Doctrine/DBAL/Types/Ltree.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
6565
{
6666
$this->assertPostgreSQLPlatform($platform);
6767

68-
if ($value instanceof LtreeInterface) {
68+
if ($value instanceof LtreeInterface || \is_string($value)) {
6969
return (string) $value;
7070
}
7171

@@ -78,8 +78,13 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?str
7878

7979
private function assertPostgreSQLPlatform(AbstractPlatform $platform): void
8080
{
81-
if (!$platform instanceof PostgreSQLPlatform) {
82-
throw new \LogicException('Ltree DBAL type can only be used with the PostgreSQL platform.');
81+
$isDbalTwoPostgres = \class_exists(PostgreSQLPlatform::class)
82+
&& \is_a($platform, '\Doctrine\DBAL\Platforms\PostgreSqlPlatform');
83+
84+
if ($platform instanceof PostgreSQLPlatform || $isDbalTwoPostgres) {
85+
return;
8386
}
87+
88+
throw new \LogicException('Ltree DBAL type can only be used with the PostgreSQL platform.');
8489
}
8590
}

tests/Unit/MartinGeorgiev/Doctrine/DBAL/Types/LtreeTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public function test_get_mapped_database_types(): void
5050
self::assertSame(['ltree'], $this->fixture->getMappedDatabaseTypes($this->platform));
5151
}
5252

53+
public function test_convert_to_database_value_accepts_string(): void
54+
{
55+
$value = 'alpha.beta.gamma';
56+
$databaseValue = $this->fixture->convertToDatabaseValue($value, $this->platform);
57+
58+
self::assertSame($value, $databaseValue);
59+
}
60+
5361
#[DataProvider('provideValidTransformations')]
5462
#[Test]
5563
public function can_transform_from_php_value(?LtreeValueObject $ltreeValueObject, ?string $postgresValue): void
@@ -103,7 +111,6 @@ public function throws_exception_for_invalid_database_value_inputs(mixed $phpVal
103111
public static function provideInvalidDatabaseValueInputs(): array
104112
{
105113
return [
106-
'string input' => ['not..ltree'],
107114
'integer input' => [123],
108115
'array input' => [['not', 'ltree']],
109116
'boolean input' => [true],

0 commit comments

Comments
 (0)