Skip to content

Commit 55f536b

Browse files
author
Danny van Wijk
committed
Map add multipolygon support
1 parent 5665746 commit 55f536b

File tree

6 files changed

+189
-7
lines changed

6 files changed

+189
-7
lines changed

src/Map/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 2.26
4+
5+
- Add MultiPolygon support. A MultiPolygon allows you to represent multiple polygons (possibly with holes or separate areas) as a single map element. See documentation for usage.
6+
37
## 2.25
48

59
- Downgrade PHP requirement from 8.3 to 8.1

src/Map/assets/dist/abstract_map_controller.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type MarkerDefinition<MarkerOptions, InfoWindowOptions> = WithIdentifier<
3636
}>;
3737
export type PolygonDefinition<PolygonOptions, InfoWindowOptions> = WithIdentifier<{
3838
infoWindow?: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
39-
points: Array<Point>;
39+
points: Array<Point> | Array<Array<Point>>;
4040
title: string | null;
4141
rawOptions?: PolygonOptions;
4242
extra: Record<string, unknown>;

src/Map/assets/src/abstract_map_controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type MarkerDefinition<MarkerOptions, InfoWindowOptions> = WithIdentifier<
4848

4949
export type PolygonDefinition<PolygonOptions, InfoWindowOptions> = WithIdentifier<{
5050
infoWindow?: InfoWindowWithoutPositionDefinition<InfoWindowOptions>;
51-
points: Array<Point>;
51+
points: Array<Point> | Array<Array<Point>>;
5252
title: string | null;
5353
/**
5454
* Raw options passed to the marker constructor, specific to the map provider (e.g.: `L.marker()` for Leaflet).

src/Map/doc/index.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ You can also add Polygons, which represents an area enclosed by a series of ``Po
170170
),
171171
));
172172

173+
.. versionadded:: 2.26
174+
175+
``MultiPolygon`` is available since UX Map 2.26.
176+
177+
You can also add a MultiPolygon. A MultiPolygon allows you to represent multiple polygons (possibly with holes or separate areas) as a single map element::
178+
179+
$myMap->addPolygon(new Polygon(
180+
points: [
181+
[
182+
new Point(-180, 90),
183+
new Point(180, 90),
184+
new Point(180, -90),
185+
new Point(-180, -90),
186+
],
187+
[
188+
new Point(48.8566, 2.3522),
189+
new Point(45.7640, 4.8357),
190+
new Point(43.2965, 5.3698),
191+
new Point(44.8378, -0.5792),
192+
]
193+
],
194+
));
195+
173196
Add Polylines
174197
~~~~~~~~~~~~~
175198

src/Map/src/Polygon.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
final class Polygon implements Element
2222
{
2323
/**
24-
* @param array<string, mixed> $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side
24+
* @param array<array<Point>|array<array<Point>>> $points Points of the polygon. Each point can be a single Point or an array of Points.
25+
* @param array<string, mixed> $extra Extra data, can be used by the developer to store additional information and use them later JavaScript side
2526
*/
2627
public function __construct(
2728
private readonly array $points,
@@ -36,7 +37,7 @@ public function __construct(
3637
* Convert the polygon to an array representation.
3738
*
3839
* @return array{
39-
* points: array<array{lat: float, lng: float}>,
40+
* points: array<array{lat: float, lng: float}|array<array{lat: float, lng: float}>>,
4041
* title: string|null,
4142
* infoWindow: array<string, mixed>|null,
4243
* extra: array,
@@ -46,7 +47,7 @@ public function __construct(
4647
public function toArray(): array
4748
{
4849
return [
49-
'points' => array_map(fn (Point $point) => $point->toArray(), $this->points),
50+
'points' => array_map(fn ($pointOrList) => \is_array($pointOrList) ? array_map(fn (Point $point) => $point->toArray(), $pointOrList) : $pointOrList->toArray(), $this->points),
5051
'title' => $this->title,
5152
'infoWindow' => $this->infoWindow?->toArray(),
5253
'extra' => $this->extra,
@@ -56,7 +57,7 @@ public function toArray(): array
5657

5758
/**
5859
* @param array{
59-
* points: array<array{lat: float, lng: float}>,
60+
* points: array<array{lat: float, lng: float}|array<array{lat: float, lng: float}>>,
6061
* title: string|null,
6162
* infoWindow: array<string, mixed>|null,
6263
* extra: array,
@@ -70,7 +71,9 @@ public static function fromArray(array $polygon): self
7071
if (!isset($polygon['points'])) {
7172
throw new InvalidArgumentException('The "points" parameter is required.');
7273
}
73-
$polygon['points'] = array_map(Point::fromArray(...), $polygon['points']);
74+
75+
$isSinglePoint = isset($polygon['points'][0]['lat'], $polygon['points'][0]['lng']);
76+
$polygon['points'] = array_map(fn ($pointOrList) => $isSinglePoint ? Point::fromArray($pointOrList) : array_map(Point::fromArray(...), $pointOrList), $polygon['points']);
7477

7578
if (isset($polygon['infoWindow'])) {
7679
$polygon['infoWindow'] = InfoWindow::fromArray($polygon['infoWindow']);

src/Map/tests/PolygonTest.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\UX\Map\Exception\InvalidArgumentException;
16+
use Symfony\UX\Map\InfoWindow;
17+
use Symfony\UX\Map\Point;
18+
use Symfony\UX\Map\Polygon;
19+
20+
class PolygonTest extends TestCase
21+
{
22+
public function testToArray()
23+
{
24+
$point1 = new Point(1.1, 2.2);
25+
$point2 = new Point(3.3, 4.4);
26+
27+
$infoWindow = new InfoWindow('info content');
28+
29+
$polygon = new Polygon(
30+
points: [$point1, $point2],
31+
title: 'Test Polygon',
32+
infoWindow: $infoWindow,
33+
extra: ['foo' => 'bar'],
34+
id: 'poly1'
35+
);
36+
37+
$array = $polygon->toArray();
38+
$this->assertSame([
39+
'points' => [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
40+
'title' => 'Test Polygon',
41+
'infoWindow' => [
42+
'headerContent' => 'info content',
43+
'content' => null,
44+
'position' => null,
45+
'opened' => false,
46+
'autoClose' => true,
47+
'extra' => $array['infoWindow']['extra'],
48+
],
49+
'extra' => ['foo' => 'bar'],
50+
'id' => 'poly1',
51+
], $array);
52+
}
53+
54+
public function testToArrayMultidimensional()
55+
{
56+
$point1 = new Point(1.1, 2.2);
57+
$point2 = new Point(3.3, 4.4);
58+
$point3 = new Point(5.5, 6.6);
59+
60+
$polygon = new Polygon(
61+
points: [[$point1, $point2], [$point3]],
62+
);
63+
64+
$array = $polygon->toArray();
65+
$this->assertSame([
66+
'points' => [
67+
[['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
68+
[['lat' => 5.5, 'lng' => 6.6]],
69+
],
70+
'title' => null,
71+
'infoWindow' => null,
72+
'extra' => $array['extra'],
73+
'id' => null,
74+
], $array);
75+
}
76+
77+
public function testFromArray()
78+
{
79+
$data = [
80+
'points' => [
81+
['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4],
82+
],
83+
'title' => 'Test Polygon',
84+
'infoWindow' => ['content' => 'info content'],
85+
'extra' => ['foo' => 'bar'],
86+
'id' => 'poly1',
87+
];
88+
89+
$polygon = Polygon::fromArray($data);
90+
91+
$this->assertInstanceOf(Polygon::class, $polygon);
92+
93+
$array = $polygon->toArray();
94+
$this->assertSame([
95+
'points' => [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
96+
'title' => 'Test Polygon',
97+
'infoWindow' => [
98+
'headerContent' => null,
99+
'content' => 'info content',
100+
'position' => null,
101+
'opened' => false,
102+
'autoClose' => true,
103+
'extra' => $array['infoWindow']['extra'],
104+
],
105+
'extra' => ['foo' => 'bar'],
106+
'id' => 'poly1',
107+
], $array);
108+
}
109+
110+
public function testFromArrayMultidimensional()
111+
{
112+
$data = [
113+
'points' => [
114+
[['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
115+
[['lat' => 5.5, 'lng' => 6.6]],
116+
],
117+
'title' => 'Test Polygon',
118+
'infoWindow' => ['content' => 'info content'],
119+
'extra' => ['foo' => 'bar'],
120+
'id' => 'poly1',
121+
];
122+
123+
$polygon = Polygon::fromArray($data);
124+
125+
$this->assertInstanceOf(Polygon::class, $polygon);
126+
127+
$array = $polygon->toArray();
128+
$this->assertSame([
129+
'points' => [
130+
[['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
131+
[['lat' => 5.5, 'lng' => 6.6]],
132+
],
133+
'title' => 'Test Polygon',
134+
'infoWindow' => [
135+
'headerContent' => null,
136+
'content' => 'info content',
137+
'position' => null,
138+
'opened' => false,
139+
'autoClose' => true,
140+
'extra' => $array['infoWindow']['extra'],
141+
],
142+
'extra' => ['foo' => 'bar'],
143+
'id' => 'poly1',
144+
], $array);
145+
}
146+
147+
public function testFromArrayThrowsExceptionIfPointsMissing()
148+
{
149+
$this->expectException(InvalidArgumentException::class);
150+
Polygon::fromArray(['invalid' => 'No points']);
151+
}
152+
}

0 commit comments

Comments
 (0)