Skip to content

Commit e19fff9

Browse files
sblondeauKocal
authored andcommitted
[Map] Add dedicated methods to remove Marker, Polygon and Polyline instances from a Map
1 parent feb0256 commit e19fff9

15 files changed

+404
-54
lines changed

src/Map/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
- Add `DistanceCalculatorInterface` interface and three implementations:
88
`HaversineDistanceCalculator`, `SphericalCosineDistanceCalculator` and `VincentyDistanceCalculator`.
99
- Add `CoordinateUtils` helper, to convert decimal coordinates (`43.2109`) in DMS (`56° 78' 90"`)
10+
- Add parameter `id` to `Marker`, `Polygon` and `Polyline` constructors
11+
- Add method `Map::removeMarker(string|Marker $markerOrId)`
12+
- Add method `Map::removePolygon(string|Polygon $polygonOrId)`
13+
- Add method `Map::removePolyline(string|Polyline $polylineOrId)`
1014

1115
## 2.22
1216

src/Map/doc/index.rst

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

139+
Remove elements from Map
140+
~~~~~~~~~~~~~~~~~~~~~~~~
141+
142+
It is possible to remove elements like ``Marker``, ``Polygon`` and ``Polyline`` instances by using ``Map::remove*()`` methods::
143+
144+
// Add elements
145+
$map->addMarker($marker = new Marker(/* ... */));
146+
$map->addPolygon($polygon = new Polygon(/* ... */));
147+
$map->addPolyline($polyline = new Polyline(/* ... */));
148+
149+
// And later, remove those elements
150+
$map->removeMarker($marker);
151+
$map->removePolygon($polygon);
152+
$map->removePolyline($polyline);
153+
154+
If unfortunately you were unable to store an element instance, you can still remove them by passing the identifier string::
155+
156+
$map = new Map(/* ... */);
157+
// Add elements
158+
$map->addMarker(new Marker(id: 'my-marker', /* ... */));
159+
$map->addPolygon(new Polygon(id: 'my-polygon', /* ... */));
160+
$map->addPolyline(new Polyline(id: 'my-marker', /* ... */));
161+
162+
// And later, remove those elements
163+
$map->removeMarker('my-marker');
164+
$map->removePolygon('my-polygon');
165+
$map->removePolyline('my-marker');
166+
139167
Add Polygons
140168
~~~~~~~~~~~~
141169

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/Element.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Map;
13+
14+
/**
15+
* @author Sylvain Blondeau <[email protected]>
16+
*
17+
* @internal
18+
*/
19+
interface Element
20+
{
21+
}

src/Map/src/Elements.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Map;
13+
14+
/**
15+
* Represents a collection of map elements.
16+
*
17+
* @author Sylvain Blondeau <[email protected]>
18+
*
19+
* @internal
20+
*/
21+
abstract class Elements
22+
{
23+
private \SplObjectStorage $elements;
24+
25+
public function __construct(
26+
array $elements,
27+
) {
28+
$this->elements = new \SplObjectStorage();
29+
foreach ($elements as $element) {
30+
$this->elements->attach($element);
31+
}
32+
}
33+
34+
public function add(Element $element): static
35+
{
36+
$this->elements->attach($element, $element->id ?? $this->elements->getHash($element));
37+
38+
return $this;
39+
}
40+
41+
private function getElement(string $id): ?Element
42+
{
43+
foreach ($this->elements as $element) {
44+
if ($element->id === $id) {
45+
return $element;
46+
}
47+
}
48+
49+
return null;
50+
}
51+
52+
public function remove(Element|string $elementOrId): static
53+
{
54+
if (\is_string($elementOrId)) {
55+
$elementOrId = $this->getElement($elementOrId);
56+
}
57+
58+
if (null === $elementOrId) {
59+
return $this;
60+
}
61+
62+
if ($this->elements->contains($elementOrId)) {
63+
$this->elements->detach($elementOrId);
64+
}
65+
66+
return $this;
67+
}
68+
69+
public function toArray(): array
70+
{
71+
foreach ($this->elements as $element) {
72+
$elements[] = $element->toArray();
73+
}
74+
75+
return $elements ?? [];
76+
}
77+
78+
abstract public static function fromArray(array $elements): self;
79+
}

src/Map/src/Map.php

+42-20
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,28 @@
2020
*/
2121
final class Map
2222
{
23+
private Markers $markers;
24+
private Polygons $polygons;
25+
private Polylines $polylines;
26+
27+
/**
28+
* @param Marker[] $markers
29+
* @param Polygon[] $polygons
30+
* @param Polyline[] $polylines
31+
*/
2332
public function __construct(
2433
private readonly ?string $rendererName = null,
2534
private ?MapOptionsInterface $options = null,
2635
private ?Point $center = null,
2736
private ?float $zoom = null,
2837
private bool $fitBoundsToMarkers = false,
29-
/**
30-
* @var array<Marker>
31-
*/
32-
private array $markers = [],
33-
34-
/**
35-
* @var array<Polygon>
36-
*/
37-
private array $polygons = [],
38-
39-
/**
40-
* @var array<Polyline>
41-
*/
42-
private array $polylines = [],
38+
array $markers = [],
39+
array $polygons = [],
40+
array $polylines = [],
4341
) {
42+
$this->markers = new Markers($markers);
43+
$this->polygons = new Polygons($polygons);
44+
$this->polylines = new Polylines($polylines);
4445
}
4546

4647
public function getRendererName(): ?string
@@ -88,21 +89,42 @@ public function hasOptions(): bool
8889

8990
public function addMarker(Marker $marker): self
9091
{
91-
$this->markers[] = $marker;
92+
$this->markers->add($marker);
93+
94+
return $this;
95+
}
96+
97+
public function removeMarker(Marker|string $markerOrId): self
98+
{
99+
$this->markers->remove($markerOrId);
92100

93101
return $this;
94102
}
95103

96104
public function addPolygon(Polygon $polygon): self
97105
{
98-
$this->polygons[] = $polygon;
106+
$this->polygons->add($polygon);
107+
108+
return $this;
109+
}
110+
111+
public function removePolygon(Polygon|string $polygonOrId): self
112+
{
113+
$this->polygons->remove($polygonOrId);
99114

100115
return $this;
101116
}
102117

103118
public function addPolyline(Polyline $polyline): self
104119
{
105-
$this->polylines[] = $polyline;
120+
$this->polylines->add($polyline);
121+
122+
return $this;
123+
}
124+
125+
public function removePolyline(Polyline|string $polylineOrId): self
126+
{
127+
$this->polylines->remove($polylineOrId);
106128

107129
return $this;
108130
}
@@ -124,9 +146,9 @@ public function toArray(): array
124146
'zoom' => $this->zoom,
125147
'fitBoundsToMarkers' => $this->fitBoundsToMarkers,
126148
'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),
149+
'markers' => $this->markers->toArray(),
150+
'polygons' => $this->polygons->toArray(),
151+
'polylines' => $this->polylines->toArray(),
130152
];
131153
}
132154

src/Map/src/Marker.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@
1818
*
1919
* @author Hugo Alliaume <[email protected]>
2020
*/
21-
final readonly class Marker
21+
final readonly class Marker implements Element
2222
{
2323
/**
2424
* @param array<string, mixed> $extra Extra data, can be used by the developer to store additional information and
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 $id = 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+
* id: 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+
'id' => $this->id,
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+
* id: string|null
5963
* } $marker
6064
*
6165
* @internal

src/Map/src/Markers.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Map;
13+
14+
/**
15+
* Represents a Marker collection.
16+
*
17+
* @author Sylvain Blondeau <[email protected]>
18+
*
19+
* @internal
20+
*/
21+
final class Markers extends Elements
22+
{
23+
public static function fromArray(array $elements): self
24+
{
25+
$elementObjects = [];
26+
27+
foreach ($elements as $element) {
28+
$elementObjects[] = Marker::fromArray($element);
29+
}
30+
31+
return new self(elements: $elementObjects);
32+
}
33+
}

src/Map/src/Polygon.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author [Pierre Svgnt]
2020
*/
21-
final readonly class Polygon
21+
final readonly class Polygon implements Element
2222
{
2323
/**
2424
* @param array<string, mixed> $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side
@@ -28,6 +28,7 @@ public function __construct(
2828
private ?string $title = null,
2929
private ?InfoWindow $infoWindow = null,
3030
private array $extra = [],
31+
public ?string $id = null,
3132
) {
3233
}
3334

@@ -39,6 +40,7 @@ public function __construct(
3940
* title: string|null,
4041
* infoWindow: array<string, mixed>|null,
4142
* extra: array,
43+
* id: string|null
4244
* }
4345
*/
4446
public function toArray(): array
@@ -48,6 +50,7 @@ public function toArray(): array
4850
'title' => $this->title,
4951
'infoWindow' => $this->infoWindow?->toArray(),
5052
'extra' => $this->extra,
53+
'id' => $this->id,
5154
];
5255
}
5356

@@ -57,6 +60,7 @@ public function toArray(): array
5760
* title: string|null,
5861
* infoWindow: array<string, mixed>|null,
5962
* extra: array,
63+
* id: string|null
6064
* } $polygon
6165
*
6266
* @internal

0 commit comments

Comments
 (0)