Skip to content

Commit 44432d7

Browse files
JeroenDeDauwclaude
andcommitted
Fix custom layer names that are purely numeric being dropped
array_merge renumbers integer-like array keys, so a custom Leaflet layer whose name is purely numeric (e.g. "1904") was silently dropped from the available-layers whitelist and never rendered. Use the union operator instead, which preserves such keys while keeping custom names taking precedence over a same-named stock layer. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b67f36e commit 44432d7

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

src/LeafletService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,10 @@ private function filterToAvailable( array $values, array $available ): array {
298298
* @return array<string, bool>
299299
*/
300300
private function availableWithDefinitions( array $available ): array {
301-
return array_merge(
302-
$available,
303-
array_fill_keys( $this->layerDefinitions->getLayerNames(), true )
304-
);
301+
// Union rather than array_merge: array_merge renumbers integer-like keys, which would drop
302+
// a custom layer whose name is purely numeric (e.g. "1904"). Custom names go on the left so
303+
// they take precedence over a same-named stock layer.
304+
return array_fill_keys( $this->layerDefinitions->getLayerNames(), true ) + $available;
305305
}
306306

307307
/**

tests/Unit/LeafletServiceTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ public function testCustomLayerDefinitionSurvivesFiltering() {
104104
);
105105
}
106106

107+
public function testNumericallyNamedCustomLayerSurvivesFiltering() {
108+
$mapData = $this->newLeafletMapData(
109+
[ 'layers' => [ 'OpenStreetMap', '1904' ] ],
110+
new LeafletLayerDefinitions( [
111+
'1904' => [ 'url' => 'https://tiles.example/{z}/{x}/{y}.png' ],
112+
] )
113+
);
114+
115+
$this->assertSame(
116+
[ 'OpenStreetMap', '1904' ],
117+
$mapData->getParameters()['layers']
118+
);
119+
}
120+
107121
public function testCustomOverlayDefinitionSurvivesFiltering() {
108122
$mapData = $this->newLeafletMapData(
109123
[ 'overlays' => [ 'OpenSeaMap', 'Historic', '<img src=x onerror="alert(1)">' ] ],

0 commit comments

Comments
 (0)