Skip to content

Commit 1314606

Browse files
committed
Merge remote-tracking branch 'origin/master' into srid
# Conflicts: # composer.json # src/Types/GeometryCollection.php # src/Types/MultiLineString.php # src/Types/MultiPolygon.php # src/Types/PointCollection.php
2 parents db42f20 + 7f62564 commit 1314606

18 files changed

+226
-90
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class Place extends Model
140140
```php
141141
use Grimzy\LaravelMysqlSpatial\Types\Point;
142142
use Grimzy\LaravelMysqlSpatial\Types\Polygon;
143+
use Grimzy\LaravelMysqlSpatial\Types\LineString;
143144

144145
$place1 = new Place();
145146
$place1->name = 'Empire State Building';

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
"php": ">=5.5.9",
1919
"ext-pdo": "*",
2020
"ext-json": "*",
21-
"illuminate/database": "^5.2||^6.0",
21+
"illuminate/database": "^5.2|^6.0|^7.0",
2222
"geo-io/wkb-parser": "^1.0",
2323
"jmikola/geojson": "^1.0"
2424
},
2525
"require-dev": {
26-
"phpunit/phpunit": "~4.8||~5.7",
26+
"phpunit/phpunit": "~4.8|~5.7",
2727
"mockery/mockery": "^0.9.9",
28-
"laravel/laravel": "^5.2||^6.0",
28+
"laravel/laravel": "^5.2|^6.0|^7.0",
2929
"doctrine/dbal": "^2.5",
3030
"laravel/browser-kit-testing": "^2.0",
3131
"php-coveralls/php-coveralls": "^2.0"

src/Eloquent/SpatialTrait.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ protected function newBaseQueryBuilder()
7575
$connection = $this->getConnection();
7676

7777
return new BaseBuilder(
78-
$connection, $connection->getQueryGrammar(), $connection->getPostProcessor()
78+
$connection,
79+
$connection->getQueryGrammar(),
80+
$connection->getPostProcessor()
7981
);
8082
}
8183

@@ -102,7 +104,7 @@ public function setRawAttributes(array $attributes, $sync = false)
102104
$spatial_fields = $this->getSpatialFields();
103105

104106
foreach ($attributes as $attribute => &$value) {
105-
if (in_array($attribute, $spatial_fields) && is_string($value) && strlen($value) >= 15) {
107+
if (in_array($attribute, $spatial_fields) && is_string($value) && strlen($value) >= 13) {
106108
$value = Geometry::fromWKB($value);
107109
}
108110
}

src/Types/GeometryCollection.php

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414

1515
class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable
1616
{
17+
/**
18+
* The minimum number of items required to create this collection.
19+
*
20+
* @var int
21+
*/
22+
protected $minimumCollectionItems = 0;
23+
24+
/**
25+
* The class of the items in the collection.
26+
*
27+
* @var string
28+
*/
29+
protected $collectionItemType = GeometryInterface::class;
30+
1731
/**
1832
* The items contained in the spatial collection.
1933
*
@@ -31,13 +45,7 @@ public function __construct(array $geometries, $srid = 0)
3145
{
3246
parent::__construct($srid);
3347

34-
$validated = array_filter($geometries, function ($value) {
35-
return $value instanceof GeometryInterface;
36-
});
37-
38-
if (count($geometries) !== count($validated)) {
39-
throw new InvalidArgumentException('$geometries must be an array of Geometry objects');
40-
}
48+
$this->validateItems($geometries);
4149

4250
$this->items = $geometries;
4351
}
@@ -61,6 +69,10 @@ public function __toString()
6169

6270
public static function fromString($wktArgument, $srid = 0)
6371
{
72+
if (empty($wktArgument)) {
73+
return new static([]);
74+
}
75+
6476
$geometry_strings = preg_split('/,\s*(?=[A-Za-z])/', $wktArgument);
6577

6678
return new static(array_map(function ($geometry_string) {
@@ -92,9 +104,7 @@ public function offsetGet($offset)
92104

93105
public function offsetSet($offset, $value)
94106
{
95-
if (!($value instanceof GeometryInterface)) {
96-
throw new InvalidArgumentException('$value must be an instance of GeometryInterface');
97-
}
107+
$this->validateItemType($value);
98108

99109
if (is_null($offset)) {
100110
$this->items[] = $value;
@@ -145,4 +155,57 @@ public function jsonSerialize()
145155

146156
return new \GeoJson\Geometry\GeometryCollection($geometries);
147157
}
158+
159+
/**
160+
* Checks whether the items are valid to create this collection.
161+
*
162+
* @param array $items
163+
*/
164+
protected function validateItems(array $items)
165+
{
166+
$this->validateItemCount($items);
167+
168+
foreach ($items as $item) {
169+
$this->validateItemType($item);
170+
}
171+
}
172+
173+
/**
174+
* Checks whether the array has enough items to generate a valid WKT.
175+
*
176+
* @param array $items
177+
*
178+
* @see $minimumCollectionItems
179+
*/
180+
protected function validateItemCount(array $items)
181+
{
182+
if (count($items) < $this->minimumCollectionItems) {
183+
$entries = $this->minimumCollectionItems === 1 ? 'entry' : 'entries';
184+
185+
throw new InvalidArgumentException(sprintf(
186+
'%s must contain at least %d %s',
187+
get_class($this),
188+
$this->minimumCollectionItems,
189+
$entries
190+
));
191+
}
192+
}
193+
194+
/**
195+
* Checks the type of the items in the array.
196+
*
197+
* @param $item
198+
*
199+
* @see $collectionItemType
200+
*/
201+
protected function validateItemType($item)
202+
{
203+
if (!$item instanceof $this->collectionItemType) {
204+
throw new InvalidArgumentException(sprintf(
205+
'%s must be a collection of %s',
206+
get_class($this),
207+
$this->collectionItemType
208+
));
209+
}
210+
}
148211
}

src/Types/LineString.php

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

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

src/Types/MultiLineString.php

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,22 @@
55
use GeoJson\GeoJson;
66
use GeoJson\Geometry\MultiLineString as GeoJsonMultiLineString;
77
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
8-
use InvalidArgumentException;
98

109
class MultiLineString extends GeometryCollection
1110
{
1211
/**
13-
* @param LineString[] $lineStrings
14-
* @param int $srid
12+
* The minimum number of items required to create this collection.
13+
*
14+
* @var int
1515
*/
16-
public function __construct(array $lineStrings, $srid = 0)
17-
{
18-
if (count($lineStrings) < 1) {
19-
throw new InvalidArgumentException('$lineStrings must contain at least one entry');
20-
}
21-
22-
$validated = array_filter($lineStrings, function ($value) {
23-
return $value instanceof LineString;
24-
});
25-
26-
if (count($lineStrings) !== count($validated)) {
27-
throw new InvalidArgumentException('$lineStrings must be an array of LineString');
28-
}
16+
protected $minimumCollectionItems = 1;
2917

30-
parent::__construct($lineStrings, $srid);
31-
}
18+
/**
19+
* The class of the items in the collection.
20+
*
21+
* @var string
22+
*/
23+
protected $collectionItemType = LineString::class;
3224

3325
public function getLineStrings()
3426
{
@@ -59,9 +51,7 @@ public function __toString()
5951

6052
public function offsetSet($offset, $value)
6153
{
62-
if (!($value instanceof LineString)) {
63-
throw new InvalidArgumentException('$value must be an instance of LineString');
64-
}
54+
$this->validateItemType($value);
6555

6656
parent::offsetSet($offset, $value);
6757
}

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: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,22 @@
55
use GeoJson\GeoJson;
66
use GeoJson\Geometry\MultiPolygon as GeoJsonMultiPolygon;
77
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
8-
use InvalidArgumentException;
98

109
class MultiPolygon extends GeometryCollection
1110
{
1211
/**
13-
* @param Polygon[] $polygons
14-
* @param int $srid
12+
* The minimum number of items required to create this collection.
13+
*
14+
* @var int
1515
*/
16-
public function __construct(array $polygons, $srid = 0)
17-
{
18-
$validated = array_filter($polygons, function ($value) {
19-
return $value instanceof Polygon;
20-
});
16+
protected $minimumCollectionItems = 1;
2117

22-
if (count($polygons) !== count($validated)) {
23-
throw new InvalidArgumentException('$polygons must be an array of Polygon');
24-
}
25-
parent::__construct($polygons, $srid);
26-
}
18+
/**
19+
* The class of the items in the collection.
20+
*
21+
* @var string
22+
*/
23+
protected $collectionItemType = Polygon::class;
2724

2825
public function toWKT()
2926
{
@@ -94,9 +91,7 @@ protected static function assembleParts(array $parts)
9491

9592
public function offsetSet($offset, $value)
9693
{
97-
if (!($value instanceof Polygon)) {
98-
throw new InvalidArgumentException('$value must be an instance of Polygon');
99-
}
94+
$this->validateItemType($value);
10095

10196
parent::offsetSet($offset, $value);
10297
}

src/Types/PointCollection.php

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,11 @@
88
abstract class PointCollection extends GeometryCollection
99
{
1010
/**
11-
* @param Point[] $points
12-
* @param int $srid
11+
* The class of the items in the collection.
12+
*
13+
* @var string
1314
*/
14-
public function __construct(array $points, $srid = 0)
15-
{
16-
if (count($points) < 2) {
17-
throw new InvalidArgumentException('$points must contain at least two entries');
18-
}
19-
20-
$validated = array_filter($points, function ($value) {
21-
return $value instanceof Point;
22-
});
23-
24-
if (count($points) !== count($validated)) {
25-
throw new InvalidArgumentException('$points must be an array of Points');
26-
}
27-
28-
parent::__construct($points, $srid);
29-
}
15+
protected $collectionItemType = Point::class;
3016

3117
public function toPairList()
3218
{
@@ -37,9 +23,7 @@ public function toPairList()
3723

3824
public function offsetSet($offset, $value)
3925
{
40-
if (!($value instanceof Point)) {
41-
throw new InvalidArgumentException('$value must be an instance of Point');
42-
}
26+
$this->validateItemType($value);
4327

4428
parent::offsetSet($offset, $value);
4529
}

tests/Integration/Models/GeometryModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ class GeometryModel extends Model
1717

1818
protected $table = 'geometry';
1919

20-
protected $spatialFields = ['location', 'line'];
20+
protected $spatialFields = ['location', 'line', 'multi_geometries'];
2121
}

0 commit comments

Comments
 (0)