diff --git a/src/Map/src/InfoWindow.php b/src/Map/src/InfoWindow.php index 4897b3c9cd..a822465c24 100644 --- a/src/Map/src/InfoWindow.php +++ b/src/Map/src/InfoWindow.php @@ -18,6 +18,24 @@ */ final readonly class InfoWindow { + public static function fromArray( + ?string $headerContent = null, + ?string $content = null, + ?array $position = null, + bool $opened = false, + bool $autoClose = true, + array $extra = [], + ): self { + return new self( + $headerContent, + $content, + $position ? Point::fromArray($position) : null, + $opened, + $autoClose, + $extra, + ); + } + /** * @param array $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side */ diff --git a/src/Map/src/Map.php b/src/Map/src/Map.php index 834e0d3d29..495a96442c 100644 --- a/src/Map/src/Map.php +++ b/src/Map/src/Map.php @@ -20,6 +20,28 @@ */ final class Map { + /** + * @param array{0: float, 1: float} $center + * @param array $markers + * + * @return self + */ + public static function fromArray( + array $center, + float $zoom, + bool $fitBoundsToMarkers, + array $markers, + ) { + return new self( + null, + null, + Point::fromArray($center), + $zoom, + $fitBoundsToMarkers, + array_map(static fn (array $marker) => Marker::fromArray(...$marker), $markers), + ); + } + public function __construct( private readonly ?string $rendererName = null, private ?MapOptionsInterface $options = null, diff --git a/src/Map/src/Marker.php b/src/Map/src/Marker.php index 5c822698b7..5a8dd5b600 100644 --- a/src/Map/src/Marker.php +++ b/src/Map/src/Marker.php @@ -18,6 +18,20 @@ */ final readonly class Marker { + public static function fromArray( + array $position, + ?string $title = null, + ?array $infoWindow = null, + array $extra = [], + ): self { + return new self( + Point::fromArray($position), + $title, + $infoWindow ? InfoWindow::fromArray(...$infoWindow) : null, + $extra, + ); + } + /** * @param array $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side */ diff --git a/src/Map/src/Point.php b/src/Map/src/Point.php index a6d71d88f6..cb12d2473a 100644 --- a/src/Map/src/Point.php +++ b/src/Map/src/Point.php @@ -33,6 +33,16 @@ public function __construct( } } + /** + * @param array{0: float, 1: float} $center + * + * @return self + */ + public static function fromArray(array $center) + { + return new self($center[0], $center[1]); + } + /** * @return array{lat: float, lng: float} */ diff --git a/src/Map/src/Renderer/Renderers.php b/src/Map/src/Renderer/Renderers.php index ea7fae8eed..cd1a97422d 100644 --- a/src/Map/src/Renderer/Renderers.php +++ b/src/Map/src/Renderer/Renderers.php @@ -42,8 +42,16 @@ public function __construct(iterable $renderers) } } - public function renderMap(Map $map, array $attributes = []): string - { + public function renderMap( + ?Map $map = null, + array $attributes = [], + ?array $center = null, + ?float $zoom = null, + bool $fitBoundsToMarkers = false, + array $markers = [], + ): string { + $map ??= Map::fromArray($center, $zoom, $fitBoundsToMarkers, $markers); + $renderer = $this->default; if ($rendererName = $map->getRendererName()) {