Skip to content

Commit 4744d3d

Browse files
committed
[Map] Add "fromArray" methods to options and other DTO, make "*Array" methods internal
1 parent 8ab164b commit 4744d3d

12 files changed

+153
-20
lines changed

src/Map/src/Bridge/Google/src/GoogleOptions.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,41 @@ public function fullscreenControlOptions(FullscreenControlOptions $fullscreenCon
132132
return $this;
133133
}
134134

135+
/**
136+
* @internal
137+
*/
138+
public static function fromArray(array $array): self
139+
{
140+
$array += ['zoomControl' => false, 'mapTypeControl' => false, 'streetViewControl' => false, 'fullscreenControl' => false];
141+
142+
if (isset($array['zoomControlOptions'])) {
143+
$array['zoomControl'] = true;
144+
$array['zoomControlOptions'] = ZoomControlOptions::fromArray($array['zoomControlOptions']);
145+
}
146+
147+
if (isset($array['mapTypeControlOptions'])) {
148+
$array['mapTypeControl'] = true;
149+
$array['mapTypeControlOptions'] = MapTypeControlOptions::fromArray($array['mapTypeControlOptions']);
150+
}
151+
152+
if (isset($array['streetViewControlOptions'])) {
153+
$array['streetViewControl'] = true;
154+
$array['streetViewControlOptions'] = StreetViewControlOptions::fromArray($array['streetViewControlOptions']);
155+
}
156+
157+
if (isset($array['fullscreenControlOptions'])) {
158+
$array['fullscreenControl'] = true;
159+
$array['fullscreenControlOptions'] = FullscreenControlOptions::fromArray($array['fullscreenControlOptions']);
160+
}
161+
162+
$array['gestureHandling'] = GestureHandling::from($array['gestureHandling']);
163+
164+
return new self(...$array);
165+
}
166+
167+
/**
168+
* @internal
169+
*/
135170
public function toArray(): array
136171
{
137172
$array = [

src/Map/src/Bridge/Google/src/Option/FullscreenControlOptions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public function __construct(
2525
) {
2626
}
2727

28+
/**
29+
* @internal
30+
*/
31+
public static function fromArray(array $array): self
32+
{
33+
return new self(
34+
position: ControlPosition::from($array['position']),
35+
);
36+
}
37+
38+
/**
39+
* @internal
40+
*/
2841
public function toArray(): array
2942
{
3043
return [

src/Map/src/Bridge/Google/src/Option/MapTypeControlOptions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ public function __construct(
3030
) {
3131
}
3232

33+
/**
34+
* @internal
35+
*/
36+
public static function fromArray(array $array): self
37+
{
38+
return new self(
39+
mapTypeIds: $array['mapTypeIds'],
40+
position: ControlPosition::from($array['position']),
41+
style: MapTypeControlStyle::from($array['style']),
42+
);
43+
}
44+
45+
/**
46+
* @internal
47+
*/
3348
public function toArray(): array
3449
{
3550
return [

src/Map/src/Bridge/Google/src/Option/StreetViewControlOptions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public function __construct(
2525
) {
2626
}
2727

28+
/**
29+
* @internal
30+
*/
31+
public static function fromArray(array $array): self
32+
{
33+
return new self(
34+
position: ControlPosition::from($array['position']),
35+
);
36+
}
37+
38+
/**
39+
* @internal
40+
*/
2841
public function toArray(): array
2942
{
3043
return [

src/Map/src/Bridge/Google/src/Option/ZoomControlOptions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@ public function __construct(
2525
) {
2626
}
2727

28+
/**
29+
* @internal
30+
*/
31+
public static function fromArray(array $array): self
32+
{
33+
return new self(
34+
position: ControlPosition::from($array['position']),
35+
);
36+
}
37+
38+
/**
39+
* @internal
40+
*/
2841
public function toArray(): array
2942
{
3043
return [

src/Map/src/Bridge/Google/tests/GoogleOptionsTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ public function testWithMinimalConfiguration(): void
4343
'position' => ControlPosition::INLINE_END_BLOCK_START->value,
4444
],
4545
], $options->toArray());
46+
47+
self::assertEquals($options, GoogleOptions::fromArray($options->toArray()));
4648
}
4749

4850
public function testWithMinimalConfigurationAndWithoutControls(): void
4951
{
5052
$options = new GoogleOptions(
51-
mapId: '2b2d73ba4b8c7b41',
53+
mapId: 'abcdefgh12345678',
5254
gestureHandling: GestureHandling::GREEDY,
5355
backgroundColor: '#f00',
5456
disableDoubleClickZoom: true,
@@ -59,10 +61,12 @@ public function testWithMinimalConfigurationAndWithoutControls(): void
5961
);
6062

6163
self::assertSame([
62-
'mapId' => '2b2d73ba4b8c7b41',
64+
'mapId' => 'abcdefgh12345678',
6365
'gestureHandling' => GestureHandling::GREEDY->value,
6466
'backgroundColor' => '#f00',
6567
'disableDoubleClickZoom' => true,
6668
], $options->toArray());
69+
70+
self::assertEquals($options, GoogleOptions::fromArray($options->toArray()));
6771
}
6872
}

src/Map/src/Bridge/Leaflet/src/LeafletOptions.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public function tileLayer(TileLayer $tileLayer): self
3434
return $this;
3535
}
3636

37+
/**
38+
* @internal
39+
*/
40+
public static function fromArray(array $array): MapOptionsInterface
41+
{
42+
return new self(
43+
tileLayer: TileLayer::fromArray($array['tileLayer']),
44+
);
45+
}
46+
47+
/**
48+
* @internal
49+
*/
3750
public function toArray(): array
3851
{
3952
return [

src/Map/src/Bridge/Leaflet/src/Option/TileLayer.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,27 @@ public function __construct(
3030
) {
3131
}
3232

33+
/**
34+
* @internal
35+
*/
36+
public static function fromArray(array $array): self
37+
{
38+
return new self(
39+
url: $array['url'],
40+
attribution: $array['attribution'],
41+
options: $array['options'],
42+
);
43+
}
44+
45+
/**
46+
* @internal
47+
*/
3348
public function toArray(): array
3449
{
3550
return [
3651
'url' => $this->url,
3752
'attribution' => $this->attribution,
38-
'options' => (object) $this->options,
53+
'options' => $this->options,
3954
];
4055
}
4156
}

src/Map/src/Bridge/Leaflet/tests/LeafletOptionsTest.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public function testWithMinimalConfiguration(): void
2121
{
2222
$leafletOptions = new LeafletOptions();
2323

24-
$array = $leafletOptions->toArray();
25-
2624
self::assertSame([
2725
'tileLayer' => [
2826
'url' => 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
2927
'attribution' => '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
30-
'options' => $array['tileLayer']['options'], // stdClass
28+
'options' => [],
3129
],
32-
], $array);
30+
], $leafletOptions->toArray());
31+
32+
self::assertEquals($leafletOptions, LeafletOptions::fromArray($leafletOptions->toArray()));
3333
}
3434

3535
public function testWithMaximumConfiguration(): void
@@ -47,18 +47,19 @@ public function testWithMaximumConfiguration(): void
4747
),
4848
);
4949

50-
$array = $leafletOptions->toArray();
51-
5250
self::assertSame([
5351
'tileLayer' => [
5452
'url' => 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
5553
'attribution' => '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
56-
'options' => $array['tileLayer']['options'], // stdClass
54+
'options' => [
55+
'maxZoom' => 19,
56+
'minZoom' => 1,
57+
'maxNativeZoom' => 18,
58+
'zoomOffset' => 0,
59+
],
5760
],
58-
], $array);
59-
self::assertSame(19, $array['tileLayer']['options']->maxZoom);
60-
self::assertSame(1, $array['tileLayer']['options']->minZoom);
61-
self::assertSame(18, $array['tileLayer']['options']->maxNativeZoom);
62-
self::assertSame(0, $array['tileLayer']['options']->zoomOffset);
61+
], $leafletOptions->toArray());
62+
63+
self::assertEquals($leafletOptions, LeafletOptions::fromArray($leafletOptions->toArray()));
6364
}
6465
}

src/Map/src/Bridge/Leaflet/tests/Option/TileLayerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public function testToArray()
2626
],
2727
);
2828

29-
$array = $tileLayer->toArray();
30-
3129
self::assertSame([
3230
'url' => 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
3331
'attribution' => '© OpenStreetMap contributors',
34-
'options' => $array['options'], // stdClass
35-
], $array);
36-
self::assertSame(19, $array['options']->maxZoom);
32+
'options' => [
33+
'maxZoom' => 19,
34+
],
35+
], $tileLayer->toArray());
36+
self::assertEquals(TileLayer::fromArray($tileLayer->toArray()), $tileLayer);
3737
}
3838
}

0 commit comments

Comments
 (0)