1111
1212namespace Symfony \UX \Map ;
1313
14+ use SplObjectStorage ;
1415use Symfony \UX \Map \Exception \InvalidArgumentException ;
1516
1617/**
@@ -29,19 +30,18 @@ public function __construct(
2930 /**
3031 * @var array<Marker>
3132 */
32- private array $ markers = [] ,
33+ private SplObjectStorage $ markers = new SplObjectStorage () ,
3334
3435 /**
3536 * @var array<Polygon>
3637 */
37- private array $ polygons = [] ,
38+ private SplObjectStorage $ polygons = new SplObjectStorage () ,
3839
3940 /**
4041 * @var array<Polyline>
4142 */
42- private array $ polylines = [],
43- ) {
44- }
43+ private SplObjectStorage $ polylines = new SplObjectStorage (),
44+ ) {}
4545
4646 public function getRendererName (): ?string
4747 {
@@ -86,23 +86,99 @@ public function hasOptions(): bool
8686 return null !== $ this ->options ;
8787 }
8888
89+
90+ public function getMarker (string $ identifier ): ?Marker
91+ {
92+ foreach ($ this ->markers as $ marker ) {
93+ if ($ this ->markers ->offsetGet ($ marker ) === $ identifier ) {
94+ return $ marker ;
95+ }
96+ }
97+
98+ return null ;
99+ }
100+
89101 public function addMarker (Marker $ marker ): self
90102 {
91- $ this ->markers [] = $ marker ;
103+ $ this ->markers ->attach ($ marker , $ marker ->identifier ?? $ this ->markers ->getHash ($ marker ));
104+
105+
106+ return $ this ;
107+ }
108+
109+ public function removeMarker (?Marker $ marker ): self
110+ {
111+ if ($ marker === null ) {
112+ return $ this ;
113+ }
114+
115+ if ($ this ->markers ->contains ($ marker )) {
116+ $ this ->markers ->detach ($ marker );
117+ }
92118
93119 return $ this ;
94120 }
95121
122+
123+ public function getPolygon (string $ identifier ): ?Polygon
124+ {
125+ foreach ($ this ->polygons as $ polygon ) {
126+ if ($ this ->polygons ->offsetGet ($ polygon ) === $ identifier ) {
127+ return $ polygon ;
128+ }
129+ }
130+
131+ return null ;
132+ }
133+
96134 public function addPolygon (Polygon $ polygon ): self
97135 {
98- $ this ->polygons [] = $ polygon ;
136+ $ this ->polygons -> attach ( $ polygon , $ polygon -> identifier ?? $ this -> polygons -> getHash ( $ polygon)) ;
99137
100138 return $ this ;
101139 }
102140
141+ public function removePolygon (?Polygon $ polygon ): self
142+ {
143+ if ($ polygon === null ) {
144+ return $ this ;
145+ }
146+
147+ if ($ this ->polygons ->contains ($ polygon )) {
148+ $ this ->polygons ->detach ($ polygon );
149+ }
150+
151+ return $ this ;
152+ }
153+
154+
155+ public function getPolyline (string $ identifier ): ?Polyline
156+ {
157+ foreach ($ this ->polylines as $ polyline ) {
158+ if ($ this ->polylines ->offsetGet ($ polyline ) === $ identifier ) {
159+ return $ polyline ;
160+ }
161+ }
162+
163+ return null ;
164+ }
165+
103166 public function addPolyline (Polyline $ polyline ): self
104167 {
105- $ this ->polylines [] = $ polyline ;
168+ $ this ->polylines ->attach ($ polyline , $ polyline ->identifier ?? $ this ->polylines ->getHash ($ polyline ));
169+
170+ return $ this ;
171+ }
172+
173+ public function removePolyline (?Polyline $ polyline ): self
174+ {
175+ if ($ polyline === null ) {
176+ return $ this ;
177+ }
178+
179+ if ($ this ->polylines ->contains ($ polyline )) {
180+ $ this ->polylines ->detach ($ polyline );
181+ }
106182
107183 return $ this ;
108184 }
@@ -124,12 +200,73 @@ public function toArray(): array
124200 'zoom ' => $ this ->zoom ,
125201 'fitBoundsToMarkers ' => $ this ->fitBoundsToMarkers ,
126202 'options ' => $ this ->options ? MapOptionsNormalizer::normalize ($ this ->options ) : [],
127- 'markers ' => array_map ( static fn ( Marker $ marker ) => $ marker -> toArray (), $ this ->markers ),
128- 'polygons ' => array_map ( static fn ( Polygon $ polygon ) => $ polygon -> toArray (), $ this ->polygons ),
129- 'polylines ' => array_map ( static fn ( Polyline $ polyline ) => $ polyline -> toArray (), $ this ->polylines ),
203+ 'markers ' => $ this ->markersToArray ( ),
204+ 'polygons ' => $ this ->polygonsToArray ( ),
205+ 'polylines ' => $ this ->polylinesToArray ( ),
130206 ];
131207 }
132208
209+ private function markersToArray (): array
210+ {
211+ foreach ($ this ->markers as $ marker ) {
212+ $ markers [] = $ marker ->toArray ();
213+ }
214+
215+ return $ markers ?? [];
216+ }
217+
218+ private static function markersFromArray (array $ markers ): SplObjectStorage
219+ {
220+ $ markerObjects = new SplObjectStorage ();
221+ foreach ($ markers as $ marker ) {
222+ $ markerObject = Marker::fromArray ($ marker );
223+ $ markerObjects ->attach ($ markerObject , $ markerObject ->identifier );
224+ }
225+
226+ return $ markerObjects ;
227+ }
228+
229+ private function polygonsToArray (): array
230+ {
231+ foreach ($ this ->polygons as $ polygon ) {
232+ $ polygons [] = $ polygon ->toArray ();
233+ }
234+
235+ return $ polygons ?? [];
236+ }
237+
238+ private static function polygonsFromArray (array $ polygons ): SplObjectStorage
239+ {
240+ $ polygonObjects = new SplObjectStorage ();
241+ foreach ($ polygons as $ polygon ) {
242+ $ polygonObject = Polygon::fromArray ($ polygon );
243+ $ polygonObjects ->attach ($ polygonObject , $ polygonObject ->identifier );
244+ }
245+
246+ return $ polygonObjects ;
247+ }
248+
249+ private function polylinesToArray (): array
250+ {
251+ foreach ($ this ->polylines as $ polyline ) {
252+ $ polylines [] = $ polyline ->toArray ();
253+ }
254+
255+ return $ polylines ?? [];
256+ }
257+
258+ private static function polylinesFromArray (array $ polylines ): SplObjectStorage
259+ {
260+ $ polylineObjects = new SplObjectStorage ();
261+ foreach ($ polylines as $ polyline ) {
262+ $ polylineObject = Polyline::fromArray ($ polyline );
263+ $ polylineObjects ->attach ($ polylineObject , $ polylineObject ->identifier );
264+ }
265+
266+ return $ polylineObjects ;
267+ }
268+
269+
133270 /**
134271 * @param array{
135272 * center?: array{lat: float, lng: float},
@@ -159,23 +296,23 @@ public static function fromArray(array $map): self
159296 $ map ['fitBoundsToMarkers ' ] = false ;
160297 }
161298
162- $ map ['markers ' ] ??= [] ;
299+ $ map ['markers ' ] ??= new SplObjectStorage () ;
163300 if (!\is_array ($ map ['markers ' ])) {
164301 throw new InvalidArgumentException ('The "markers" parameter must be an array. ' );
165302 }
166- $ map ['markers ' ] = array_map (Marker:: fromArray (...), $ map ['markers ' ]);
303+ $ map ['markers ' ] = self :: markersFromArray ( $ map ['markers ' ]);
167304
168- $ map ['polygons ' ] ??= [] ;
305+ $ map ['polygons ' ] ??= new SplObjectStorage () ;
169306 if (!\is_array ($ map ['polygons ' ])) {
170307 throw new InvalidArgumentException ('The "polygons" parameter must be an array. ' );
171308 }
172- $ map ['polygons ' ] = array_map (Polygon:: fromArray (...), $ map ['polygons ' ]);
309+ $ map ['polygons ' ] = self :: polygonsFromArray ( $ map ['polygons ' ]);
173310
174- $ map ['polylines ' ] ??= [] ;
311+ $ map ['polylines ' ] ??= new SplObjectStorage () ;
175312 if (!\is_array ($ map ['polylines ' ])) {
176313 throw new InvalidArgumentException ('The "polylines" parameter must be an array. ' );
177314 }
178- $ map ['polylines ' ] = array_map (Polyline:: fromArray (...), $ map ['polylines ' ]);
315+ $ map ['polylines ' ] = self :: polylinesFromArray ( $ map ['polylines ' ]);
179316
180317 return new self (...$ map );
181318 }
0 commit comments