1414
1515class 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}
0 commit comments