Skip to content

Commit 15fe420

Browse files
committed
fix: validate string value in convertToDatabaseValue() method
1 parent 3bd9390 commit 15fe420

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/MartinGeorgiev/Doctrine/DBAL/Types/Exceptions/InvalidLtreeForPHPException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public static function forInvalidType(mixed $value): self
1717
{
1818
return self::create('Value must be a LtreeInterface, %s given', \gettype($value));
1919
}
20+
21+
public static function forInvalidFormat(mixed $value): self
22+
{
23+
return self::create('Invalid Ltree format: %s', $value);
24+
}
2025
}

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

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

68-
if ($value instanceof LtreeInterface || \is_string($value)) {
68+
if (\is_string($value)) {
69+
try {
70+
$value = LtreeValueObject::fromString($value);
71+
} catch (\InvalidArgumentException) {
72+
throw InvalidLtreeForPHPException::forInvalidFormat($value);
73+
}
74+
}
75+
76+
if ($value instanceof LtreeInterface) {
6977
return (string) $value;
7078
}
7179

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public function throws_exception_for_invalid_database_value_inputs(mixed $phpVal
111111
public static function provideInvalidDatabaseValueInputs(): array
112112
{
113113
return [
114+
'invalid string' => ['invalid..ltree'],
114115
'integer input' => [123],
115116
'array input' => [['not', 'ltree']],
116117
'boolean input' => [true],

0 commit comments

Comments
 (0)