Skip to content

Commit aac7b0f

Browse files
committed
allow marker, polygone and polyline removal
1 parent dec3eb3 commit aac7b0f

File tree

10 files changed

+316
-55
lines changed

10 files changed

+316
-55
lines changed

src/Map/CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
2-
2+
3+
## 2.23
4+
- Add property `$identifier` to marker, polygon and polyline
5+
- Add method `Symfony\UX\Map::removeMarker(Marker $marker)`, to allow removal of a marker from its identifier
6+
- Add method `Symfony\UX\Map::removePolygon(Polygon $polygon)`, to allow removal of a polygon from its identifier
7+
- Add method `Symfony\UX\Map::removePolyline(Polyline $polyline)`, to allow removal of a polyline from its identifier
8+
39
## 2.22
410

511
- Add method `Symfony\UX\Map\Renderer\AbstractRenderer::tapOptions()`, to allow Renderer to modify options before rendering a Map.

src/Map/doc/index.rst

+23
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ You can add markers to a map using the ``addMarker()`` method::
136136
))
137137
;
138138

139+
Remove elements from Map
140+
~~~~~~~~~~~~~~~~~~~~~~~~
141+
142+
You can remove markers, polygons and polylines from a map using the ``removeMarker(Marker $marker)``, ``removePolyline(Polyline $polyline)`` and ``removePolygon(Polygon $polygon)`` methods::
143+
144+
When you create Marker, Polygon or Polyne, you can add an identifier in construct :
145+
146+
$departureMarker = new Marker (
147+
position: new Point(45.7640, 4.8357),
148+
title: 'Lyon',
149+
identifier: 'departure'
150+
)
151+
152+
then retrieve the marker with ``Map::getMarker(string $identifier)``, also works with ``Map::getPolygon(string $identifier)`` and ``Map::getPolyline(string $identifier)``. It can be useful to remove an element from its identifier.
153+
154+
$map->addMarker($departureMarker);
155+
156+
// remove marker with
157+
$map->removeMarker($departureMarker)
158+
// or
159+
$map->removeMarker($map->getMarker('departure'));
160+
161+
139162
Add Polygons
140163
~~~~~~~~~~~~
141164

src/Map/src/Bridge/Google/tests/GoogleRendererTest.php

+55-12
Large diffs are not rendered by default.

src/Map/src/Bridge/Leaflet/tests/LeafletRendererTest.php

+48-15
Large diffs are not rendered by default.

src/Map/src/Map.php

+154-17
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\UX\Map;
1313

14+
use SplObjectStorage;
1415
use 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
}

src/Map/src/Marker.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
* use them later JavaScript side
2626
*/
2727
public function __construct(
28-
private Point $position,
29-
private ?string $title = null,
30-
private ?InfoWindow $infoWindow = null,
31-
private array $extra = [],
28+
public Point $position,
29+
public ?string $title = null,
30+
public ?InfoWindow $infoWindow = null,
31+
public array $extra = [],
32+
public ?string $identifier = null,
3233
) {
3334
}
3435

@@ -38,6 +39,7 @@ public function __construct(
3839
* title: string|null,
3940
* infoWindow: array<string, mixed>|null,
4041
* extra: array,
42+
* identifier: string|null
4143
* }
4244
*/
4345
public function toArray(): array
@@ -47,6 +49,7 @@ public function toArray(): array
4749
'title' => $this->title,
4850
'infoWindow' => $this->infoWindow?->toArray(),
4951
'extra' => $this->extra,
52+
'identifier' => $this->identifier,
5053
];
5154
}
5255

@@ -56,6 +59,7 @@ public function toArray(): array
5659
* title: string|null,
5760
* infoWindow: array<string, mixed>|null,
5861
* extra: array,
62+
* identifier: string|null
5963
* } $marker
6064
*
6165
* @internal

src/Map/src/Polygon.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function __construct(
2828
private ?string $title = null,
2929
private ?InfoWindow $infoWindow = null,
3030
private array $extra = [],
31-
) {
32-
}
31+
public ?string $identifier = null,
32+
) {}
3333

3434
/**
3535
* Convert the polygon to an array representation.
@@ -39,15 +39,17 @@ public function __construct(
3939
* title: string|null,
4040
* infoWindow: array<string, mixed>|null,
4141
* extra: array,
42+
* identifier: string|null
4243
* }
4344
*/
4445
public function toArray(): array
4546
{
4647
return [
47-
'points' => array_map(fn (Point $point) => $point->toArray(), $this->points),
48+
'points' => array_map(fn(Point $point) => $point->toArray(), $this->points),
4849
'title' => $this->title,
4950
'infoWindow' => $this->infoWindow?->toArray(),
5051
'extra' => $this->extra,
52+
'identifier' => $this->identifier,
5153
];
5254
}
5355

@@ -57,6 +59,7 @@ public function toArray(): array
5759
* title: string|null,
5860
* infoWindow: array<string, mixed>|null,
5961
* extra: array,
62+
* identifier: string|null
6063
* } $polygon
6164
*
6265
* @internal

0 commit comments

Comments
 (0)