Skip to content

Commit 0f64532

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

File tree

16 files changed

+395
-48
lines changed

16 files changed

+395
-48
lines changed

src/LiveComponent/tests/Functional/EventListener/LiveComponentSubscriberTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\UX\LiveComponent\Tests\Functional\EventListener;
1313

1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
15-
use Symfony\Component\DomCrawler\Crawler;
16-
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1715
use Symfony\Component\Security\Core\User\InMemoryUser;
1816
use Symfony\Component\Security\Http\Attribute\IsGranted;
1917
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\Entity1;

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: 79 additions & 0 deletions
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+
use Symfony\UX\Map\Element;
15+
16+
/**
17+
* Represents a collection of map elements.
18+
*
19+
* @author Sylvain Blondeau <[email protected]>
20+
*/
21+
abstract class Elements
22+
{
23+
protected \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): self
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): self
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

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: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,26 @@
1111

1212
namespace Symfony\UX\Map;
1313

14+
use Symfony\UX\Map\Element;
1415
use Symfony\UX\Map\Exception\InvalidArgumentException;
1516

1617
/**
1718
* Represents a marker on a map.
1819
*
1920
* @author Hugo Alliaume <[email protected]>
2021
*/
21-
final readonly class Marker
22+
final readonly class Marker implements Element
2223
{
2324
/**
2425
* @param array<string, mixed> $extra Extra data, can be used by the developer to store additional information and
2526
* use them later JavaScript side
2627
*/
2728
public function __construct(
28-
private Point $position,
29-
private ?string $title = null,
30-
private ?InfoWindow $infoWindow = null,
31-
private array $extra = [],
29+
public Point $position,
30+
public ?string $title = null,
31+
public ?InfoWindow $infoWindow = null,
32+
public array $extra = [],
33+
public ?string $id = null,
3234
) {
3335
}
3436

@@ -38,6 +40,7 @@ public function __construct(
3840
* title: string|null,
3941
* infoWindow: array<string, mixed>|null,
4042
* extra: array,
43+
* id: string|null
4144
* }
4245
*/
4346
public function toArray(): array
@@ -47,6 +50,7 @@ public function toArray(): array
4750
'title' => $this->title,
4851
'infoWindow' => $this->infoWindow?->toArray(),
4952
'extra' => $this->extra,
53+
'id' => $this->id,
5054
];
5155
}
5256

@@ -56,6 +60,7 @@ public function toArray(): array
5660
* title: string|null,
5761
* infoWindow: array<string, mixed>|null,
5862
* extra: array,
63+
* id: string|null
5964
* } $marker
6065
*
6166
* @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+
}

0 commit comments

Comments
 (0)