11
11
12
12
namespace Symfony \UX \Map ;
13
13
14
+ use SplObjectStorage ;
14
15
use Symfony \UX \Map \Exception \InvalidArgumentException ;
15
16
16
17
/**
@@ -29,19 +30,18 @@ public function __construct(
29
30
/**
30
31
* @var array<Marker>
31
32
*/
32
- private array $ markers = [] ,
33
+ private SplObjectStorage $ markers = new SplObjectStorage () ,
33
34
34
35
/**
35
36
* @var array<Polygon>
36
37
*/
37
- private array $ polygons = [] ,
38
+ private SplObjectStorage $ polygons = new SplObjectStorage () ,
38
39
39
40
/**
40
41
* @var array<Polyline>
41
42
*/
42
- private array $ polylines = [],
43
- ) {
44
- }
43
+ private SplObjectStorage $ polylines = new SplObjectStorage (),
44
+ ) {}
45
45
46
46
public function getRendererName (): ?string
47
47
{
@@ -86,23 +86,99 @@ public function hasOptions(): bool
86
86
return null !== $ this ->options ;
87
87
}
88
88
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
+
89
101
public function addMarker (Marker $ marker ): self
90
102
{
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
+ }
92
118
93
119
return $ this ;
94
120
}
95
121
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
+
96
134
public function addPolygon (Polygon $ polygon ): self
97
135
{
98
- $ this ->polygons [] = $ polygon ;
136
+ $ this ->polygons -> attach ( $ polygon , $ polygon -> identifier ?? $ this -> polygons -> getHash ( $ polygon)) ;
99
137
100
138
return $ this ;
101
139
}
102
140
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
+
103
166
public function addPolyline (Polyline $ polyline ): self
104
167
{
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
+ }
106
182
107
183
return $ this ;
108
184
}
@@ -124,12 +200,73 @@ public function toArray(): array
124
200
'zoom ' => $ this ->zoom ,
125
201
'fitBoundsToMarkers ' => $ this ->fitBoundsToMarkers ,
126
202
'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 ( ),
130
206
];
131
207
}
132
208
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
+
133
270
/**
134
271
* @param array{
135
272
* center?: array{lat: float, lng: float},
@@ -159,23 +296,23 @@ public static function fromArray(array $map): self
159
296
$ map ['fitBoundsToMarkers ' ] = false ;
160
297
}
161
298
162
- $ map ['markers ' ] ??= [] ;
299
+ $ map ['markers ' ] ??= new SplObjectStorage () ;
163
300
if (!\is_array ($ map ['markers ' ])) {
164
301
throw new InvalidArgumentException ('The "markers" parameter must be an array. ' );
165
302
}
166
- $ map ['markers ' ] = array_map (Marker:: fromArray (...), $ map ['markers ' ]);
303
+ $ map ['markers ' ] = self :: markersFromArray ( $ map ['markers ' ]);
167
304
168
- $ map ['polygons ' ] ??= [] ;
305
+ $ map ['polygons ' ] ??= new SplObjectStorage () ;
169
306
if (!\is_array ($ map ['polygons ' ])) {
170
307
throw new InvalidArgumentException ('The "polygons" parameter must be an array. ' );
171
308
}
172
- $ map ['polygons ' ] = array_map (Polygon:: fromArray (...), $ map ['polygons ' ]);
309
+ $ map ['polygons ' ] = self :: polygonsFromArray ( $ map ['polygons ' ]);
173
310
174
- $ map ['polylines ' ] ??= [] ;
311
+ $ map ['polylines ' ] ??= new SplObjectStorage () ;
175
312
if (!\is_array ($ map ['polylines ' ])) {
176
313
throw new InvalidArgumentException ('The "polylines" parameter must be an array. ' );
177
314
}
178
- $ map ['polylines ' ] = array_map (Polyline:: fromArray (...), $ map ['polylines ' ]);
315
+ $ map ['polylines ' ] = self :: polylinesFromArray ( $ map ['polylines ' ]);
179
316
180
317
return new self (...$ map );
181
318
}
0 commit comments