Skip to content

Commit 4559e2c

Browse files
authored
Merge pull request #19 from grimzy/feature/collection
ArrayAccess, IteratorAggregate, Arrayable, Jsonable on Collections
2 parents 84e80bf + d602134 commit 4559e2c

13 files changed

+327
-207
lines changed

src/Types/Geometry.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use GeoIO\WKB\Parser\Parser;
66
use Grimzy\LaravelMysqlSpatial\Exceptions\UnknownWKTTypeException;
7+
use Illuminate\Contracts\Support\Jsonable;
78

8-
abstract class Geometry implements GeometryInterface, \JsonSerializable
9+
abstract class Geometry implements GeometryInterface, Jsonable, \JsonSerializable
910
{
1011
protected static $wkb_types = [
1112
1 => Point::class,
@@ -69,4 +70,9 @@ public static function fromWKT($wkt)
6970

7071
return static::fromString($wktArgument);
7172
}
73+
74+
public function toJson($options = 0)
75+
{
76+
return json_encode($this, $options);
77+
}
7278
}

src/Types/GeometryCollection.php

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
namespace Grimzy\LaravelMysqlSpatial\Types;
44

5+
use ArrayAccess;
6+
use ArrayIterator;
57
use Countable;
8+
use Illuminate\Contracts\Support\Arrayable;
69
use InvalidArgumentException;
10+
use IteratorAggregate;
711

8-
class GeometryCollection extends Geometry implements Countable
12+
class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable
913
{
1014
/**
15+
* The items contained in the spatial collection.
16+
*
1117
* @var GeometryInterface[]
1218
*/
13-
protected $geometries = [];
19+
protected $items = [];
1420

1521
/**
1622
* @param GeometryInterface[] $geometries
@@ -27,12 +33,12 @@ public function __construct(array $geometries)
2733
throw new InvalidArgumentException('$geometries must be an array of Geometry objects');
2834
}
2935

30-
$this->geometries = $geometries;
36+
$this->items = $geometries;
3137
}
3238

3339
public function getGeometries()
3440
{
35-
return $this->geometries;
41+
return $this->items;
3642
}
3743

3844
public function toWKT()
@@ -44,7 +50,7 @@ public function __toString()
4450
{
4551
return implode(',', array_map(function (GeometryInterface $geometry) {
4652
return $geometry->toWKT();
47-
}, $this->geometries));
53+
}, $this->items));
4854
}
4955

5056
public static function fromString($wktArgument)
@@ -58,9 +64,47 @@ public static function fromString($wktArgument)
5864
}, $geometry_strings));
5965
}
6066

67+
public function toArray()
68+
{
69+
return $this->items;
70+
}
71+
72+
public function getIterator()
73+
{
74+
return new ArrayIterator($this->items);
75+
}
76+
77+
public function offsetExists($offset)
78+
{
79+
return isset($this->items[$offset]);
80+
}
81+
82+
public function offsetGet($offset)
83+
{
84+
return $this->offsetExists($offset) ? $this->items[$offset] : null;
85+
}
86+
87+
public function offsetSet($offset, $value)
88+
{
89+
if (!($value instanceof GeometryInterface)) {
90+
throw new InvalidArgumentException('$value must be an instance of GeometryInterface');
91+
}
92+
93+
if (is_null($offset)) {
94+
$this->items[] = $value;
95+
} else {
96+
$this->items[$offset] = $value;
97+
}
98+
}
99+
100+
public function offsetUnset($offset)
101+
{
102+
unset($this->items[$offset]);
103+
}
104+
61105
public function count()
62106
{
63-
return count($this->geometries);
107+
return count($this->items);
64108
}
65109

66110
/**
@@ -71,7 +115,7 @@ public function count()
71115
public function jsonSerialize()
72116
{
73117
$geometries = [];
74-
foreach ($this->geometries as $geometry) {
118+
foreach ($this->items as $geometry) {
75119
$geometries[] = $geometry->jsonSerialize();
76120
}
77121

src/Types/LineString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Grimzy\LaravelMysqlSpatial\Types;
44

5-
class LineString extends PointCollection implements GeometryInterface
5+
class LineString extends PointCollection
66
{
77
public function toWKT()
88
{
@@ -39,7 +39,7 @@ public function __toString()
3939
public function jsonSerialize()
4040
{
4141
$points = [];
42-
foreach ($this->points as $point) {
42+
foreach ($this->items as $point) {
4343
$points[] = $point->jsonSerialize();
4444
}
4545

src/Types/MultiLineString.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace Grimzy\LaravelMysqlSpatial\Types;
44

5-
use Countable;
65
use InvalidArgumentException;
76

8-
class MultiLineString extends Geometry implements Countable
7+
class MultiLineString extends GeometryCollection
98
{
10-
/**
11-
* @var LineString[]
12-
*/
13-
protected $linestrings = [];
14-
159
/**
1610
* @param LineString[] $linestrings
1711
*/
@@ -29,12 +23,12 @@ public function __construct(array $linestrings)
2923
throw new InvalidArgumentException('$linestrings must be an array of LineString');
3024
}
3125

32-
$this->linestrings = $linestrings;
26+
parent::__construct($linestrings);
3327
}
3428

3529
public function getLineStrings()
3630
{
37-
return $this->linestrings;
31+
return $this->items;
3832
}
3933

4034
public function toWKT()
@@ -59,9 +53,13 @@ public function __toString()
5953
}, $this->getLineStrings()));
6054
}
6155

62-
public function count()
56+
public function offsetSet($offset, $value)
6357
{
64-
return count($this->linestrings);
58+
if (!($value instanceof LineString)) {
59+
throw new InvalidArgumentException('$value must be an instance of LineString');
60+
}
61+
62+
parent::offsetSet($offset, $value);
6563
}
6664

6765
/**
@@ -73,7 +71,7 @@ public function jsonSerialize()
7371
{
7472
$linestrings = [];
7573

76-
foreach ($this->linestrings as $linestring) {
74+
foreach ($this->items as $linestring) {
7775
$linestrings[] = $linestring->jsonSerialize();
7876
}
7977

src/Types/MultiPoint.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Grimzy\LaravelMysqlSpatial\Types;
44

5-
class MultiPoint extends PointCollection implements GeometryInterface, \JsonSerializable
5+
class MultiPoint extends PointCollection
66
{
77
public function toWKT()
88
{
@@ -32,7 +32,7 @@ public function __toString()
3232
{
3333
return implode(',', array_map(function (Point $point) {
3434
return sprintf('(%s)', $point->toPair());
35-
}, $this->points));
35+
}, $this->items));
3636
}
3737

3838
/**
@@ -43,7 +43,7 @@ public function __toString()
4343
public function jsonSerialize()
4444
{
4545
$points = [];
46-
foreach ($this->points as $point) {
46+
foreach ($this->items as $point) {
4747
$points[] = $point->jsonSerialize();
4848
}
4949

src/Types/MultiPolygon.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,10 @@
22

33
namespace Grimzy\LaravelMysqlSpatial\Types;
44

5-
use Countable;
65
use InvalidArgumentException;
76

8-
class MultiPolygon extends Geometry implements Countable
7+
class MultiPolygon extends GeometryCollection
98
{
10-
/**
11-
* @var Polygon[]
12-
*/
13-
protected $polygons;
14-
159
/**
1610
* @param Polygon[] $polygons
1711
*/
@@ -24,7 +18,7 @@ public function __construct(array $polygons)
2418
if (count($polygons) !== count($validated)) {
2519
throw new InvalidArgumentException('$polygons must be an array of Polygon');
2620
}
27-
$this->polygons = $polygons;
21+
parent::__construct($polygons);
2822
}
2923

3024
public function toWKT()
@@ -36,7 +30,7 @@ public function __toString()
3630
{
3731
return implode(',', array_map(function (Polygon $polygon) {
3832
return sprintf('(%s)', (string) $polygon);
39-
}, $this->polygons));
33+
}, $this->items));
4034
}
4135

4236
public static function fromString($wktArgument)
@@ -49,30 +43,14 @@ public static function fromString($wktArgument)
4943
}, $polygons));
5044
}
5145

52-
/**
53-
* (PHP 5 &gt;= 5.1.0)<br/>
54-
* Count elements of an object.
55-
*
56-
* @link http://php.net/manual/en/countable.count.php
57-
*
58-
* @return int The custom count as an integer.
59-
* </p>
60-
* <p>
61-
* The return value is cast to an integer.
62-
*/
63-
public function count()
64-
{
65-
return count($this->polygons);
66-
}
67-
6846
/**
6947
* Get the polygons that make up this MultiPolygon.
7048
*
7149
* @return array|Polygon[]
7250
*/
7351
public function getPolygons()
7452
{
75-
return $this->polygons;
53+
return $this->items;
7654
}
7755

7856
/**
@@ -110,6 +88,15 @@ protected static function assembleParts(array $parts)
11088
return $polygons;
11189
}
11290

91+
public function offsetSet($offset, $value)
92+
{
93+
if (!($value instanceof Polygon)) {
94+
throw new InvalidArgumentException('$value must be an instance of Polygon');
95+
}
96+
97+
parent::offsetSet($offset, $value);
98+
}
99+
113100
/**
114101
* Convert to GeoJson MultiPolygon that is jsonable to GeoJSON.
115102
*
@@ -118,7 +105,7 @@ protected static function assembleParts(array $parts)
118105
public function jsonSerialize()
119106
{
120107
$polygons = [];
121-
foreach ($this->polygons as $polygon) {
108+
foreach ($this->items as $polygon) {
122109
$polygons[] = $polygon->jsonSerialize();
123110
}
124111

0 commit comments

Comments
 (0)