Skip to content

Commit d302fee

Browse files
committed
fixes #29
turn ShapeType into an Enum Signed-off-by: Julien Dephix <[email protected]>
1 parent f825d8f commit d302fee

File tree

7 files changed

+180
-168
lines changed

7 files changed

+180
-168
lines changed

examples/create_shapefile.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,26 @@
2929

3030
require_once __DIR__ . '/../vendor/autoload.php';
3131

32-
$shp = new ShapeFile(ShapeType::POINT, [
32+
$shp = new ShapeFile(ShapeType::Point, [
3333
'xmin' => 464079.002268,
3434
'ymin' => 2120153.74792,
3535
'xmax' => 505213.52849,
3636
'ymax' => 2163205.70036,
3737
]);
3838

39-
$record0 = new ShapeRecord(ShapeType::POINT);
39+
$record0 = new ShapeRecord(ShapeType::Point);
4040
$record0->addPoint([
4141
'x' => 482131.764567,
4242
'y' => 2143634.39608,
4343
]);
4444

45-
$record1 = new ShapeRecord(ShapeType::POINT);
45+
$record1 = new ShapeRecord(ShapeType::Point);
4646
$record1->addPoint([
4747
'x' => 472131.764567,
4848
'y' => 2143634.39608,
4949
]);
5050

51-
$record2 = new ShapeRecord(ShapeType::POINT);
51+
$record2 = new ShapeRecord(ShapeType::Point);
5252
$record2->addPoint([
5353
'x' => 492131.764567,
5454
'y' => 2143634.39608,

examples/read.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// phpcs:ignore Squiz.Functions.GlobalFunction.Found
3535
function display_file(string $filename): void
3636
{
37-
$shp = new ShapeFile(ShapeType::POINT);
37+
$shp = new ShapeFile(ShapeType::Point);
3838
$shp->loadFromFile($filename);
3939

4040
$i = 1;

phpstan-baseline.neon

+1-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ parameters:
127127

128128
-
129129
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
130-
count: 17
130+
count: 18
131131
path: tests/ShapeFileTest.php
132132

133133
-
@@ -159,4 +159,3 @@ parameters:
159159
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
160160
count: 1
161161
path: tests/UtilTest.php
162-

src/ShapeFile.php

+11-7
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,11 @@ 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+
if ($shapeType === false) {
378+
$this->shapeType = ShapeType::Unknown;
379+
} else {
380+
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown;
381+
}
378382

379383
$this->boundingBox = [];
380384
$this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8));
@@ -435,7 +439,7 @@ private function saveHeaders(): void
435439
fwrite($this->shpFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0));
436440
fwrite($this->shpFile, pack('N', $this->fileLength));
437441
fwrite($this->shpFile, pack('V', 1000));
438-
fwrite($this->shpFile, pack('V', $this->shapeType));
442+
fwrite($this->shpFile, pack('V', $this->shapeType->value));
439443
$this->saveBBox($this->shpFile);
440444

441445
if ($this->shxFile === false) {
@@ -445,7 +449,7 @@ private function saveHeaders(): void
445449
fwrite($this->shxFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0));
446450
fwrite($this->shxFile, pack('N', 50 + 4 * count($this->records)));
447451
fwrite($this->shxFile, pack('V', 1000));
448-
fwrite($this->shxFile, pack('V', $this->shapeType));
452+
fwrite($this->shxFile, pack('V', $this->shapeType->value));
449453
$this->saveBBox($this->shxFile);
450454
}
451455

@@ -456,15 +460,15 @@ private function loadRecords(): bool
456460
{
457461
/* Need to start at offset 100 */
458462
while (! $this->eofSHP()) {
459-
$record = new ShapeRecord(-1);
463+
$record = new ShapeRecord(ShapeType::Unknown);
460464
$record->loadFromFile($this, $this->dbfFile);
461465
if ($record->lastError !== '') {
462466
$this->setError($record->lastError);
463467

464468
return false;
465469
}
466470

467-
if (($record->shapeType === -1) && $this->eofSHP()) {
471+
if (($record->shapeType === ShapeType::Unknown) && $this->eofSHP()) {
468472
break;
469473
}
470474

0 commit comments

Comments
 (0)