14
14
15
15
class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAccess, Arrayable, Countable
16
16
{
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
+
17
31
/**
18
32
* The items contained in the spatial collection.
19
33
*
@@ -28,13 +42,7 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc
28
42
*/
29
43
public function __construct (array $ geometries )
30
44
{
31
- $ validated = array_filter ($ geometries , function ($ value ) {
32
- return $ value instanceof GeometryInterface;
33
- });
34
-
35
- if (count ($ geometries ) !== count ($ validated )) {
36
- throw new InvalidArgumentException ('$geometries must be an array of Geometry objects ' );
37
- }
45
+ $ this ->validateItems ($ geometries );
38
46
39
47
$ this ->items = $ geometries ;
40
48
}
@@ -58,6 +66,10 @@ public function __toString()
58
66
59
67
public static function fromString ($ wktArgument )
60
68
{
69
+ if (empty ($ wktArgument )) {
70
+ return new static ([]);
71
+ }
72
+
61
73
$ geometry_strings = preg_split ('/,\s*(?=[A-Za-z])/ ' , $ wktArgument );
62
74
63
75
return new static (array_map (function ($ geometry_string ) {
@@ -89,9 +101,7 @@ public function offsetGet($offset)
89
101
90
102
public function offsetSet ($ offset , $ value )
91
103
{
92
- if (!($ value instanceof GeometryInterface)) {
93
- throw new InvalidArgumentException ('$value must be an instance of GeometryInterface ' );
94
- }
104
+ $ this ->validateItemType ($ value );
95
105
96
106
if (is_null ($ offset )) {
97
107
$ this ->items [] = $ value ;
@@ -142,4 +152,57 @@ public function jsonSerialize()
142
152
143
153
return new \GeoJson \Geometry \GeometryCollection ($ geometries );
144
154
}
155
+
156
+ /**
157
+ * Checks whether the items are valid to create this collection.
158
+ *
159
+ * @param array $items
160
+ */
161
+ protected function validateItems (array $ items )
162
+ {
163
+ $ this ->validateItemCount ($ items );
164
+
165
+ foreach ($ items as $ item ) {
166
+ $ this ->validateItemType ($ item );
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Checks whether the array has enough items to generate a valid WKT.
172
+ *
173
+ * @param array $items
174
+ *
175
+ * @see $minimumCollectionItems
176
+ */
177
+ protected function validateItemCount (array $ items )
178
+ {
179
+ if (count ($ items ) < $ this ->minimumCollectionItems ) {
180
+ $ entries = $ this ->minimumCollectionItems === 1 ? 'entry ' : 'entries ' ;
181
+
182
+ throw new InvalidArgumentException (sprintf (
183
+ '%s must contain at least %d %s ' ,
184
+ get_class ($ this ),
185
+ $ this ->minimumCollectionItems ,
186
+ $ entries
187
+ ));
188
+ }
189
+ }
190
+
191
+ /**
192
+ * Checks the type of the items in the array.
193
+ *
194
+ * @param $item
195
+ *
196
+ * @see $collectionItemType
197
+ */
198
+ protected function validateItemType ($ item )
199
+ {
200
+ if (!$ item instanceof $ this ->collectionItemType ) {
201
+ throw new InvalidArgumentException (sprintf (
202
+ '%s must be a collection of %s ' ,
203
+ get_class ($ this ),
204
+ $ this ->collectionItemType
205
+ ));
206
+ }
207
+ }
145
208
}
0 commit comments