Skip to content

[Map] Add multipolygon support #2762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Map/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.26

- Add MultiPolygon support

## 2.25

- Downgrade PHP requirement from 8.3 to 8.1
Expand Down
6 changes: 4 additions & 2 deletions src/Map/src/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(
public function toArray(): array
{
return [
'points' => array_map(fn (Point $point) => $point->toArray(), $this->points),
'points' => array_map(fn ($pointOrList) => \is_array($pointOrList) ? array_map(fn (Point $point) => $point->toArray(), $pointOrList) : $pointOrList->toArray(), $this->points),
'title' => $this->title,
'infoWindow' => $this->infoWindow?->toArray(),
'extra' => $this->extra,
Expand All @@ -70,7 +70,9 @@ public static function fromArray(array $polygon): self
if (!isset($polygon['points'])) {
throw new InvalidArgumentException('The "points" parameter is required.');
}
$polygon['points'] = array_map(Point::fromArray(...), $polygon['points']);

$isSinglePoint = isset($polygon['points'][0]['lat'], $polygon['points'][0]['lng']);
$polygon['points'] = array_map(fn ($pointOrList) => $isSinglePoint ? Point::fromArray($pointOrList) : array_map(Point::fromArray(...), $pointOrList), $polygon['points']);

if (isset($polygon['infoWindow'])) {
$polygon['infoWindow'] = InfoWindow::fromArray($polygon['infoWindow']);
Expand Down
152 changes: 152 additions & 0 deletions src/Map/tests/PolygonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Map\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\UX\Map\Exception\InvalidArgumentException;
use Symfony\UX\Map\InfoWindow;
use Symfony\UX\Map\Point;
use Symfony\UX\Map\Polygon;

class PolygonTest extends TestCase
{
public function testToArray()
{
$point1 = new Point(1.1, 2.2);
$point2 = new Point(3.3, 4.4);

$infoWindow = new InfoWindow('info content');

$polygon = new Polygon(
points: [$point1, $point2],
title: 'Test Polygon',
infoWindow: $infoWindow,
extra: ['foo' => 'bar'],
id: 'poly1'
);

$array = $polygon->toArray();
$this->assertSame([
'points' => [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
'title' => 'Test Polygon',
'infoWindow' => [
'headerContent' => 'info content',
'content' => null,
'position' => null,
'opened' => false,
'autoClose' => true,
'extra' => $array['infoWindow']['extra'],
],
'extra' => ['foo' => 'bar'],
'id' => 'poly1',
], $array);
}

public function testToArrayMultidimensional()
{
$point1 = new Point(1.1, 2.2);
$point2 = new Point(3.3, 4.4);
$point3 = new Point(5.5, 6.6);

$polygon = new Polygon(
points: [[$point1, $point2], [$point3]],
);

$array = $polygon->toArray();
$this->assertSame([
'points' => [
[['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
[['lat' => 5.5, 'lng' => 6.6]],
],
'title' => null,
'infoWindow' => null,
'extra' => $array['extra'],
'id' => null,
], $array);
}

public function testFromArray()
{
$data = [
'points' => [
['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4],
],
'title' => 'Test Polygon',
'infoWindow' => ['content' => 'info content'],
'extra' => ['foo' => 'bar'],
'id' => 'poly1',
];

$polygon = Polygon::fromArray($data);

$this->assertInstanceOf(Polygon::class, $polygon);

$array = $polygon->toArray();
$this->assertSame([
'points' => [['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
'title' => 'Test Polygon',
'infoWindow' => [
'headerContent' => null,
'content' => 'info content',
'position' => null,
'opened' => false,
'autoClose' => true,
'extra' => $array['infoWindow']['extra'],
],
'extra' => ['foo' => 'bar'],
'id' => 'poly1',
], $array);
}

public function testFromArrayMultidimensional()
{
$data = [
'points' => [
[['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
[['lat' => 5.5, 'lng' => 6.6]],
],
'title' => 'Test Polygon',
'infoWindow' => ['content' => 'info content'],
'extra' => ['foo' => 'bar'],
'id' => 'poly1',
];

$polygon = Polygon::fromArray($data);

$this->assertInstanceOf(Polygon::class, $polygon);

$array = $polygon->toArray();
$this->assertSame([
'points' => [
[['lat' => 1.1, 'lng' => 2.2], ['lat' => 3.3, 'lng' => 4.4]],
[['lat' => 5.5, 'lng' => 6.6]],
],
'title' => 'Test Polygon',
'infoWindow' => [
'headerContent' => null,
'content' => 'info content',
'position' => null,
'opened' => false,
'autoClose' => true,
'extra' => $array['infoWindow']['extra'],
],
'extra' => ['foo' => 'bar'],
'id' => 'poly1',
], $array);
}

public function testFromArrayThrowsExceptionIfPointsMissing()
{
$this->expectException(InvalidArgumentException::class);
Polygon::fromArray(['invalid' => 'No points']);
}
}
Loading