Skip to content

Commit a27bd08

Browse files
committed
allow marker, polygone and polyline removal
1 parent feb0256 commit a27bd08

15 files changed

+392
-54
lines changed

src/Map/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
`HaversineDistanceCalculator`, `SphericalCosineDistanceCalculator` and `VincentyDistanceCalculator`.
99
- Add `CoordinateUtils` helper, to convert decimal coordinates (`43.2109`) in DMS (`56° 78' 90"`)
1010

11+
- Add property `$id` to `Marker`, `Polygon` and `Polyline` constructors
12+
- Add method `Map::removeMarker(string|Marker $markerOrId)`
13+
- Add method `Map::removePolygon(string|Polygon $polygonOrId)`
14+
- Add method `Map::removePolyline(string|Polyline $polylineOrId)`
15+
1116
## 2.22
1217

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

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

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
interface Element
15+
{
16+
}

src/Map/src/Elements.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
abstract class Elements
20+
{
21+
protected \SplObjectStorage $elements;
22+
23+
public function __construct(
24+
array $elements,
25+
) {
26+
$this->elements = new \SplObjectStorage();
27+
foreach ($elements as $element) {
28+
$this->elements->attach($element);
29+
}
30+
}
31+
32+
public function add(Element $element): self
33+
{
34+
$this->elements->attach($element, $element->id ?? $this->elements->getHash($element));
35+
36+
return $this;
37+
}
38+
39+
private function getElement(string $id): ?Element
40+
{
41+
foreach ($this->elements as $element) {
42+
if ($element->id === $id) {
43+
return $element;
44+
}
45+
}
46+
47+
return null;
48+
}
49+
50+
public function remove(Element|string $elementOrId): self
51+
{
52+
if (\is_string($elementOrId)) {
53+
$elementOrId = $this->getElement($elementOrId);
54+
}
55+
56+
if (null === $elementOrId) {
57+
return $this;
58+
}
59+
60+
if ($this->elements->contains($elementOrId)) {
61+
$this->elements->detach($elementOrId);
62+
}
63+
64+
return $this;
65+
}
66+
67+
public function toArray(): array
68+
{
69+
foreach ($this->elements as $element) {
70+
$elements[] = $element->toArray();
71+
}
72+
73+
return $elements ?? [];
74+
}
75+
76+
abstract public static function fromArray(array $elements): self;
77+
}

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 Polyline[] $polylines
30+
* @param Polygone[] $polygons
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->polylines = new Polylines($polylines);
44+
$this->polygons = new Polygons($polygons);
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

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
final class Markers extends Elements
20+
{
21+
public static function fromArray(array $elements): self
22+
{
23+
$elementObjects = [];
24+
25+
foreach ($elements as $element) {
26+
$elementObjects[] = Marker::fromArray($element);
27+
}
28+
29+
return new self(elements: $elementObjects);
30+
}
31+
}

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

src/Map/src/Polygons.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 Polygon collection.
16+
*
17+
* @author Sylvain Blondeau <[email protected]>
18+
*/
19+
final class Polygons extends Elements
20+
{
21+
public static function fromArray(array $elements): self
22+
{
23+
$elementObjects = [];
24+
25+
foreach ($elements as $element) {
26+
$elementObjects[] = Polygon::fromArray($element);
27+
}
28+
29+
return new self(elements: $elementObjects);
30+
}
31+
}

0 commit comments

Comments
 (0)