|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\DBAL\Types; |
| 6 | + |
| 7 | +use InvalidArgumentException; |
| 8 | +use Stringable; |
| 9 | + |
| 10 | +use function preg_match; |
| 11 | +use function sprintf; |
| 12 | + |
| 13 | +/** |
| 14 | + * Value object representing spatial geometry data with optional SRID (Spatial Reference System Identifier). |
| 15 | + * |
| 16 | + * This class separates the WKT representation from the SRID metadata, providing a clean abstraction |
| 17 | + * that works across different database platforms. PostgreSQL uses EWKT format (SRID prefix in text), |
| 18 | + * while MySQL stores SRID separately in the geometry binary format. |
| 19 | + */ |
| 20 | +final class Geometry implements Stringable |
| 21 | +{ |
| 22 | + private function __construct( |
| 23 | + private readonly WKT $wkt, |
| 24 | + private readonly int|null $srid, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a Geometry from WKT text and optional SRID. |
| 30 | + * |
| 31 | + * @param string $wkt Well-Known Text representation of the geometry |
| 32 | + * @param int|null $srid Spatial Reference System Identifier (e.g., 4326 for WGS84) |
| 33 | + */ |
| 34 | + public static function fromWkt(string $wkt, int|null $srid = null): self |
| 35 | + { |
| 36 | + return new self(WKT::fromString($wkt), $srid); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a Geometry from Extended Well-Known Text (EWKT) format. |
| 41 | + * |
| 42 | + * EWKT format includes the SRID prefix: "SRID=4326;POINT(1 2)" |
| 43 | + * This method parses the SRID and WKT components automatically. |
| 44 | + * |
| 45 | + * @param string $ewkt Extended Well-Known Text with SRID prefix |
| 46 | + * |
| 47 | + * @throws InvalidArgumentException If the EWKT format is invalid. |
| 48 | + */ |
| 49 | + public static function fromEwkt(string $ewkt): self |
| 50 | + { |
| 51 | + if (preg_match('/^SRID=(\d+);(.+)$/is', $ewkt, $matches)) { |
| 52 | + return new self( |
| 53 | + WKT::fromString($matches[2]), |
| 54 | + (int) $matches[1], |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + // No SRID prefix, treat as standard WKT |
| 59 | + return new self(WKT::fromString($ewkt), null); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns the Well-Known Text representation. |
| 64 | + */ |
| 65 | + public function getWkt(): WKT |
| 66 | + { |
| 67 | + return $this->wkt; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Returns the Spatial Reference System Identifier, if set. |
| 72 | + */ |
| 73 | + public function getSrid(): int|null |
| 74 | + { |
| 75 | + return $this->srid; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns Extended Well-Known Text format (EWKT) if SRID is set, otherwise standard WKT. |
| 80 | + * |
| 81 | + * Examples: |
| 82 | + * - With SRID: "SRID=4326;POINT(1 2)" |
| 83 | + * - Without SRID: "POINT(1 2)" |
| 84 | + */ |
| 85 | + public function toEwkt(): string |
| 86 | + { |
| 87 | + if ($this->srid !== null) { |
| 88 | + return sprintf('SRID=%d;%s', $this->srid, $this->wkt->toString()); |
| 89 | + } |
| 90 | + |
| 91 | + return $this->wkt->toString(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns Extended Well-Known Text format (alias for toEwkt). |
| 96 | + */ |
| 97 | + public function __toString(): string |
| 98 | + { |
| 99 | + return $this->toEwkt(); |
| 100 | + } |
| 101 | +} |
0 commit comments