Skip to content

Commit c4f4574

Browse files
chore: refactor exceptions for Point's value object (#426)
1 parent 04e0a54 commit c4f4574

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidPointForDatabaseException;
99
use MartinGeorgiev\Doctrine\DBAL\Types\Exceptions\InvalidPointForPHPException;
10+
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Exceptions\InvalidPointException;
1011
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Point as PointValueObject;
1112

1213
/**
@@ -46,7 +47,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?PointVal
4647

4748
try {
4849
return PointValueObject::fromString($value);
49-
} catch (\InvalidArgumentException) {
50+
} catch (InvalidPointException) {
5051
throw InvalidPointForDatabaseException::forInvalidFormat($value);
5152
}
5253
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Exceptions;
6+
7+
/**
8+
* Exception thrown when creating or manipulating Point value objects with invalid data.
9+
*
10+
* This exception is specifically for validation errors within the Point value object itself,
11+
* separate from DBAL conversion exceptions.
12+
*
13+
* @since 3.5
14+
*
15+
* @author Martin Georgiev <martin.georgiev@gmail.com>
16+
*/
17+
final class InvalidPointException extends \InvalidArgumentException
18+
{
19+
public static function forInvalidPointFormat(string $pointString, string $expectedPattern): self
20+
{
21+
return new self(\sprintf(
22+
'Invalid point format. Expected format matching %s, got: %s',
23+
\var_export($expectedPattern, true),
24+
\var_export($pointString, true)
25+
));
26+
}
27+
28+
public static function forInvalidCoordinate(string $coordinateName, string $value): self
29+
{
30+
return new self(\sprintf(
31+
'Invalid %s coordinate format: %s',
32+
\var_export($coordinateName, true),
33+
\var_export($value, true)
34+
));
35+
}
36+
}

src/MartinGeorgiev/Doctrine/DBAL/Types/ValueObject/Point.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace MartinGeorgiev\Doctrine\DBAL\Types\ValueObject;
66

7+
use MartinGeorgiev\Doctrine\DBAL\Types\ValueObject\Exceptions\InvalidPointException;
8+
79
/**
810
* @since 3.1
911
*
@@ -41,9 +43,7 @@ public function getY(): float
4143
public static function fromString(string $pointString): self
4244
{
4345
if (!\preg_match(self::POINT_REGEX, $pointString, $matches)) {
44-
throw new \InvalidArgumentException(
45-
\sprintf('Invalid point format. Expected format matching %s, got: %s', self::POINT_REGEX, $pointString)
46-
);
46+
throw InvalidPointException::forInvalidPointFormat($pointString, self::POINT_REGEX);
4747
}
4848

4949
return new self((float) $matches[1], (float) $matches[2]);
@@ -55,9 +55,7 @@ private function validateCoordinate(float $value, string $name): void
5555

5656
$floatRegex = '/^'.self::COORDINATE_PATTERN.'$/';
5757
if (!\preg_match($floatRegex, $stringValue)) {
58-
throw new \InvalidArgumentException(
59-
\sprintf('Invalid %s coordinate format: %s', $name, $stringValue)
60-
);
58+
throw InvalidPointException::forInvalidCoordinate($name, $stringValue);
6159
}
6260
}
6361
}

0 commit comments

Comments
 (0)