diff --git a/CHANGELOG.md b/CHANGELOG.md index 918a71f..27b9e4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ 💥 **Breaking changes** - **Minimum PHP version is now `8.1`** +- `AbstractWKBWriter::setByteOrder()` now accepts a `WKBByteOrder` enum instead of a `WKBTools::*_ENDIAN` constant +- constants `WKBTools::BIG_ENDIAN` and `WKBTools::LITTLE_ENDIAN` have been removed +- method `WKBTools::checkByteOrder()` has been removed ## [0.9.0](https://github.com/brick/geo/releases/tag/0.9.0) - 2022-11-22 diff --git a/src/IO/AbstractWKBWriter.php b/src/IO/AbstractWKBWriter.php index e1f1326..9363bfd 100644 --- a/src/IO/AbstractWKBWriter.php +++ b/src/IO/AbstractWKBWriter.php @@ -21,15 +21,9 @@ */ abstract class AbstractWKBWriter { - /** - * @psalm-var WKBTools::BIG_ENDIAN|WKBTools::LITTLE_ENDIAN - */ - private int $byteOrder; + private WKBByteOrder $byteOrder; - /** - * @psalm-var WKBTools::BIG_ENDIAN|WKBTools::LITTLE_ENDIAN - */ - private int $machineByteOrder; + private WKBByteOrder $machineByteOrder; /** * @throws GeometryIOException @@ -39,15 +33,8 @@ public function __construct() $this->byteOrder = $this->machineByteOrder = WKBTools::getMachineByteOrder(); } - /** - * @param int $byteOrder The byte order, one of the WKBTools::BIG_ENDIAN or WKBTools::LITTLE_ENDIAN constants. - * - * @throws \InvalidArgumentException If the byte order is invalid. - */ - public function setByteOrder(int $byteOrder) : void + public function setByteOrder(WKBByteOrder $byteOrder) : void { - WKBTools::checkByteOrder($byteOrder); - /** @var WKBTools::BIG_ENDIAN|WKBTools::LITTLE_ENDIAN byteOrder */ $this->byteOrder = $byteOrder; } @@ -110,12 +97,15 @@ protected function doWrite(Geometry $geometry, bool $outer) : string private function packByteOrder() : string { - return pack('C', $this->byteOrder); + return pack('C', $this->byteOrder->value); } protected function packUnsignedInteger(int $uint) : string { - return pack($this->byteOrder === WKBTools::BIG_ENDIAN ? 'N' : 'V', $uint); + return pack(match ($this->byteOrder) { + WKBByteOrder::BIG_ENDIAN => 'N', + WKBByteOrder::LITTLE_ENDIAN => 'V' + }, $uint); } private function packDouble(float $double) : string diff --git a/src/IO/WKBBuffer.php b/src/IO/WKBBuffer.php index 8165cb5..50e1573 100644 --- a/src/IO/WKBBuffer.php +++ b/src/IO/WKBBuffer.php @@ -14,8 +14,7 @@ class WKBBuffer private readonly string $wkb; private readonly int $length; private int $position = 0; - /** @psalm-var WKBTools::BIG_ENDIAN|WKBTools::LITTLE_ENDIAN */ - private readonly int $machineByteOrder; + private readonly WKBByteOrder $machineByteOrder; private bool $invert = false; public function __construct(string $wkb) @@ -105,12 +104,13 @@ public function readDoubles(int $count) : array public function readByteOrder() : void { $byteOrder = $this->readUnsignedChar(); + $wkbByteOrder = WKBByteOrder::tryFrom($byteOrder); - if ($byteOrder !== WKBTools::BIG_ENDIAN && $byteOrder !== WKBTools::LITTLE_ENDIAN) { + if ($wkbByteOrder === null) { throw GeometryIOException::invalidWKB('unknown byte order: ' . $byteOrder); } - $this->invert = ($byteOrder !== $this->machineByteOrder); + $this->invert = ($wkbByteOrder !== $this->machineByteOrder); } public function rewind(int $bytes) : void diff --git a/src/IO/WKBByteOrder.php b/src/IO/WKBByteOrder.php new file mode 100644 index 0000000..04bb5bd --- /dev/null +++ b/src/IO/WKBByteOrder.php @@ -0,0 +1,11 @@ + self::BIG_ENDIAN, - 'dcba' => self::LITTLE_ENDIAN, + 'abcd' => WKBByteOrder::BIG_ENDIAN, + 'dcba' => WKBByteOrder::LITTLE_ENDIAN, default => throw GeometryIOException::unsupportedEndianness(), }; } diff --git a/tests/GeometryTest.php b/tests/GeometryTest.php index c878704..cbfbdba 100644 --- a/tests/GeometryTest.php +++ b/tests/GeometryTest.php @@ -5,6 +5,7 @@ namespace Brick\Geo\Tests; use Brick\Geo\Exception\UnexpectedGeometryException; +use Brick\Geo\IO\WKBByteOrder; use Brick\Geo\IO\WKBTools; use Brick\Geo\Geometry; use Brick\Geo\Point; @@ -71,11 +72,10 @@ public function testAsBinary(string $text, string $bigEndianBinary, string $litt { $machineByteOrder = WKBTools::getMachineByteOrder(); - if ($machineByteOrder === WKBTools::BIG_ENDIAN) { - $binary = $bigEndianBinary; - } else { - $binary = $littleEndianBinary; - } + $binary = match ($machineByteOrder) { + WKBByteOrder::BIG_ENDIAN => $bigEndianBinary, + WKBByteOrder::LITTLE_ENDIAN => $littleEndianBinary, + }; self::assertSame($binary, bin2hex(Geometry::fromText($text)->asBinary())); } diff --git a/tests/IO/EWKBWriterTest.php b/tests/IO/EWKBWriterTest.php index 202b2d1..0e389fa 100644 --- a/tests/IO/EWKBWriterTest.php +++ b/tests/IO/EWKBWriterTest.php @@ -5,9 +5,9 @@ namespace Brick\Geo\Tests\IO; use Brick\Geo\Exception\GeometryIOException; -use Brick\Geo\IO\EWKBTools; use Brick\Geo\IO\EWKBWriter; use Brick\Geo\IO\EWKTReader; +use Brick\Geo\IO\WKBByteOrder; use Brick\Geo\Point; /** @@ -18,11 +18,11 @@ class EWKBWriterTest extends EWKBAbstractTest /** * @dataProvider providerWrite * - * @param string $ewkt The EWKT to read. - * @param string $ewkb The expected EWKB output, hex-encoded. - * @param int $byteOrder The byte order to use. + * @param string $ewkt The EWKT to read. + * @param string $ewkb The expected EWKB output, hex-encoded. + * @param WKBByteOrder $byteOrder The byte order to use. */ - public function testWrite(string $ewkt, string $ewkb, int $byteOrder) : void + public function testWrite(string $ewkt, string $ewkb, WKBByteOrder $byteOrder) : void { $writer = new EWKBWriter(); $writer->setByteOrder($byteOrder); @@ -38,19 +38,19 @@ public function testWrite(string $ewkt, string $ewkb, int $byteOrder) : void public function providerWrite() : \Generator { foreach ($this->providerLittleEndianEWKB() as [$wkt, $ewkb]) { - yield [$wkt, $ewkb, EWKBTools::LITTLE_ENDIAN]; + yield [$wkt, $ewkb, WKBByteOrder::LITTLE_ENDIAN]; } foreach ($this->providerLittleEndianEWKB_SRID() as [$wkt, $ewkb]) { - yield [$wkt, $ewkb, EWKBTools::LITTLE_ENDIAN]; + yield [$wkt, $ewkb, WKBByteOrder::LITTLE_ENDIAN]; } foreach ($this->providerBigEndianEWKB() as [$wkt, $ewkb]) { - yield [$wkt, $ewkb, EWKBTools::BIG_ENDIAN]; + yield [$wkt, $ewkb, WKBByteOrder::BIG_ENDIAN]; } foreach ($this->providerBigEndianEWKB_SRID() as [$wkt, $ewkb]) { - yield [$wkt, $ewkb, EWKBTools::BIG_ENDIAN]; + yield [$wkt, $ewkb, WKBByteOrder::BIG_ENDIAN]; } } diff --git a/tests/IO/WKBWriterTest.php b/tests/IO/WKBWriterTest.php index 57a2525..e93865c 100644 --- a/tests/IO/WKBWriterTest.php +++ b/tests/IO/WKBWriterTest.php @@ -5,7 +5,7 @@ namespace Brick\Geo\Tests\IO; use Brick\Geo\Exception\GeometryIOException; -use Brick\Geo\IO\WKBTools; +use Brick\Geo\IO\WKBByteOrder; use Brick\Geo\IO\WKBWriter; use Brick\Geo\IO\WKTReader; use Brick\Geo\Point; @@ -18,11 +18,11 @@ class WKBWriterTest extends WKBAbstractTest /** * @dataProvider providerWrite * - * @param string $wkt The WKT to read. - * @param string $wkb The expected WKB output, hex-encoded. - * @param int $byteOrder The byte order to use. + * @param string $wkt The WKT to read. + * @param string $wkb The expected WKB output, hex-encoded. + * @param WKBByteOrder $byteOrder The byte order to use. */ - public function testWrite(string $wkt, string $wkb, int $byteOrder) : void + public function testWrite(string $wkt, string $wkb, WKBByteOrder $byteOrder) : void { $writer = new WKBWriter(); $writer->setByteOrder($byteOrder); @@ -38,11 +38,11 @@ public function testWrite(string $wkt, string $wkb, int $byteOrder) : void public function providerWrite() : \Generator { foreach ($this->providerLittleEndianWKB() as [$wkt, $wkb]) { - yield [$wkt, $wkb, WKBTools::LITTLE_ENDIAN]; + yield [$wkt, $wkb, WKBByteOrder::LITTLE_ENDIAN]; } foreach ($this->providerBigEndianWKB() as [$wkt, $wkb]) { - yield [$wkt, $wkb, WKBTools::BIG_ENDIAN]; + yield [$wkt, $wkb, WKBByteOrder::BIG_ENDIAN]; } }