Skip to content

Commit be968d0

Browse files
committed
Fix bug with colors in label/line styles, add utility functions for working with polygon coordinates
1 parent 34da770 commit be968d0

File tree

6 files changed

+38
-12
lines changed

6 files changed

+38
-12
lines changed

src/Model/Node/StyleNode.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function appendTo(\DOMDocument $document, \DOMElement $parent): void
159159
$styleElement->appendChild($labelStyle);
160160

161161
if (null !== $this->getLabelStyle()->getColor()) {
162-
$labelStyle->appendChild($document->createElement('Color', $this->getLabelStyle()->getColor()));
162+
$labelStyle->appendChild($document->createElement('color', $this->getLabelStyle()->getColor()));
163163
}
164164
if (null !== $this->getLabelStyle()->getScale()) {
165165
$labelStyle->appendChild($document->createElement('scale', (string) $this->getLabelStyle()->getScale()));
@@ -171,7 +171,7 @@ public function appendTo(\DOMDocument $document, \DOMElement $parent): void
171171
$styleElement->appendChild($lineStyle);
172172

173173
if (null !== $this->getLineStyle()->getColor()) {
174-
$lineStyle->appendChild($document->createElement('Color', $this->getLineStyle()->getColor()));
174+
$lineStyle->appendChild($document->createElement('color', $this->getLineStyle()->getColor()));
175175
}
176176
if (null !== $this->getLineStyle()->getWidth()) {
177177
$lineStyle->appendChild($document->createElement('Width', (string) $this->getLineStyle()->getWidth()));
@@ -183,7 +183,7 @@ public function appendTo(\DOMDocument $document, \DOMElement $parent): void
183183
$styleElement->appendChild($polyStyle);
184184

185185
if (null !== $this->getPolyStyle()->getColor()) {
186-
$polyStyle->appendChild($document->createElement('Color', $this->getPolyStyle()->getColor()));
186+
$polyStyle->appendChild($document->createElement('color', $this->getPolyStyle()->getColor()));
187187
}
188188
if (null !== $this->getPolyStyle()->getFill()) {
189189
$polyStyle->appendChild($document->createElement('fill', (string) $this->getPolyStyle()->getFill()));

src/Model/Polygon.php

+14
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,18 @@ public static function buildFromLinearRing(\SimpleXMLElement $ring): Polygon
135135

136136
return $polygon;
137137
}
138+
139+
/** @param array<array<int, float>> $coordinates */
140+
public function setCoordinatesFromFloatArray(array $coordinates): static
141+
{
142+
$this->coordinates = array_map(fn (array $coordinate) => new Coordinate($coordinate[0], $coordinate[1], $coordinate[2] ?? 0), $coordinates);
143+
144+
return $this;
145+
}
146+
147+
/** @return array<array<int, float>> */
148+
public function getCoordinatesAsFloatArray(): array
149+
{
150+
return array_map(fn (Coordinate $coordinate) => [$coordinate->getLatitude(), $coordinate->getLongitude(), $coordinate->getAltitude()], $this->coordinates);
151+
}
138152
}

src/Model/Style/LabelStyle.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public static function fromSimpleXmlElement(\SimpleXMLElement $node): ?self
4646
{
4747
$style = new self();
4848

49-
if (isset($node->Color)) {
50-
$style->setColor((string) $node->Color);
49+
if (isset($node->color)) {
50+
$style->setColor((string) $node->color);
5151
}
5252
if (isset($node->scale)) {
5353
$style->setScale((int) $node->scale);

src/Model/Style/LineStyle.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function setWidth(?int $width): static
4545
public static function fromSimpleXmlElement(\SimpleXMLElement $node): ?self
4646
{
4747
$style = new self();
48-
if (isset($node->Color)) {
49-
$style->setColor((string) $node->Color);
48+
if (isset($node->color)) {
49+
$style->setColor((string) $node->color);
5050
}
5151

5252
if (isset($node->width)) {

tests/Unit/PolygonTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public function testSettingInvalidCoordinates(): void
7474
$polygon->setCoordinatesFromString('1,2,3 4,5,6,7');
7575
}
7676

77+
public function testCreateFromFloatArray(): void
78+
{
79+
$polygon = new Polygon();
80+
$polygon->setCoordinatesFromFloatArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
81+
$this->assertEquals([
82+
new Coordinate(1, 2, 3),
83+
new Coordinate(4, 5, 6),
84+
new Coordinate(7, 8, 9),
85+
], $polygon->getCoordinates());
86+
$this->assertEquals([[1, 2, 3], [4, 5, 6], [7, 8, 9]], $polygon->getCoordinatesAsFloatArray());
87+
}
88+
7789
public function testCreateFromLinearRing(): void
7890
{
7991
$polygon = Polygon::buildFromLinearRing(new SimpleXMLElement(<<<EOT

tests/Unit/StyleNodeTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ public function testFactory(): void
135135
</Icon>
136136
</IconStyle>
137137
<LabelStyle>
138-
<Color>ff0000ff</Color>
138+
<color>ff0000ff</color>
139139
<scale>2</scale>
140140
</LabelStyle>
141141
<LineStyle>
142-
<Color>00ff00ff</Color>
142+
<color>00ff00ff</color>
143143
<width>1</width>
144144
</LineStyle>
145145
<PolyStyle>
@@ -202,11 +202,11 @@ public function testFactory(): void
202202
$this->assertStringContainsStringIgnoringCase('<h>32</h>', $xml);
203203

204204
$this->assertStringContainsStringIgnoringCase('<LabelStyle>', $xml);
205-
$this->assertStringContainsStringIgnoringCase('<Color>ff0000ff</Color>', $xml);
205+
$this->assertStringContainsStringIgnoringCase('<color>ff0000ff</color>', $xml);
206206
$this->assertStringContainsStringIgnoringCase('<scale>2</scale>', $xml);
207207

208208
$this->assertStringContainsStringIgnoringCase('<LineStyle>', $xml);
209-
$this->assertStringContainsStringIgnoringCase('<Color>00ff00ff</Color>', $xml);
209+
$this->assertStringContainsStringIgnoringCase('<color>00ff00ff</color>', $xml);
210210
$this->assertStringContainsStringIgnoringCase('<width>1</width>', $xml);
211211

212212
$this->assertStringContainsStringIgnoringCase('<PolyStyle>', $xml);
@@ -286,7 +286,7 @@ public function testLabelStyleWithMissingColor(): void
286286
$this->assertNotFalse($xml);
287287
$this->assertStringContainsStringIgnoringCase('<Style id="test">', $xml);
288288
$this->assertStringContainsStringIgnoringCase('<LabelStyle', $xml);
289-
$this->assertStringNotContainsStringIgnoringCase('<Color>', $xml);
289+
$this->assertStringNotContainsStringIgnoringCase('<color>', $xml);
290290
$this->assertStringContainsStringIgnoringCase('<scale>', $xml);
291291
}
292292

0 commit comments

Comments
 (0)