Skip to content

Commit 6f614dd

Browse files
author
Danny van Wijk
committed
Map add multipolygon support
1 parent 5665746 commit 6f614dd

File tree

3 files changed

+160
-2
lines changed

3 files changed

+160
-2
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
6+
37
## 2.25
48

59
- Downgrade PHP requirement from 8.3 to 8.1

src/Map/src/Polygon.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct(
4646
public function toArray(): array
4747
{
4848
return [
49-
'points' => array_map(fn (Point $point) => $point->toArray(), $this->points),
49+
'points' => array_map(fn ($pointOrList) => is_array($pointOrList) ? array_map(fn (Point $point) => $point->toArray(), $pointOrList) : $pointOrList->toArray(), $this->points),
5050
'title' => $this->title,
5151
'infoWindow' => $this->infoWindow?->toArray(),
5252
'extra' => $this->extra,
@@ -70,7 +70,9 @@ public static function fromArray(array $polygon): self
7070
if (!isset($polygon['points'])) {
7171
throw new InvalidArgumentException('The "points" parameter is required.');
7272
}
73-
$polygon['points'] = array_map(Point::fromArray(...), $polygon['points']);
73+
74+
$isSinglePoint = isset($polygon['points'][0]['lat'], $polygon['points'][0]['lng']);
75+
$polygon['points'] = array_map(fn ($pointOrList) => $isSinglePoint ? Point::fromArray($pointOrList) : array_map(Point::fromArray(...), $pointOrList), $polygon['points']);
7476

7577
if (isset($polygon['infoWindow'])) {
7678
$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)