Skip to content

Commit 8439802

Browse files
committed
Set $minimumCollectionItems to 0 for GeometryCollection and MultiPolygon. Adjusted child classes. Added/update tests.
(cherry picked from commit 534dddc)
1 parent 26e6bde commit 8439802

File tree

7 files changed

+69
-2
lines changed

7 files changed

+69
-2
lines changed

src/Types/GeometryCollection.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc
1919
*
2020
* @var int
2121
*/
22-
protected $minimumCollectionItems = 1;
22+
protected $minimumCollectionItems = 0;
2323

2424
/**
2525
* The class of the items in the collection.
@@ -66,6 +66,10 @@ public function __toString()
6666

6767
public static function fromString($wktArgument)
6868
{
69+
if (empty($wktArgument)) {
70+
return new static([]);
71+
}
72+
6973
$geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument);
7074

7175
return new static(array_map(function ($geometry_string) {

src/Types/MultiLineString.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class MultiLineString extends GeometryCollection
1010
{
11+
/**
12+
* The minimum number of items required to create this collection.
13+
*
14+
* @var int
15+
*/
16+
protected $minimumCollectionItems = 1;
17+
1118
/**
1219
* The class of the items in the collection.
1320
*

src/Types/MultiPoint.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class MultiPoint extends PointCollection
1010
{
11+
/**
12+
* The minimum number of items required to create this collection.
13+
*
14+
* @var int
15+
*/
16+
protected $minimumCollectionItems = 1;
17+
1118
public function toWKT()
1219
{
1320
return sprintf('MULTIPOINT(%s)', (string) $this);

src/Types/MultiPolygon.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class MultiPolygon extends GeometryCollection
1010
{
11+
/**
12+
* The minimum number of items required to create this collection.
13+
*
14+
* @var int
15+
*/
16+
protected $minimumCollectionItems = 1;
17+
1118
/**
1219
* The class of the items in the collection.
1320
*

tests/Integration/SpatialTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ public function testInsertGeometryCollection()
175175
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
176176
}
177177

178+
public function testInsertEmptyGeometryCollection()
179+
{
180+
$geo = new GeometryModel();
181+
182+
$geo->location = new Point(1, 2);
183+
184+
$geo->multi_geometries = new GeometryCollection([]);
185+
$geo->save();
186+
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
187+
}
188+
178189
public function testUpdate()
179190
{
180191
$geo = new GeometryModel();

tests/Unit/Types/GeometryCollectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ public function testJsonSerialize()
3333
$this->assertSame('{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[0,0],[1,0],[1,1],[0,1],[0,0]]},{"type":"Point","coordinates":[200,100]}]}', json_encode($this->getGeometryCollection()->jsonSerialize()));
3434
}
3535

36+
public function testCanCreateEmptyGeometryCollection()
37+
{
38+
$geometryCollection = new GeometryCollection([]);
39+
$this->assertInstanceOf(GeometryCollection::class, $geometryCollection);
40+
}
41+
42+
public function testFromWKTWithEmptyGeometryCollection()
43+
{
44+
/**
45+
* @var GeometryCollection
46+
*/
47+
$geometryCollection = GeometryCollection::fromWKT('GEOMETRYCOLLECTION()');
48+
$this->assertInstanceOf(GeometryCollection::class, $geometryCollection);
49+
50+
$this->assertEquals(0, $geometryCollection->count());
51+
}
52+
53+
public function testToWKTWithEmptyGeometryCollection()
54+
{
55+
$this->assertEquals('GEOMETRYCOLLECTION()', (new GeometryCollection([]))->toWKT());
56+
}
57+
3658
public function testInvalidArgumentExceptionNotArrayGeometries()
3759
{
3860
$this->assertException(

tests/Unit/Types/MultiPolygonTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ public function testJsonSerialize()
6868
$this->assertSame('{"type":"MultiPolygon","coordinates":[[[[0,0],[1,0],[1,1],[0,1],[0,0]],[[10,10],[20,10],[20,20],[10,20],[10,10]]],[[[100,100],[200,100],[200,200],[100,200],[100,100]]]]}', json_encode($this->getMultiPolygon()));
6969
}
7070

71-
public function testInvalidArgumentExceptionNotArrayOfLineString()
71+
public function testInvalidArgumentExceptionAtLeastOneEntry()
72+
{
73+
$this->assertException(
74+
InvalidArgumentException::class,
75+
'Grimzy\LaravelMysqlSpatial\Types\MultiPolygon must contain at least 1 entry'
76+
);
77+
$multipolygon = new MultiPolygon([]);
78+
}
79+
80+
public function testInvalidArgumentExceptionNotArrayOfPolygon()
7281
{
7382
$this->assertException(
7483
InvalidArgumentException::class,

0 commit comments

Comments
 (0)