Skip to content

Commit 277d6a6

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

File tree

15 files changed

+390
-46
lines changed

15 files changed

+390
-46
lines changed

src/Map/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,33 @@ 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+
// Add elements
144+
$map->addMarker($marker = new Marker(/* ... */));
145+
$map->addPolygon($polygon = new Polygon(/* ... */));
146+
$map->addPolyline($polyline = new Polyline(/* ... */));
147+
148+
// And later, remove those elements
149+
$map->removeMarker($marker);
150+
$map->removePolygon($polygon);
151+
$map->removePolyline($polyline);
152+
153+
If unfortunately you were unable to store an element instance, you can still remove them by passing the identifier string::
154+
155+
$map = new Map(/* ... */);
156+
// Add elements
157+
$map->addMarker(new Marker(id: 'my-marker', /* ... */));
158+
$map->addPolygon(new Polygon(id: 'my-polygon', /* ... */));
159+
$map->addPolyline(new Polyline(id: 'my-marker', /* ... */));
160+
161+
// And later, remove those elements
162+
$map->removeMarker('my-marker');
163+
$map->removePolygon('my-polygon');
164+
$map->removePolyline('my-marker');
165+
139166
Add Polygons
140167
~~~~~~~~~~~~
141168

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

Lines changed: 55 additions & 12 deletions
Large diffs are not rendered by default.

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

Lines changed: 48 additions & 15 deletions
Large diffs are not rendered by default.

src/Map/src/Element.php

Lines changed: 16 additions & 0 deletions
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

Lines changed: 77 additions & 0 deletions
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

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,35 @@
2020
*/
2121
final class Map
2222
{
23+
private Markers $markers;
24+
private Polygons $polygons;
25+
private Polylines $polylines;
26+
2327
public function __construct(
2428
private readonly ?string $rendererName = null,
2529
private ?MapOptionsInterface $options = null,
2630
private ?Point $center = null,
2731
private ?float $zoom = null,
2832
private bool $fitBoundsToMarkers = false,
33+
2934
/**
30-
* @var array<Marker>
35+
* @var Marker[]
3136
*/
32-
private array $markers = [],
37+
array $markers = [],
3338

3439
/**
35-
* @var array<Polygon>
40+
* @var Polygon[]
3641
*/
37-
private array $polygons = [],
42+
array $polygons = [],
3843

3944
/**
40-
* @var array<Polyline>
45+
* @var Polyline[]
4146
*/
42-
private array $polylines = [],
47+
array $polylines = [],
4348
) {
49+
$this->markers = new Markers($markers);
50+
$this->polylines = new Polylines($polylines);
51+
$this->polygons = new Polygons($polygons);
4452
}
4553

4654
public function getRendererName(): ?string
@@ -88,21 +96,42 @@ public function hasOptions(): bool
8896

8997
public function addMarker(Marker $marker): self
9098
{
91-
$this->markers[] = $marker;
99+
$this->markers->add($marker);
100+
101+
return $this;
102+
}
103+
104+
public function removeMarker(Marker|string $markerOrId): self
105+
{
106+
$this->markers->remove($markerOrId);
92107

93108
return $this;
94109
}
95110

96111
public function addPolygon(Polygon $polygon): self
97112
{
98-
$this->polygons[] = $polygon;
113+
$this->polygons->add($polygon);
114+
115+
return $this;
116+
}
117+
118+
public function removePolygon(Polygon|string $polygonOrId): self
119+
{
120+
$this->polygons->remove($polygonOrId);
99121

100122
return $this;
101123
}
102124

103125
public function addPolyline(Polyline $polyline): self
104126
{
105-
$this->polylines[] = $polyline;
127+
$this->polylines->add($polyline);
128+
129+
return $this;
130+
}
131+
132+
public function removePolyline(Polyline|string $polylineOrId): self
133+
{
134+
$this->polylines->remove($polylineOrId);
106135

107136
return $this;
108137
}
@@ -124,9 +153,9 @@ public function toArray(): array
124153
'zoom' => $this->zoom,
125154
'fitBoundsToMarkers' => $this->fitBoundsToMarkers,
126155
'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),
156+
'markers' => $this->markers->toArray(),
157+
'polygons' => $this->polygons->toArray(),
158+
'polylines' => $this->polylines->toArray(),
130159
];
131160
}
132161

src/Map/src/Marker.php

Lines changed: 9 additions & 5 deletions
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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)