diff --git a/data/no-dbf.shp b/data/no-dbf.shp index e69de29..aa19d09 100644 Binary files a/data/no-dbf.shp and b/data/no-dbf.shp differ diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c228e92..33623e7 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -21,7 +21,6 @@ records[$index]->getContentLength()]]> - diff --git a/src/ShapeFile.php b/src/ShapeFile.php index 851e398..5f60463 100644 --- a/src/ShapeFile.php +++ b/src/ShapeFile.php @@ -27,6 +27,11 @@ use function chr; use function count; +use function dbase_close; +use function dbase_create; +use function dbase_delete_record; +use function dbase_open; +use function dbase_pack; use function extension_loaded; use function fclose; use function feof; @@ -76,6 +81,8 @@ class ShapeFile /** @var array */ public array $records = []; + private bool $allowNoDbf = false; + /** * Checks whether dbase manipulations are supported. */ @@ -101,6 +108,11 @@ public function __construct( ) { } + public function setAllowNoDbf(bool $allowNoDbf): void + { + $this->allowNoDbf = $allowNoDbf; + } + /** * Loads shapefile and dbase (if supported). * @@ -613,6 +625,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; diff --git a/src/ShapeRecord.php b/src/ShapeRecord.php index 8c289be..5d57b79 100644 --- a/src/ShapeRecord.php +++ b/src/ShapeRecord.php @@ -27,6 +27,10 @@ use function array_values; use function count; +use function dbase_add_record; +use function dbase_get_record_with_names; +use function dbase_numrecords; +use function dbase_replace_record; use function fwrite; use function in_array; use function is_array; diff --git a/tests/ShapeFileTest.php b/tests/ShapeFileTest.php index e75184d..bce1fe2 100644 --- a/tests/ShapeFileTest.php +++ b/tests/ShapeFileTest.php @@ -418,4 +418,15 @@ public function testSearch(): void $shp->getIndexFromDBFData('CNTRY_NAME', 'Czech Republic'), ); } + + public function testAllowsNoDbf(): void + { + if (! ShapeFile::supportsDbase()) { + self::markTestSkipped(); + } + + $shp = new ShapeFile(ShapeType::Null); + $shp->setAllowNoDbf(true); + self::assertTrue($shp->loadFromFile('data/no-dbf.*')); + } }