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     * 
@@ -28,13 +42,7 @@ class GeometryCollection extends Geometry implements IteratorAggregate, ArrayAcc
2842     */ 
2943    public  function  __construct (array  $ geometries )
3044    {
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 );
3846
3947        $ this  ->items  = $ geometries ;
4048    }
@@ -58,6 +66,10 @@ public function __toString()
5866
5967    public  static  function  fromString ($ wktArgument )
6068    {
69+         if  (empty ($ wktArgument )) {
70+             return  new  static ([]);
71+         }
72+ 
6173        $ geometry_strings  = preg_split ('/,\s*(?=[A-Za-z])/ ' , $ wktArgument );
6274
6375        return  new  static (array_map (function  ($ geometry_string ) {
@@ -89,9 +101,7 @@ public function offsetGet($offset)
89101
90102    public  function  offsetSet ($ offset , $ value )
91103    {
92-         if  (!($ value  instanceof  GeometryInterface)) {
93-             throw  new  InvalidArgumentException ('$value must be an instance of GeometryInterface ' );
94-         }
104+         $ this  ->validateItemType ($ value );
95105
96106        if  (is_null ($ offset )) {
97107            $ this  ->items [] = $ value ;
@@ -142,4 +152,57 @@ public function jsonSerialize()
142152
143153        return  new  \GeoJson \Geometry \GeometryCollection ($ geometries );
144154    }
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+     }
145208}
0 commit comments