Skip to content

Commit

Permalink
adds a toggle to allow shape files with no dbf
Browse files Browse the repository at this point in the history
fixes #36
  • Loading branch information
JoolsMcFly committed Nov 19, 2024
1 parent 0705dd9 commit fa57fb1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Binary file modified data/no-dbf.shp
Binary file not shown.
12 changes: 12 additions & 0 deletions src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class ShapeFile
/** @var array */
public $records = [];

/** @var bool */
private $allowNoDbf = false;

/**
* Checks whether dbase manipulations are supported.
*/
Expand Down Expand Up @@ -121,6 +124,11 @@ public function __construct(
$this->fileLength = 50;
}

public function setAllowNoDbf(bool $allowNoDbf): void
{
$this->allowNoDbf = $allowNoDbf;
}

/**
* Loads shapefile and dbase (if supported).
*
Expand Down Expand Up @@ -620,6 +628,10 @@ private function openDBFFile(): bool

$dbfName = $this->getFilename('.dbf');
if (! is_readable($dbfName)) {
if ($this->allowNoDbf) {
return true;
}

$this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbfName));

return false;
Expand Down
11 changes: 11 additions & 0 deletions tests/ShapeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,15 @@ public function testSearch(): void
$shp->getIndexFromDBFData('CNTRY_NAME', 'Czech Republic')
);
}

public function testAllowsNoDbf(): void
{
if (! ShapeFile::supportsDbase()) {
self::markTestSkipped();
}

$shp = new ShapeFile(0);
$shp->setAllowNoDbf(true);
self::assertTrue($shp->loadFromFile('data/no-dbf.*'));
}
}

0 comments on commit fa57fb1

Please sign in to comment.