Skip to content

Commit 22744ef

Browse files
committed
turns ShapeType into an Enum
1 parent f825d8f commit 22744ef

File tree

4 files changed

+71
-66
lines changed

4 files changed

+71
-66
lines changed

src/ShapeFile.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ public static function supportsDbase(): bool
8585
}
8686

8787
/**
88-
* @param int $shapeType File shape type, should be same as all records
88+
* @param ShapeType $shapeType File shape type, should be same as all records
8989
* @param mixed[] $boundingBox File bounding box
9090
* @param string|null $fileName File name
9191
*/
9292
public function __construct(
93-
public int $shapeType,
93+
public ShapeType $shapeType,
9494
public array $boundingBox = [
9595
'xmin' => 0.0,
9696
'ymin' => 0.0,
@@ -374,7 +374,7 @@ private function loadHeaders(): bool
374374
$this->readSHP(4);
375375

376376
$shapeType = Util::loadData('V', $this->readSHP(4));
377-
$this->shapeType = $shapeType === false ? -1 : (int) $shapeType;
377+
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::NULL;
378378

379379
$this->boundingBox = [];
380380
$this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8));
@@ -435,7 +435,7 @@ private function saveHeaders(): void
435435
fwrite($this->shpFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0));
436436
fwrite($this->shpFile, pack('N', $this->fileLength));
437437
fwrite($this->shpFile, pack('V', 1000));
438-
fwrite($this->shpFile, pack('V', $this->shapeType));
438+
fwrite($this->shpFile, pack('V', $this->shapeType->value));
439439
$this->saveBBox($this->shpFile);
440440

441441
if ($this->shxFile === false) {
@@ -445,7 +445,7 @@ private function saveHeaders(): void
445445
fwrite($this->shxFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0));
446446
fwrite($this->shxFile, pack('N', 50 + 4 * count($this->records)));
447447
fwrite($this->shxFile, pack('V', 1000));
448-
fwrite($this->shxFile, pack('V', $this->shapeType));
448+
fwrite($this->shxFile, pack('V', $this->shapeType->value));
449449
$this->saveBBox($this->shxFile);
450450
}
451451

@@ -456,15 +456,15 @@ private function loadRecords(): bool
456456
{
457457
/* Need to start at offset 100 */
458458
while (! $this->eofSHP()) {
459-
$record = new ShapeRecord(-1);
459+
$record = new ShapeRecord(ShapeType::NULL);
460460
$record->loadFromFile($this, $this->dbfFile);
461461
if ($record->lastError !== '') {
462462
$this->setError($record->lastError);
463463

464464
return false;
465465
}
466466

467-
if (($record->shapeType === -1) && $this->eofSHP()) {
467+
if (($record->shapeType === ShapeType::NULL) && $this->eofSHP()) {
468468
break;
469469
}
470470

src/ShapeRecord.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
use function in_array;
3232
use function is_array;
3333
use function pack;
34-
use function sprintf;
3534
use function strlen;
3635

3736
/**
@@ -58,7 +57,7 @@ class ShapeRecord
5857
/** @var mixed[] */
5958
public array $dbfData = [];
6059

61-
public function __construct(public int $shapeType)
60+
public function __construct(public ShapeType $shapeType)
6261
{
6362
}
6463

@@ -92,7 +91,7 @@ public function loadFromFile(ShapeFile $shapeFile, $dbfFile): void
9291
ShapeType::MULTI_POINT => $this->loadMultiPointRecord(),
9392
ShapeType::MULTI_POINT_M => $this->loadMultiPointMRecord(),
9493
ShapeType::MULTI_POINT_Z => $this->loadMultiPointZRecord(),
95-
default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)),
94+
default => $this->reportInvalidShapeTypeError(),
9695
};
9796

9897
/* We need to skip rest of the record */
@@ -102,7 +101,7 @@ public function loadFromFile(ShapeFile $shapeFile, $dbfFile): void
102101

103102
/* Check if we didn't read too much */
104103
if ($this->read !== $this->size) {
105-
$this->setError(sprintf('Failed to parse record, read=%d, size=%d', $this->read, $this->size));
104+
$this->reportInvalidShapeTypeError();
106105
}
107106

108107
if (! ShapeFile::supportsDbase() || $dbfFile === false) {
@@ -139,7 +138,7 @@ public function saveToFile($shpFile, $dbfFile, int $recordNumber): void
139138
ShapeType::MULTI_POINT => $this->saveMultiPointRecord(),
140139
ShapeType::MULTI_POINT_M => $this->saveMultiPointMRecord(),
141140
ShapeType::MULTI_POINT_Z => $this->saveMultiPointZRecord(),
142-
default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)),
141+
default => $this->reportInvalidShapeTypeError(),
143142
};
144143

145144
if (! ShapeFile::supportsDbase() || $dbfFile === false) {
@@ -186,7 +185,7 @@ private function loadData(string $type, int $count): mixed
186185
*/
187186
private function loadHeaders(): void
188187
{
189-
$this->shapeType = -1;
188+
$this->shapeType = ShapeType::NULL;
190189
$recordNumber = $this->loadData('N', 4);
191190
if ($recordNumber === false) {
192191
return;
@@ -207,7 +206,7 @@ private function loadHeaders(): void
207206
return;
208207
}
209208

210-
$this->shapeType = (int) $shapeType;
209+
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::NULL;
211210
}
212211

213212
/**
@@ -217,7 +216,7 @@ private function saveHeaders(): void
217216
{
218217
fwrite($this->shpFile, pack('N', $this->recordNumber));
219218
fwrite($this->shpFile, pack('N', $this->getContentLength()));
220-
fwrite($this->shpFile, pack('V', $this->shapeType));
219+
fwrite($this->shpFile, pack('V', $this->shapeType->value));
221220
}
222221

223222
/** @return mixed[] */
@@ -634,7 +633,7 @@ public function addPoint(array $point, int $partIndex = 0): void
634633
$this->shpData['numpoints'] = 1 + ($this->shpData['numpoints'] ?? 0);
635634
break;
636635
default:
637-
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
636+
$this->reportInvalidShapeTypeError();
638637

639638
return;
640639
}
@@ -711,7 +710,7 @@ public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void
711710

712711
break;
713712
default:
714-
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
713+
$this->reportInvalidShapeTypeError();
715714
break;
716715
}
717716
}
@@ -774,7 +773,7 @@ public function getContentLength(): int|null
774773
break;
775774
default:
776775
$result = null;
777-
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
776+
$this->reportInvalidShapeTypeError();
778777
break;
779778
}
780779

@@ -822,4 +821,9 @@ public function getShapeName(): string
822821
{
823822
return ShapeType::name($this->shapeType);
824823
}
824+
825+
private function reportInvalidShapeTypeError(): void
826+
{
827+
$this->setError('Invalid Shape type.');
828+
}
825829
}

src/ShapeType.php

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

55
namespace PhpMyAdmin\ShapeFile;
66

7-
final class ShapeType
7+
enum ShapeType: int
88
{
9-
public const NULL = 0;
9+
case NULL = 0;
1010

11-
public const POINT = 1;
11+
case POINT = 1;
1212

13-
public const POLY_LINE = 3;
13+
case POLY_LINE = 3;
1414

15-
public const POLYGON = 5;
15+
case POLYGON = 5;
1616

17-
public const MULTI_POINT = 8;
17+
case MULTI_POINT = 8;
1818

19-
public const POINT_Z = 11;
19+
case POINT_Z = 11;
2020

21-
public const POLY_LINE_Z = 13;
21+
case POLY_LINE_Z = 13;
2222

23-
public const POLYGON_Z = 15;
23+
case POLYGON_Z = 15;
2424

25-
public const MULTI_POINT_Z = 18;
25+
case MULTI_POINT_Z = 18;
2626

27-
public const POINT_M = 21;
27+
case POINT_M = 21;
2828

29-
public const POLY_LINE_M = 23;
29+
case POLY_LINE_M = 23;
3030

31-
public const POLYGON_M = 25;
31+
case POLYGON_M = 25;
3232

33-
public const MULTI_POINT_M = 28;
33+
case MULTI_POINT_M = 28;
3434

35-
public const MULTI_PATCH = 31;
35+
case MULTI_PATCH = 31;
3636

3737
/** Shape types with a Z coordinate. */
3838
public const TYPES_WITH_Z = [self::POINT_Z, self::POLY_LINE_Z, self::POLYGON_Z, self::MULTI_POINT_Z];
@@ -50,25 +50,25 @@ final class ShapeType
5050
];
5151

5252
public const NAMES = [
53-
self::NULL => 'Null Shape',
54-
self::POINT => 'Point',
55-
self::POLY_LINE => 'PolyLine',
56-
self::POLYGON => 'Polygon',
57-
self::MULTI_POINT => 'MultiPoint',
58-
self::POINT_Z => 'PointZ',
59-
self::POLY_LINE_Z => 'PolyLineZ',
60-
self::POLYGON_Z => 'PolygonZ',
61-
self::MULTI_POINT_Z => 'MultiPointZ',
62-
self::POINT_M => 'PointM',
63-
self::POLY_LINE_M => 'PolyLineM',
64-
self::POLYGON_M => 'PolygonM',
65-
self::MULTI_POINT_M => 'MultiPointM',
66-
self::MULTI_PATCH => 'MultiPatch',
53+
self::NULL->value => 'Null Shape',
54+
self::POINT->value => 'Point',
55+
self::POLY_LINE->value => 'PolyLine',
56+
self::POLYGON->value => 'Polygon',
57+
self::MULTI_POINT->value => 'MultiPoint',
58+
self::POINT_Z->value => 'PointZ',
59+
self::POLY_LINE_Z->value => 'PolyLineZ',
60+
self::POLYGON_Z->value => 'PolygonZ',
61+
self::MULTI_POINT_Z->value => 'MultiPointZ',
62+
self::POINT_M->value => 'PointM',
63+
self::POLY_LINE_M->value => 'PolyLineM',
64+
self::POLYGON_M->value => 'PolygonM',
65+
self::MULTI_POINT_M->value => 'MultiPointM',
66+
self::MULTI_PATCH->value => 'MultiPatch',
6767
];
6868

6969
/** @psalm-return non-empty-string */
70-
public static function name(int $shapeType): string
70+
public static function name(ShapeType $shapeType): string
7171
{
72-
return self::NAMES[$shapeType] ?? 'Shape ' . $shapeType;
72+
return self::NAMES[$shapeType->value];
7373
}
7474
}

tests/ShapeFileTest.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ public function testShapeName(): void
303303
$this->assertEquals('Point', $obj->getShapeName());
304304
$obj = new ShapeFile(ShapeType::POINT);
305305
$this->assertEquals('Point', $obj->getShapeName());
306-
$obj = new ShapeRecord(-1);
307-
$this->assertEquals('Shape -1', $obj->getShapeName());
306+
$obj = new ShapeRecord(ShapeType::NULL);
307+
$this->assertEquals('Null Shape', $obj->getShapeName());
308308
}
309309

310310
/**
@@ -316,11 +316,12 @@ public function testShapeName(): void
316316
*/
317317
public function testShapeSaveLoad(int $shapeType, array $points): void
318318
{
319+
$shapeEnum = ShapeType::tryFrom($shapeType) ?? ShapeType::NULL;
319320
$filename = './data/test_shape-' . $shapeType . '.*';
320-
$shp = new ShapeFile($shapeType);
321+
$shp = new ShapeFile($shapeEnum);
321322
$shp->setDBFHeader([['ID', 'N', 19, 0], ['DESC', 'C', 14, 0]]);
322323

323-
$record0 = new ShapeRecord($shapeType);
324+
$record0 = new ShapeRecord($shapeEnum);
324325

325326
foreach ($points as $point) {
326327
$record0->addPoint($point[0], $point[1]);
@@ -330,7 +331,7 @@ public function testShapeSaveLoad(int $shapeType, array $points): void
330331

331332
$shp->saveToFile($filename);
332333

333-
$shp2 = new ShapeFile($shapeType);
334+
$shp2 = new ShapeFile($shapeEnum);
334335
$shp2->loadFromFile($filename);
335336

336337
$this->assertEquals(count($shp->records), count($shp2->records));
@@ -383,18 +384,18 @@ public static function shapesProvider(): array
383384
];
384385

385386
return [
386-
[ShapeType::POINT, $pointsForPointType],
387-
[ShapeType::POLY_LINE, $pointsForPolyLineType],
388-
[ShapeType::POLYGON, $pointsForPolygonType],
389-
[ShapeType::MULTI_POINT, $pointsForMultiPointType],
390-
[ShapeType::POINT_Z, $pointsForPointType],
391-
[ShapeType::POLY_LINE_Z, $pointsForPolyLineType],
392-
[ShapeType::POLYGON_Z, $pointsForPolygonType],
393-
[ShapeType::MULTI_POINT_Z, $pointsForMultiPointType],
394-
[ShapeType::POINT_M, $pointsForPointType],
395-
[ShapeType::POLY_LINE_M, $pointsForPolyLineType],
396-
[ShapeType::POLYGON_M, $pointsForPolygonType],
397-
[ShapeType::MULTI_POINT_M, $pointsForMultiPointType],
387+
[ShapeType::POINT->value, $pointsForPointType],
388+
[ShapeType::POLY_LINE->value, $pointsForPolyLineType],
389+
[ShapeType::POLYGON->value, $pointsForPolygonType],
390+
[ShapeType::MULTI_POINT->value, $pointsForMultiPointType],
391+
[ShapeType::POINT_Z->value, $pointsForPointType],
392+
[ShapeType::POLY_LINE_Z->value, $pointsForPolyLineType],
393+
[ShapeType::POLYGON_Z->value, $pointsForPolygonType],
394+
[ShapeType::MULTI_POINT_Z->value, $pointsForMultiPointType],
395+
[ShapeType::POINT_M->value, $pointsForPointType],
396+
[ShapeType::POLY_LINE_M->value, $pointsForPolyLineType],
397+
[ShapeType::POLYGON_M->value, $pointsForPolygonType],
398+
[ShapeType::MULTI_POINT_M->value, $pointsForMultiPointType],
398399
];
399400
}
400401

0 commit comments

Comments
 (0)