Skip to content

Commit 534dddc

Browse files
committed
Set $minimumCollectionItems to 0 for GeometryCollection and MultiPolygon. Adjusted child classes. Added/update tests.
1 parent 2d3139d commit 534dddc

File tree

7 files changed

+63
-3
lines changed

7 files changed

+63
-3
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MultiPolygon extends GeometryCollection
1313
*
1414
* @var int
1515
*/
16-
protected $minimumCollectionItems = 0;
16+
protected $minimumCollectionItems = 1;
1717

1818
/**
1919
* The class of the items in the collection.

tests/Integration/SpatialTest.php

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

245+
public function testInsertEmptyGeometryCollection()
246+
{
247+
$geo = new GeometryModel();
248+
249+
$geo->location = new Point(1, 2);
250+
251+
$geo->multi_geometries = new GeometryCollection([]);
252+
$geo->save();
253+
$this->assertDatabaseHas('geometry', ['id' => $geo->id]);
254+
}
255+
245256
public function testUpdate()
246257
{
247258
$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
@@ -75,7 +75,16 @@ public function testJsonSerialize()
7575
$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()));
7676
}
7777

78-
public function testInvalidArgumentExceptionNotArrayOfLineString()
78+
public function testInvalidArgumentExceptionAtLeastOneEntry()
79+
{
80+
$this->assertException(
81+
InvalidArgumentException::class,
82+
'Grimzy\LaravelMysqlSpatial\Types\MultiPolygon must contain at least 1 entry'
83+
);
84+
$multipolygon = new MultiPolygon([]);
85+
}
86+
87+
public function testInvalidArgumentExceptionNotArrayOfPolygon()
7988
{
8089
$this->assertException(
8190
InvalidArgumentException::class,

0 commit comments

Comments
 (0)