Skip to content

Commit

Permalink
allow marker, polygone and polyline removal
Browse files Browse the repository at this point in the history
  • Loading branch information
sblondeau committed Feb 13, 2025
1 parent feb0256 commit a27bd08
Show file tree
Hide file tree
Showing 15 changed files with 392 additions and 54 deletions.
5 changes: 5 additions & 0 deletions src/Map/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
`HaversineDistanceCalculator`, `SphericalCosineDistanceCalculator` and `VincentyDistanceCalculator`.
- Add `CoordinateUtils` helper, to convert decimal coordinates (`43.2109`) in DMS (`56° 78' 90"`)

- Add property `$id` to `Marker`, `Polygon` and `Polyline` constructors
- Add method `Map::removeMarker(string|Marker $markerOrId)`
- Add method `Map::removePolygon(string|Polygon $polygonOrId)`
- Add method `Map::removePolyline(string|Polyline $polylineOrId)`

## 2.22

- Add method `Symfony\UX\Map\Renderer\AbstractRenderer::tapOptions()`, to allow Renderer to modify options before rendering a Map.
Expand Down
28 changes: 28 additions & 0 deletions src/Map/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,34 @@ You can add markers to a map using the ``addMarker()`` method::
))
;

Remove elements from Map
~~~~~~~~~~~~~~~~~~~~~~~~

It is possible to remove elements like ``Marker``, ``Polygon`` and ``Polyline`` instances by using ``Map::remove*()`` methods::
// Add elements
$map->addMarker($marker = new Marker(/* ... */));
$map->addPolygon($polygon = new Polygon(/* ... */));
$map->addPolyline($polyline = new Polyline(/* ... */));
// And later, remove those elements
$map->removeMarker($marker);
$map->removePolygon($polygon);
$map->removePolyline($polyline);
If unfortunately you were unable to store an element instance, you can still remove them by passing the identifier string::
$map = new Map(/* ... */);
// Add elements
$map->addMarker(new Marker(id: 'my-marker', /* ... */));
$map->addPolygon(new Polygon(id: 'my-polygon', /* ... */));
$map->addPolyline(new Polyline(id: 'my-marker', /* ... */));
// And later, remove those elements
$map->removeMarker('my-marker');
$map->removePolygon('my-polygon');
$map->removePolyline('my-marker');

Add Polygons
~~~~~~~~~~~~

Expand Down
67 changes: 55 additions & 12 deletions src/Map/src/Bridge/Google/tests/GoogleRendererTest.php

Large diffs are not rendered by default.

63 changes: 48 additions & 15 deletions src/Map/src/Bridge/Leaflet/tests/LeafletRendererTest.php

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/Map/src/Element.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map;

interface Element
{
}
77 changes: 77 additions & 0 deletions src/Map/src/Elements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map;

/**
* Represents a collection of map elements.
*
* @author Sylvain Blondeau <[email protected]>
*/
abstract class Elements
{
protected \SplObjectStorage $elements;

public function __construct(
array $elements,
) {
$this->elements = new \SplObjectStorage();
foreach ($elements as $element) {
$this->elements->attach($element);
}
}

public function add(Element $element): self
{
$this->elements->attach($element, $element->id ?? $this->elements->getHash($element));

return $this;
}

private function getElement(string $id): ?Element
{
foreach ($this->elements as $element) {
if ($element->id === $id) {
return $element;
}
}

return null;
}

public function remove(Element|string $elementOrId): self
{
if (\is_string($elementOrId)) {
$elementOrId = $this->getElement($elementOrId);
}

if (null === $elementOrId) {
return $this;
}

if ($this->elements->contains($elementOrId)) {
$this->elements->detach($elementOrId);
}

return $this;
}

public function toArray(): array
{
foreach ($this->elements as $element) {
$elements[] = $element->toArray();
}

return $elements ?? [];
}

abstract public static function fromArray(array $elements): self;
}
62 changes: 42 additions & 20 deletions src/Map/src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,28 @@
*/
final class Map
{
private Markers $markers;
private Polygons $polygons;
private Polylines $polylines;

/**
* @param Marker[] $markers
* @param Polyline[] $polylines
* @param Polygone[] $polygons
*/
public function __construct(
private readonly ?string $rendererName = null,
private ?MapOptionsInterface $options = null,
private ?Point $center = null,
private ?float $zoom = null,
private bool $fitBoundsToMarkers = false,
/**
* @var array<Marker>
*/
private array $markers = [],

/**
* @var array<Polygon>
*/
private array $polygons = [],

/**
* @var array<Polyline>
*/
private array $polylines = [],
array $markers = [],
array $polygons = [],
array $polylines = [],
) {
$this->markers = new Markers($markers);
$this->polylines = new Polylines($polylines);
$this->polygons = new Polygons($polygons);
}

public function getRendererName(): ?string
Expand Down Expand Up @@ -88,21 +89,42 @@ public function hasOptions(): bool

public function addMarker(Marker $marker): self
{
$this->markers[] = $marker;
$this->markers->add($marker);

return $this;
}

public function removeMarker(Marker|string $markerOrId): self
{
$this->markers->remove($markerOrId);

return $this;
}

public function addPolygon(Polygon $polygon): self
{
$this->polygons[] = $polygon;
$this->polygons->add($polygon);

return $this;
}

public function removePolygon(Polygon|string $polygonOrId): self
{
$this->polygons->remove($polygonOrId);

return $this;
}

public function addPolyline(Polyline $polyline): self
{
$this->polylines[] = $polyline;
$this->polylines->add($polyline);

return $this;
}

public function removePolyline(Polyline|string $polylineOrId): self
{
$this->polylines->remove($polylineOrId);

return $this;
}
Expand All @@ -124,9 +146,9 @@ public function toArray(): array
'zoom' => $this->zoom,
'fitBoundsToMarkers' => $this->fitBoundsToMarkers,
'options' => $this->options ? MapOptionsNormalizer::normalize($this->options) : [],
'markers' => array_map(static fn (Marker $marker) => $marker->toArray(), $this->markers),
'polygons' => array_map(static fn (Polygon $polygon) => $polygon->toArray(), $this->polygons),
'polylines' => array_map(static fn (Polyline $polyline) => $polyline->toArray(), $this->polylines),
'markers' => $this->markers->toArray(),
'polygons' => $this->polygons->toArray(),
'polylines' => $this->polylines->toArray(),
];
}

Expand Down
14 changes: 9 additions & 5 deletions src/Map/src/Marker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
*
* @author Hugo Alliaume <[email protected]>
*/
final readonly class Marker
final readonly class Marker implements Element
{
/**
* @param array<string, mixed> $extra Extra data, can be used by the developer to store additional information and
* use them later JavaScript side
*/
public function __construct(
private Point $position,
private ?string $title = null,
private ?InfoWindow $infoWindow = null,
private array $extra = [],
public Point $position,
public ?string $title = null,
public ?InfoWindow $infoWindow = null,
public array $extra = [],
public ?string $id = null,
) {
}

Expand All @@ -38,6 +39,7 @@ public function __construct(
* title: string|null,
* infoWindow: array<string, mixed>|null,
* extra: array,
* id: string|null
* }
*/
public function toArray(): array
Expand All @@ -47,6 +49,7 @@ public function toArray(): array
'title' => $this->title,
'infoWindow' => $this->infoWindow?->toArray(),
'extra' => $this->extra,
'id' => $this->id,
];
}

Expand All @@ -56,6 +59,7 @@ public function toArray(): array
* title: string|null,
* infoWindow: array<string, mixed>|null,
* extra: array,
* id: string|null
* } $marker
*
* @internal
Expand Down
31 changes: 31 additions & 0 deletions src/Map/src/Markers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map;

/**
* Represents a Marker collection.
*
* @author Sylvain Blondeau <[email protected]>
*/
final class Markers extends Elements
{
public static function fromArray(array $elements): self
{
$elementObjects = [];

foreach ($elements as $element) {
$elementObjects[] = Marker::fromArray($element);
}

return new self(elements: $elementObjects);
}
}
6 changes: 5 additions & 1 deletion src/Map/src/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author [Pierre Svgnt]
*/
final readonly class Polygon
final readonly class Polygon implements Element
{
/**
* @param array<string, mixed> $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side
Expand All @@ -28,6 +28,7 @@ public function __construct(
private ?string $title = null,
private ?InfoWindow $infoWindow = null,
private array $extra = [],
public ?string $id = null,
) {
}

Expand All @@ -39,6 +40,7 @@ public function __construct(
* title: string|null,
* infoWindow: array<string, mixed>|null,
* extra: array,
* id: string|null
* }
*/
public function toArray(): array
Expand All @@ -48,6 +50,7 @@ public function toArray(): array
'title' => $this->title,
'infoWindow' => $this->infoWindow?->toArray(),
'extra' => $this->extra,
'id' => $this->id,
];
}

Expand All @@ -57,6 +60,7 @@ public function toArray(): array
* title: string|null,
* infoWindow: array<string, mixed>|null,
* extra: array,
* id: string|null
* } $polygon
*
* @internal
Expand Down
31 changes: 31 additions & 0 deletions src/Map/src/Polygons.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map;

/**
* Represents a Polygon collection.
*
* @author Sylvain Blondeau <[email protected]>
*/
final class Polygons extends Elements
{
public static function fromArray(array $elements): self
{
$elementObjects = [];

foreach ($elements as $element) {
$elementObjects[] = Polygon::fromArray($element);
}

return new self(elements: $elementObjects);
}
}
Loading

0 comments on commit a27bd08

Please sign in to comment.