Skip to content

Commit

Permalink
[Map] Enhance ux_map(), allow to create a Map directly in Twig!
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Sep 11, 2024
1 parent e22484b commit 0df70cb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Map/src/InfoWindow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Map/src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@
*/
final class Map
{
/**
* @param array{0: float, 1: float} $center
* @param array<array{ point: array, title: string|null}> $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,
Expand Down
14 changes: 14 additions & 0 deletions src/Map/src/Marker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side
*/
Expand Down
10 changes: 10 additions & 0 deletions src/Map/src/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand Down
12 changes: 10 additions & 2 deletions src/Map/src/Renderer/Renderers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 0df70cb

Please # to comment.