22
33namespace Grimzy \LaravelMysqlSpatial \Types ;
44
5+ use ArrayAccess ;
6+ use ArrayIterator ;
57use Countable ;
8+ use Illuminate \Contracts \Support \Arrayable ;
69use 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,52 @@ 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 );
108+ }
109+
110+ public function toJson ($ options = 0 )
111+ {
112+ return json_encode ($ this , $ options );
64113 }
65114
66115 /**
@@ -71,7 +120,7 @@ public function count()
71120 public function jsonSerialize ()
72121 {
73122 $ geometries = [];
74- foreach ($ this ->geometries as $ geometry ) {
123+ foreach ($ this ->items as $ geometry ) {
75124 $ geometries [] = $ geometry ->jsonSerialize ();
76125 }
77126
0 commit comments