From d302fee0be972fc9c1f3c37078efd7477bb77495 Mon Sep 17 00:00:00 2001 From: Julien Dephix Date: Fri, 27 Sep 2024 14:44:17 +0200 Subject: [PATCH] fixes #29 turn ShapeType into an Enum Signed-off-by: Julien Dephix --- examples/create_shapefile.php | 8 +- examples/read.php | 2 +- phpstan-baseline.neon | 3 +- src/ShapeFile.php | 18 ++-- src/ShapeRecord.php | 160 +++++++++++++++++----------------- src/ShapeType.php | 83 +++++++++--------- tests/ShapeFileTest.php | 74 ++++++++-------- 7 files changed, 180 insertions(+), 168 deletions(-) diff --git a/examples/create_shapefile.php b/examples/create_shapefile.php index f3ec850..e20dd86 100644 --- a/examples/create_shapefile.php +++ b/examples/create_shapefile.php @@ -29,26 +29,26 @@ require_once __DIR__ . '/../vendor/autoload.php'; -$shp = new ShapeFile(ShapeType::POINT, [ +$shp = new ShapeFile(ShapeType::Point, [ 'xmin' => 464079.002268, 'ymin' => 2120153.74792, 'xmax' => 505213.52849, 'ymax' => 2163205.70036, ]); -$record0 = new ShapeRecord(ShapeType::POINT); +$record0 = new ShapeRecord(ShapeType::Point); $record0->addPoint([ 'x' => 482131.764567, 'y' => 2143634.39608, ]); -$record1 = new ShapeRecord(ShapeType::POINT); +$record1 = new ShapeRecord(ShapeType::Point); $record1->addPoint([ 'x' => 472131.764567, 'y' => 2143634.39608, ]); -$record2 = new ShapeRecord(ShapeType::POINT); +$record2 = new ShapeRecord(ShapeType::Point); $record2->addPoint([ 'x' => 492131.764567, 'y' => 2143634.39608, diff --git a/examples/read.php b/examples/read.php index 38d02f1..4571a6f 100644 --- a/examples/read.php +++ b/examples/read.php @@ -34,7 +34,7 @@ // phpcs:ignore Squiz.Functions.GlobalFunction.Found function display_file(string $filename): void { - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile($filename); $i = 1; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 19a9a22..139959d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -127,7 +127,7 @@ parameters: - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" - count: 17 + count: 18 path: tests/ShapeFileTest.php - @@ -159,4 +159,3 @@ parameters: message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" count: 1 path: tests/UtilTest.php - diff --git a/src/ShapeFile.php b/src/ShapeFile.php index 37e313d..851e398 100644 --- a/src/ShapeFile.php +++ b/src/ShapeFile.php @@ -85,12 +85,12 @@ public static function supportsDbase(): bool } /** - * @param int $shapeType File shape type, should be same as all records + * @param ShapeType $shapeType File shape type, should be same as all records * @param mixed[] $boundingBox File bounding box * @param string|null $fileName File name */ public function __construct( - public int $shapeType, + public ShapeType $shapeType, public array $boundingBox = [ 'xmin' => 0.0, 'ymin' => 0.0, @@ -374,7 +374,11 @@ private function loadHeaders(): bool $this->readSHP(4); $shapeType = Util::loadData('V', $this->readSHP(4)); - $this->shapeType = $shapeType === false ? -1 : (int) $shapeType; + if ($shapeType === false) { + $this->shapeType = ShapeType::Unknown; + } else { + $this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown; + } $this->boundingBox = []; $this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8)); @@ -435,7 +439,7 @@ private function saveHeaders(): void fwrite($this->shpFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0)); fwrite($this->shpFile, pack('N', $this->fileLength)); fwrite($this->shpFile, pack('V', 1000)); - fwrite($this->shpFile, pack('V', $this->shapeType)); + fwrite($this->shpFile, pack('V', $this->shapeType->value)); $this->saveBBox($this->shpFile); if ($this->shxFile === false) { @@ -445,7 +449,7 @@ private function saveHeaders(): void fwrite($this->shxFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0)); fwrite($this->shxFile, pack('N', 50 + 4 * count($this->records))); fwrite($this->shxFile, pack('V', 1000)); - fwrite($this->shxFile, pack('V', $this->shapeType)); + fwrite($this->shxFile, pack('V', $this->shapeType->value)); $this->saveBBox($this->shxFile); } @@ -456,7 +460,7 @@ private function loadRecords(): bool { /* Need to start at offset 100 */ while (! $this->eofSHP()) { - $record = new ShapeRecord(-1); + $record = new ShapeRecord(ShapeType::Unknown); $record->loadFromFile($this, $this->dbfFile); if ($record->lastError !== '') { $this->setError($record->lastError); @@ -464,7 +468,7 @@ private function loadRecords(): bool return false; } - if (($record->shapeType === -1) && $this->eofSHP()) { + if (($record->shapeType === ShapeType::Unknown) && $this->eofSHP()) { break; } diff --git a/src/ShapeRecord.php b/src/ShapeRecord.php index 2c0e80b..8c289be 100644 --- a/src/ShapeRecord.php +++ b/src/ShapeRecord.php @@ -31,7 +31,6 @@ use function in_array; use function is_array; use function pack; -use function sprintf; use function strlen; /** @@ -58,7 +57,7 @@ class ShapeRecord /** @var mixed[] */ public array $dbfData = []; - public function __construct(public int $shapeType) + public function __construct(public ShapeType $shapeType) { } @@ -79,20 +78,20 @@ public function loadFromFile(ShapeFile $shapeFile, $dbfFile): void } match ($this->shapeType) { - ShapeType::NULL => $this->loadNullRecord(), - ShapeType::POINT => $this->loadPointRecord(), - ShapeType::POINT_M => $this->loadPointMRecord(), - ShapeType::POINT_Z => $this->loadPointZRecord(), - ShapeType::POLY_LINE => $this->loadPolyLineRecord(), - ShapeType::POLY_LINE_M => $this->loadPolyLineMRecord(), - ShapeType::POLY_LINE_Z => $this->loadPolyLineZRecord(), - ShapeType::POLYGON => $this->loadPolygonRecord(), - ShapeType::POLYGON_M => $this->loadPolygonMRecord(), - ShapeType::POLYGON_Z => $this->loadPolygonZRecord(), - ShapeType::MULTI_POINT => $this->loadMultiPointRecord(), - ShapeType::MULTI_POINT_M => $this->loadMultiPointMRecord(), - ShapeType::MULTI_POINT_Z => $this->loadMultiPointZRecord(), - default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)), + ShapeType::Null => $this->loadNullRecord(), + ShapeType::Point => $this->loadPointRecord(), + ShapeType::PointM => $this->loadPointMRecord(), + ShapeType::PointZ => $this->loadPointZRecord(), + ShapeType::PolyLine => $this->loadPolyLineRecord(), + ShapeType::PolyLineM => $this->loadPolyLineMRecord(), + ShapeType::PolyLineZ => $this->loadPolyLineZRecord(), + ShapeType::Polygon => $this->loadPolygonRecord(), + ShapeType::PolygonM => $this->loadPolygonMRecord(), + ShapeType::PolygonZ => $this->loadPolygonZRecord(), + ShapeType::MultiPoint => $this->loadMultiPointRecord(), + ShapeType::MultiPointM => $this->loadMultiPointMRecord(), + ShapeType::MultiPointZ => $this->loadMultiPointZRecord(), + default => $this->reportInvalidShapeTypeError(), }; /* We need to skip rest of the record */ @@ -102,7 +101,7 @@ public function loadFromFile(ShapeFile $shapeFile, $dbfFile): void /* Check if we didn't read too much */ if ($this->read !== $this->size) { - $this->setError(sprintf('Failed to parse record, read=%d, size=%d', $this->read, $this->size)); + $this->reportInvalidShapeTypeError(); } if (! ShapeFile::supportsDbase() || $dbfFile === false) { @@ -126,20 +125,20 @@ public function saveToFile($shpFile, $dbfFile, int $recordNumber): void $this->saveHeaders(); match ($this->shapeType) { - ShapeType::NULL => null, // Nothing to save - ShapeType::POINT => $this->savePointRecord(), - ShapeType::POINT_M => $this->savePointMRecord(), - ShapeType::POINT_Z => $this->savePointZRecord(), - ShapeType::POLY_LINE => $this->savePolyLineRecord(), - ShapeType::POLY_LINE_M => $this->savePolyLineMRecord(), - ShapeType::POLY_LINE_Z => $this->savePolyLineZRecord(), - ShapeType::POLYGON => $this->savePolygonRecord(), - ShapeType::POLYGON_M => $this->savePolygonMRecord(), - ShapeType::POLYGON_Z => $this->savePolygonZRecord(), - ShapeType::MULTI_POINT => $this->saveMultiPointRecord(), - ShapeType::MULTI_POINT_M => $this->saveMultiPointMRecord(), - ShapeType::MULTI_POINT_Z => $this->saveMultiPointZRecord(), - default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)), + ShapeType::Null => null, // Nothing to save + ShapeType::Point => $this->savePointRecord(), + ShapeType::PointM => $this->savePointMRecord(), + ShapeType::PointZ => $this->savePointZRecord(), + ShapeType::PolyLine => $this->savePolyLineRecord(), + ShapeType::PolyLineM => $this->savePolyLineMRecord(), + ShapeType::PolyLineZ => $this->savePolyLineZRecord(), + ShapeType::Polygon => $this->savePolygonRecord(), + ShapeType::PolygonM => $this->savePolygonMRecord(), + ShapeType::PolygonZ => $this->savePolygonZRecord(), + ShapeType::MultiPoint => $this->saveMultiPointRecord(), + ShapeType::MultiPointM => $this->saveMultiPointMRecord(), + ShapeType::MultiPointZ => $this->saveMultiPointZRecord(), + default => $this->reportInvalidShapeTypeError(), }; if (! ShapeFile::supportsDbase() || $dbfFile === false) { @@ -186,7 +185,7 @@ private function loadData(string $type, int $count): mixed */ private function loadHeaders(): void { - $this->shapeType = -1; + $this->shapeType = ShapeType::Unknown; $recordNumber = $this->loadData('N', 4); if ($recordNumber === false) { return; @@ -207,7 +206,7 @@ private function loadHeaders(): void return; } - $this->shapeType = (int) $shapeType; + $this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown; } /** @@ -217,7 +216,7 @@ private function saveHeaders(): void { fwrite($this->shpFile, pack('N', $this->recordNumber)); fwrite($this->shpFile, pack('N', $this->getContentLength())); - fwrite($this->shpFile, pack('V', $this->shapeType)); + fwrite($this->shpFile, pack('V', $this->shapeType->value)); } /** @return mixed[] */ @@ -605,36 +604,36 @@ public function addPoint(array $point, int $partIndex = 0): void { $point = $this->adjustPoint($point); switch ($this->shapeType) { - case ShapeType::NULL: + case ShapeType::Null: //Don't add anything return; - case ShapeType::POINT: - case ShapeType::POINT_Z: - case ShapeType::POINT_M: + case ShapeType::Point: + case ShapeType::PointZ: + case ShapeType::PointM: //Substitutes the value of the current point $this->shpData = $point; break; - case ShapeType::POLY_LINE: - case ShapeType::POLYGON: - case ShapeType::POLY_LINE_Z: - case ShapeType::POLYGON_Z: - case ShapeType::POLY_LINE_M: - case ShapeType::POLYGON_M: + case ShapeType::PolyLine: + case ShapeType::Polygon: + case ShapeType::PolyLineZ: + case ShapeType::PolygonZ: + case ShapeType::PolyLineM: + case ShapeType::PolygonM: //Adds a new point to the selected part $this->shpData['parts'][$partIndex]['points'][] = $point; $this->shpData['numparts'] = count($this->shpData['parts']); $this->shpData['numpoints'] = 1 + ($this->shpData['numpoints'] ?? 0); break; - case ShapeType::MULTI_POINT: - case ShapeType::MULTI_POINT_Z: - case ShapeType::MULTI_POINT_M: + case ShapeType::MultiPoint: + case ShapeType::MultiPointZ: + case ShapeType::MultiPointM: //Adds a new point $this->shpData['points'][] = $point; $this->shpData['numpoints'] = 1 + ($this->shpData['numpoints'] ?? 0); break; default: - $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)); + $this->reportInvalidShapeTypeError(); return; } @@ -651,30 +650,30 @@ public function addPoint(array $point, int $partIndex = 0): void public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void { switch ($this->shapeType) { - case ShapeType::NULL: + case ShapeType::Null: //Don't delete anything break; - case ShapeType::POINT: - case ShapeType::POINT_Z: - case ShapeType::POINT_M: + case ShapeType::Point: + case ShapeType::PointZ: + case ShapeType::PointM: //Sets the value of the point to zero $this->shpData['x'] = 0.0; $this->shpData['y'] = 0.0; - if (in_array($this->shapeType, [ShapeType::POINT_Z, ShapeType::POINT_M], true)) { + if (in_array($this->shapeType, [ShapeType::PointZ, ShapeType::PointM], true)) { $this->shpData['m'] = 0.0; } - if ($this->shapeType === ShapeType::POINT_Z) { + if ($this->shapeType === ShapeType::PointZ) { $this->shpData['z'] = 0.0; } break; - case ShapeType::POLY_LINE: - case ShapeType::POLYGON: - case ShapeType::POLY_LINE_Z: - case ShapeType::POLYGON_Z: - case ShapeType::POLY_LINE_M: - case ShapeType::POLYGON_M: + case ShapeType::PolyLine: + case ShapeType::Polygon: + case ShapeType::PolyLineZ: + case ShapeType::PolygonZ: + case ShapeType::PolyLineM: + case ShapeType::PolygonM: //Deletes the point from the selected part, if exists if ( isset($this->shpData['parts'][$partIndex]) @@ -694,9 +693,9 @@ public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void } break; - case ShapeType::MULTI_POINT: - case ShapeType::MULTI_POINT_Z: - case ShapeType::MULTI_POINT_M: + case ShapeType::MultiPoint: + case ShapeType::MultiPointZ: + case ShapeType::MultiPointM: //Deletes the point, if exists if (isset($this->shpData['points'][$pointIndex])) { $count = count($this->shpData['points']) - 1; @@ -711,7 +710,7 @@ public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void break; default: - $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)); + $this->reportInvalidShapeTypeError(); break; } } @@ -724,20 +723,20 @@ public function getContentLength(): int|null // The content length for a record is the length of the record contents section measured in 16-bit words. // one coordinate makes 4 16-bit words (64 bit double) switch ($this->shapeType) { - case ShapeType::NULL: + case ShapeType::Null: $result = 0; break; - case ShapeType::POINT: + case ShapeType::Point: $result = 10; break; - case ShapeType::POINT_M: + case ShapeType::PointM: $result = 10 + 4; break; - case ShapeType::POINT_Z: + case ShapeType::PointZ: $result = 10 + 8; break; - case ShapeType::POLY_LINE: - case ShapeType::POLYGON: + case ShapeType::PolyLine: + case ShapeType::Polygon: $count = count($this->shpData['parts']); $result = 22 + 2 * $count; for ($i = 0; $i < $count; ++$i) { @@ -745,8 +744,8 @@ public function getContentLength(): int|null } break; - case ShapeType::POLY_LINE_M: - case ShapeType::POLYGON_M: + case ShapeType::PolyLineM: + case ShapeType::PolygonM: $count = count($this->shpData['parts']); $result = 22 + (2 * 4) + 2 * $count; for ($i = 0; $i < $count; ++$i) { @@ -754,8 +753,8 @@ public function getContentLength(): int|null } break; - case ShapeType::POLY_LINE_Z: - case ShapeType::POLYGON_Z: + case ShapeType::PolyLineZ: + case ShapeType::PolygonZ: $count = count($this->shpData['parts']); $result = 22 + (4 * 4) + 2 * $count; for ($i = 0; $i < $count; ++$i) { @@ -763,18 +762,18 @@ public function getContentLength(): int|null } break; - case ShapeType::MULTI_POINT: + case ShapeType::MultiPoint: $result = 20 + 8 * count($this->shpData['points']); break; - case ShapeType::MULTI_POINT_M: + case ShapeType::MultiPointM: $result = 20 + (2 * 4) + (8 + 4) * count($this->shpData['points']); break; - case ShapeType::MULTI_POINT_Z: + case ShapeType::MultiPointZ: $result = 20 + (4 * 4) + (8 + 8) * count($this->shpData['points']); break; default: $result = null; - $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)); + $this->reportInvalidShapeTypeError(); break; } @@ -822,4 +821,9 @@ public function getShapeName(): string { return ShapeType::name($this->shapeType); } + + private function reportInvalidShapeTypeError(): void + { + $this->setError('Invalid Shape type.'); + } } diff --git a/src/ShapeType.php b/src/ShapeType.php index 505d7d0..0e809cd 100644 --- a/src/ShapeType.php +++ b/src/ShapeType.php @@ -4,71 +4,74 @@ namespace PhpMyAdmin\ShapeFile; -final class ShapeType +enum ShapeType: int { - public const NULL = 0; + case Null = 0; - public const POINT = 1; + case Point = 1; - public const POLY_LINE = 3; + case PolyLine = 3; - public const POLYGON = 5; + case Polygon = 5; - public const MULTI_POINT = 8; + case MultiPoint = 8; - public const POINT_Z = 11; + case PointZ = 11; - public const POLY_LINE_Z = 13; + case PolyLineZ = 13; - public const POLYGON_Z = 15; + case PolygonZ = 15; - public const MULTI_POINT_Z = 18; + case MultiPointZ = 18; - public const POINT_M = 21; + case PointM = 21; - public const POLY_LINE_M = 23; + case PolyLineM = 23; - public const POLYGON_M = 25; + case PolygonM = 25; - public const MULTI_POINT_M = 28; + case MultiPointM = 28; - public const MULTI_PATCH = 31; + case MultiPatch = 31; + + case Unknown = -1; /** Shape types with a Z coordinate. */ - public const TYPES_WITH_Z = [self::POINT_Z, self::POLY_LINE_Z, self::POLYGON_Z, self::MULTI_POINT_Z]; + public const TYPES_WITH_Z = [self::PointZ, self::PolyLineZ, self::PolygonZ, self::MultiPointZ]; /** Shape types with a measure field. */ public const MEASURED_TYPES = [ - self::POINT_Z, - self::POLY_LINE_Z, - self::POLYGON_Z, - self::MULTI_POINT_Z, - self::POINT_M, - self::POLY_LINE_M, - self::POLYGON_M, - self::MULTI_POINT_M, + self::PointZ, + self::PolyLineZ, + self::PolygonZ, + self::MultiPointZ, + self::PointM, + self::PolyLineM, + self::PolygonM, + self::MultiPointM, ]; public const NAMES = [ - self::NULL => 'Null Shape', - self::POINT => 'Point', - self::POLY_LINE => 'PolyLine', - self::POLYGON => 'Polygon', - self::MULTI_POINT => 'MultiPoint', - self::POINT_Z => 'PointZ', - self::POLY_LINE_Z => 'PolyLineZ', - self::POLYGON_Z => 'PolygonZ', - self::MULTI_POINT_Z => 'MultiPointZ', - self::POINT_M => 'PointM', - self::POLY_LINE_M => 'PolyLineM', - self::POLYGON_M => 'PolygonM', - self::MULTI_POINT_M => 'MultiPointM', - self::MULTI_PATCH => 'MultiPatch', + self::Unknown->value => 'Unknown Shape', + self::Null->value => 'Null Shape', + self::Point->value => 'Point', + self::PolyLine->value => 'PolyLine', + self::Polygon->value => 'Polygon', + self::MultiPoint->value => 'MultiPoint', + self::PointZ->value => 'PointZ', + self::PolyLineZ->value => 'PolyLineZ', + self::PolygonZ->value => 'PolygonZ', + self::MultiPointZ->value => 'MultiPointZ', + self::PointM->value => 'PointM', + self::PolyLineM->value => 'PolyLineM', + self::PolygonM->value => 'PolygonM', + self::MultiPointM->value => 'MultiPointM', + self::MultiPatch->value => 'MultiPatch', ]; /** @psalm-return non-empty-string */ - public static function name(int $shapeType): string + public static function name(ShapeType $shapeType): string { - return self::NAMES[$shapeType] ?? 'Shape ' . $shapeType; + return self::NAMES[$shapeType->value]; } } diff --git a/tests/ShapeFileTest.php b/tests/ShapeFileTest.php index 64b9a5c..6878503 100644 --- a/tests/ShapeFileTest.php +++ b/tests/ShapeFileTest.php @@ -45,7 +45,7 @@ class ShapeFileTest extends TestCase */ public function testLoad(string $filename, int $records, int|null $parts): void { - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile($filename); $this->assertEquals('', $shp->lastError); $this->assertEquals($records, count($shp->records)); @@ -106,7 +106,7 @@ public static function provideFiles(): array */ public function testLoadError(string $filename): void { - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile($filename); $this->assertNotEquals('', $shp->lastError); } @@ -116,7 +116,7 @@ public function testLoadError(string $filename): void */ public function testLoadEmptyFilename(): void { - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile(''); if (ShapeFile::supportsDbase()) { $this->assertEquals('It wasn\'t possible to find the DBase file ""', $shp->lastError); @@ -132,7 +132,7 @@ public function testLoadEmptyFilename(): void */ public function testGetDBFHeader(): void { - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $this->assertNull($shp->getDBFHeader()); } @@ -162,18 +162,18 @@ public static function provideErrorFiles(): array */ private function createTestData(): void { - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); - $record0 = new ShapeRecord(ShapeType::POINT); + $record0 = new ShapeRecord(ShapeType::Point); $record0->addPoint(['x' => 482131.764567, 'y' => 2143634.39608]); - $record1 = new ShapeRecord(ShapeType::POINT_Z); + $record1 = new ShapeRecord(ShapeType::PointZ); $record1->addPoint(['x' => 472131.764567, 'y' => 2143634.39608, 'z' => 220, 'm' => 120]); - $record2 = new ShapeRecord(ShapeType::POINT_M); + $record2 = new ShapeRecord(ShapeType::PointM); $record2->addPoint(['x' => 492131.764567, 'y' => 2143634.39608, 'z' => 150, 'm' => 80]); - $record3 = new ShapeRecord(ShapeType::POLY_LINE); + $record3 = new ShapeRecord(ShapeType::PolyLine); $record3->addPoint(['x' => 482131.764567, 'y' => 2143634.39608], 0); $record3->addPoint(['x' => 482132.764567, 'y' => 2143635.39608], 0); $record3->addPoint(['x' => 482131.764567, 'y' => 2143635.39608], 1); @@ -227,7 +227,7 @@ public function testCreate(): void $this->createTestData(); - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile('./data/test_shape.*'); $this->assertEquals(4, count($shp->records)); } @@ -243,13 +243,13 @@ public function testDelete(): void $this->createTestData(); - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile('./data/test_shape.*'); $shp->deleteRecord(1); $shp->saveToFile(); $this->assertEquals(3, count($shp->records)); - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile('./data/test_shape.*'); $this->assertEquals(3, count($shp->records)); } @@ -265,10 +265,10 @@ public function testAdd(): void $this->createTestData(); - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile('./data/test_shape.*'); - $record0 = new ShapeRecord(ShapeType::POINT); + $record0 = new ShapeRecord(ShapeType::Point); $record0->addPoint(['x' => 482131.764567, 'y' => 2143634.39608]); $shp->addRecord($record0); @@ -278,7 +278,7 @@ public function testAdd(): void $shp->saveToFile(); $this->assertEquals(5, count($shp->records)); - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->loadFromFile('./data/test_shape.*'); $this->assertEquals(5, count($shp->records)); } @@ -288,7 +288,7 @@ public function testAdd(): void */ public function testSaveNoDBF(): void { - $shp = new ShapeFile(ShapeType::POINT); + $shp = new ShapeFile(ShapeType::Point); $shp->saveToFile('./data/test_nodbf.*'); $this->assertFileDoesNotExist('./data/test_nodbf.dbf'); @@ -299,12 +299,14 @@ public function testSaveNoDBF(): void */ public function testShapeName(): void { - $obj = new ShapeRecord(ShapeType::POINT); + $obj = new ShapeRecord(ShapeType::Point); $this->assertEquals('Point', $obj->getShapeName()); - $obj = new ShapeFile(ShapeType::POINT); + $obj = new ShapeFile(ShapeType::Point); $this->assertEquals('Point', $obj->getShapeName()); - $obj = new ShapeRecord(-1); - $this->assertEquals('Shape -1', $obj->getShapeName()); + $obj = new ShapeRecord(ShapeType::Null); + $this->assertEquals('Null Shape', $obj->getShapeName()); + $obj = new ShapeRecord(ShapeType::Unknown); + $this->assertEquals('Unknown Shape', $obj->getShapeName()); } /** @@ -314,9 +316,9 @@ public function testShapeName(): void * * @dataProvider shapesProvider */ - public function testShapeSaveLoad(int $shapeType, array $points): void + public function testShapeSaveLoad(ShapeType $shapeType, array $points): void { - $filename = './data/test_shape-' . $shapeType . '.*'; + $filename = './data/test_shape-' . $shapeType->value . '.*'; $shp = new ShapeFile($shapeType); $shp->setDBFHeader([['ID', 'N', 19, 0], ['DESC', 'C', 14, 0]]); @@ -354,7 +356,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void /** * Data provider for save/load testing. * - * @psalm-return list}> + * @psalm-return list}> */ public static function shapesProvider(): array { @@ -383,24 +385,24 @@ public static function shapesProvider(): array ]; return [ - [ShapeType::POINT, $pointsForPointType], - [ShapeType::POLY_LINE, $pointsForPolyLineType], - [ShapeType::POLYGON, $pointsForPolygonType], - [ShapeType::MULTI_POINT, $pointsForMultiPointType], - [ShapeType::POINT_Z, $pointsForPointType], - [ShapeType::POLY_LINE_Z, $pointsForPolyLineType], - [ShapeType::POLYGON_Z, $pointsForPolygonType], - [ShapeType::MULTI_POINT_Z, $pointsForMultiPointType], - [ShapeType::POINT_M, $pointsForPointType], - [ShapeType::POLY_LINE_M, $pointsForPolyLineType], - [ShapeType::POLYGON_M, $pointsForPolygonType], - [ShapeType::MULTI_POINT_M, $pointsForMultiPointType], + [ShapeType::Point, $pointsForPointType], + [ShapeType::PolyLine, $pointsForPolyLineType], + [ShapeType::Polygon, $pointsForPolygonType], + [ShapeType::MultiPoint, $pointsForMultiPointType], + [ShapeType::PointZ, $pointsForPointType], + [ShapeType::PolyLineZ, $pointsForPolyLineType], + [ShapeType::PolygonZ, $pointsForPolygonType], + [ShapeType::MultiPointZ, $pointsForMultiPointType], + [ShapeType::PointM, $pointsForPointType], + [ShapeType::PolyLineM, $pointsForPolyLineType], + [ShapeType::PolygonM, $pointsForPolygonType], + [ShapeType::MultiPointM, $pointsForMultiPointType], ]; } public function testSearch(): void { - $shp = new ShapeFile(ShapeType::NULL); + $shp = new ShapeFile(ShapeType::Null); $shp->loadFromFile('data/capitals.*'); /* Nonexisting entry or no dbase support */ $this->assertEquals(