From 9c42aa320210a72b8991f03e00b59e9f61a67d92 Mon Sep 17 00:00:00 2001 From: Julien Dephix Date: Wed, 11 Dec 2024 10:28:38 +0100 Subject: [PATCH] add test add a max number of records to load fixes #42 --- src/ShapeFile.php | 12 +++++++++++- tests/ShapeFileTest.php | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ShapeFile.php b/src/ShapeFile.php index a780140..a2857dc 100644 --- a/src/ShapeFile.php +++ b/src/ShapeFile.php @@ -83,6 +83,8 @@ class ShapeFile private bool $allowNoDbf = false; + private bool|int $maxRecords = false; + /** * Checks whether dbase manipulations are supported. */ @@ -108,6 +110,11 @@ public function __construct( ) { } + public function setMaxRecords(int|bool $max): void + { + $this->maxRecords = $max; + } + public function setAllowNoDbf(bool $allowNoDbf): void { $this->allowNoDbf = $allowNoDbf; @@ -490,8 +497,11 @@ private function loadRecords(): bool if (($record->shapeType === ShapeType::Unknown) && $this->eofSHP()) { break; } - $this->records[] = $record; + + if ($this->maxRecords !== false && count($this->records) > $this->maxRecords) { + return true; + } } return true; diff --git a/tests/ShapeFileTest.php b/tests/ShapeFileTest.php index bce1fe2..7b4f385 100644 --- a/tests/ShapeFileTest.php +++ b/tests/ShapeFileTest.php @@ -429,4 +429,18 @@ public function testAllowsNoDbf(): void $shp->setAllowNoDbf(true); self::assertTrue($shp->loadFromFile('data/no-dbf.*')); } + + public function testMaxNumberRecords() + { + if (! ShapeFile::supportsDbase()) { + self::markTestSkipped('dbase extension missing'); + } + + $this->createTestData(); + + $shp = new ShapeFile(ShapeType::Point); + $shp->setMaxRecords(5); + $shp->loadFromFile('data/mexico.*'); + self::assertCount(5, $shp->records); + } }