Skip to content

Commit 1683967

Browse files
rrr63Kocal
authored andcommitted
[Map] Add polygon to twig component
1 parent f04fb4f commit 1683967

File tree

6 files changed

+100
-1
lines changed

6 files changed

+100
-1
lines changed

src/Map/src/Map.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public function toArray(): array
122122
* center?: array{lat: float, lng: float},
123123
* zoom?: float,
124124
* markers?: list<array>,
125+
* polygons?: list<array>,
125126
* fitBoundsToMarkers?: bool,
126127
* options?: object,
127128
* } $map
@@ -146,6 +147,12 @@ public static function fromArray(array $map): self
146147
}
147148
$map['markers'] = array_map(Marker::fromArray(...), $map['markers']);
148149

150+
$map['polygons'] ??= [];
151+
if (!\is_array($map['polygons'])) {
152+
throw new InvalidArgumentException('The "polygons" parameter must be an array.');
153+
}
154+
$map['polygons'] = array_map(Polygon::fromArray(...), $map['polygons']);
155+
149156
return new self(...$map);
150157
}
151158
}

src/Map/src/Polygon.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\UX\Map;
1313

14+
use Symfony\UX\Map\Exception\InvalidArgumentException;
15+
1416
/**
1517
* Represents a polygon on a map.
1618
*
@@ -41,4 +43,28 @@ public function toArray(): array
4143
'extra' => (object) $this->extra,
4244
];
4345
}
46+
47+
/**
48+
* @param array{
49+
* points: array<array{lat: float, lng: float}>,
50+
* title: string|null,
51+
* infoWindow: array<string, mixed>|null,
52+
* extra: object,
53+
* } $polygon
54+
*
55+
* @internal
56+
*/
57+
public static function fromArray(array $polygon): self
58+
{
59+
if (!isset($polygon['points'])) {
60+
throw new InvalidArgumentException('The "points" parameter is required.');
61+
}
62+
$polygon['points'] = array_map(Point::fromArray(...), $polygon['points']);
63+
64+
if (isset($polygon['infoWindow'])) {
65+
$polygon['infoWindow'] = InfoWindow::fromArray($polygon['infoWindow']);
66+
}
67+
68+
return new self(...$polygon);
69+
}
4470
}

src/Map/src/Twig/MapRuntime.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\UX\Map\Map;
1515
use Symfony\UX\Map\Marker;
1616
use Symfony\UX\Map\Point;
17+
use Symfony\UX\Map\Polygon;
1718
use Symfony\UX\Map\Renderer\RendererInterface;
1819
use Twig\Extension\RuntimeExtensionInterface;
1920

@@ -32,11 +33,13 @@ public function __construct(
3233
/**
3334
* @param array<string, mixed> $attributes
3435
* @param array<string, mixed> $markers
36+
* @param array<string, mixed> $polygons
3537
*/
3638
public function renderMap(
3739
?Map $map = null,
3840
array $attributes = [],
3941
?array $markers = null,
42+
?array $polygons = null,
4043
?array $center = null,
4144
?float $zoom = null,
4245
): string {
@@ -52,6 +55,9 @@ public function renderMap(
5255
foreach ($markers ?? [] as $marker) {
5356
$map->addMarker(Marker::fromArray($marker));
5457
}
58+
foreach ($polygons ?? [] as $polygons) {
59+
$map->addPolygon(Polygon::fromArray($polygons));
60+
}
5561
if (null !== $center) {
5662
$map->center(Point::fromArray($center));
5763
}

src/Map/src/Twig/UXMapComponent.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\UX\Map\Marker;
1515
use Symfony\UX\Map\Point;
16+
use Symfony\UX\Map\Polygon;
1617

1718
/**
1819
* @author Simon André <smn.andre@gmail.com>
@@ -29,4 +30,9 @@ final class UXMapComponent
2930
* @var Marker[]
3031
*/
3132
public array $markers;
33+
34+
/**
35+
* @var Polygon[]
36+
*/
37+
public array $polygons;
3238
}

src/Map/src/Twig/UXMapComponentListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function onPreCreateForRender(PreCreateForRenderEvent $event): void
3232
}
3333

3434
$attributes = $event->getInputProps();
35-
$map = array_intersect_key($attributes, ['markers' => 0, 'center' => 1, 'zoom' => 2]);
35+
$map = array_intersect_key($attributes, ['markers' => 0, 'polygons' => 0, 'center' => 1, 'zoom' => 2]);
3636
$attributes = array_diff_key($attributes, $map);
3737

3838
$html = $this->mapRuntime->renderMap(...$map, attributes: $attributes);

src/Map/tests/MapFactoryTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public function testFromArray(): void
3232
$this->assertSame($array['markers'][0]['title'], $markers[0]['title']);
3333
$this->assertSame($array['markers'][0]['infoWindow']['headerContent'], $markers[0]['infoWindow']['headerContent']);
3434
$this->assertSame($array['markers'][0]['infoWindow']['content'], $markers[0]['infoWindow']['content']);
35+
36+
$this->assertCount(1, $polygons = $map->toArray()['polygons']);
37+
$this->assertEquals($array['polygons'][0]['points'], $polygons[0]['points']);
38+
$this->assertEquals($array['polygons'][0]['points'], $polygons[0]['points']);
39+
$this->assertSame($array['polygons'][0]['title'], $polygons[0]['title']);
40+
$this->assertSame($array['polygons'][0]['infoWindow']['headerContent'], $polygons[0]['infoWindow']['headerContent']);
41+
$this->assertSame($array['polygons'][0]['infoWindow']['content'], $polygons[0]['infoWindow']['content']);
3542
}
3643

3744
public function testFromArrayWithInvalidCenter(): void
@@ -76,6 +83,30 @@ public function testFromArrayWithInvalidMarker(): void
7683
Map::fromArray($array);
7784
}
7885

86+
public function testFromArrayWithInvalidPolygons(): void
87+
{
88+
$array = self::createMapArray();
89+
$array['polygons'] = 'invalid';
90+
91+
$this->expectException(\InvalidArgumentException::class);
92+
$this->expectExceptionMessage('The "polygons" parameter must be an array.');
93+
Map::fromArray($array);
94+
}
95+
96+
public function testFromArrayWithInvalidPolygon(): void
97+
{
98+
$array = self::createMapArray();
99+
$array['polygons'] = [
100+
[
101+
'invalid',
102+
],
103+
];
104+
105+
$this->expectException(\InvalidArgumentException::class);
106+
$this->expectExceptionMessage('The "points" parameter is required.');
107+
Map::fromArray($array);
108+
}
109+
79110
private static function createMapArray(): array
80111
{
81112
return [
@@ -97,6 +128,29 @@ private static function createMapArray(): array
97128
],
98129
],
99130
],
131+
'polygons' => [
132+
[
133+
'points' => [
134+
[
135+
'lat' => 48.858844,
136+
'lng' => 2.294351,
137+
],
138+
[
139+
'lat' => 48.853,
140+
'lng' => 2.3499,
141+
],
142+
[
143+
'lat' => 48.8566,
144+
'lng' => 2.3522,
145+
],
146+
],
147+
'title' => 'Polygon 1',
148+
'infoWindow' => [
149+
'headerContent' => 'Polygon 1',
150+
'content' => 'Polygon 1',
151+
],
152+
],
153+
],
100154
];
101155
}
102156
}

0 commit comments

Comments
 (0)