Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #29 - Turn ShapeType into an enum #34

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/create_shapefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/read.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ parameters:

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

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

18 changes: 11 additions & 7 deletions src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@
}

/**
* @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,
Expand Down Expand Up @@ -374,7 +374,11 @@
$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));
Expand Down Expand Up @@ -435,7 +439,7 @@
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) {
Expand All @@ -445,7 +449,7 @@
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);
}

Expand All @@ -456,15 +460,15 @@
{
/* 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);

return false;
}

if (($record->shapeType === -1) && $this->eofSHP()) {
if (($record->shapeType === ShapeType::Unknown) && $this->eofSHP()) {
break;
}

Expand Down Expand Up @@ -650,7 +654,7 @@
*
* @param int<0, max> $bytes
*/
public function readSHP(int $bytes): string|false

Check failure on line 657 in src/ShapeFile.php

View workflow job for this annotation

GitHub Actions / lint-docs

int<min,max> has not the correct format on "PhpMyAdmin\ShapeFile\ShapeFile::readSHP"
{
if ($this->shpFile === false) {
return false;
Expand Down
Loading
Loading