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
*
@@ -31,13 +45,7 @@ public function __construct(array $geometries, $srid = 0)
31
45
{
32
46
parent ::__construct ($ srid );
33
47
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 );
41
49
42
50
$ this ->items = $ geometries ;
43
51
}
@@ -61,6 +69,10 @@ public function __toString()
61
69
62
70
public static function fromString ($ wktArgument , $ srid = 0 )
63
71
{
72
+ if (empty ($ wktArgument )) {
73
+ return new static ([]);
74
+ }
75
+
64
76
$ geometry_strings = preg_split ('/,\s*(?=[A-Za-z])/ ' , $ wktArgument );
65
77
66
78
return new static (array_map (function ($ geometry_string ) {
@@ -92,9 +104,7 @@ public function offsetGet($offset)
92
104
93
105
public function offsetSet ($ offset , $ value )
94
106
{
95
- if (!($ value instanceof GeometryInterface)) {
96
- throw new InvalidArgumentException ('$value must be an instance of GeometryInterface ' );
97
- }
107
+ $ this ->validateItemType ($ value );
98
108
99
109
if (is_null ($ offset )) {
100
110
$ this ->items [] = $ value ;
@@ -145,4 +155,57 @@ public function jsonSerialize()
145
155
146
156
return new \GeoJson \Geometry \GeometryCollection ($ geometries );
147
157
}
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
+ }
148
211
}
0 commit comments