Skip to content
Merged
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
17 changes: 13 additions & 4 deletions src/KmlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function loadFromString(string $content): self
$this->xml->registerXPathNamespace('kml', $this->namespace);

return $this;
} catch (\Exception $e) {
} catch (Exception $e) {
libxml_clear_errors();
throw KmlParserException::failedToParse($e->getMessage());
}
Expand Down Expand Up @@ -95,7 +95,7 @@ public function getPlacemarks(): array

foreach ($placemarksXml as $placemarkXml) {
$placemark = [
'name' => (string) ($placemarkXml->name ?: $placemarkXml->n),
'name' => (string) $placemarkXml->name,
'description' => (string) $placemarkXml->description,
];

Expand Down Expand Up @@ -157,6 +157,17 @@ public function getStyles(): array

foreach ($stylesXml as $styleXml) {
$id = (string) $styleXml->attributes()->id;

/*
* A Style declared inline on a Placemark carries no id and cannot be
* referenced through a styleUrl. Keeping it here would make every
* anonymous style collide under the same empty key, so only shared
* styles end up in the returned map.
*/
if ($id === '') {
continue;
}

$style = [
'id' => $id,
];
Expand Down Expand Up @@ -341,8 +352,6 @@ public function getDocumentName(): ?string
$document = $this->xml->xpath('//kml:Document');
if (! empty($document) && isset($document[0]->name)) {
return (string) $document[0]->name;
} elseif (! empty($document) && isset($document[0]->n)) {
return (string) $document[0]->n;
}

return null;
Expand Down
69 changes: 69 additions & 0 deletions tests/StyleAndNameExtractionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

use PlinCode\KmlParser\KmlParser;

function kmlWithStyles(): string
{
return <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>0</name>
<Style id="shared-red">
<IconStyle>
<scale>1.2</scale>
</IconStyle>
</Style>
<Style id="shared-blue">
<IconStyle>
<scale>0.8</scale>
</IconStyle>
</Style>
<Placemark>
<name>0</name>
<Style>
<IconStyle>
<scale>3</scale>
</IconStyle>
</Style>
<Point>
<coordinates>7.7300965,45.8635629,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Second</name>
<Style>
<IconStyle>
<scale>4</scale>
</IconStyle>
</Style>
<Point>
<coordinates>8.1,45.1,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
XML;
}

beforeEach(function () {
$this->parser = (new KmlParser)->loadFromString(kmlWithStyles());
});

it('reads the placemark name from the name element', function () {
expect($this->parser->getPlacemarks()[0]['name'])->toBe('0');
});

it('reads the document name from the name element', function () {
expect($this->parser->getDocumentName())->toBe('0');
});

it('returns only styles that can be referenced by a styleUrl', function () {
$styles = $this->parser->getStyles();

expect($styles)->toHaveCount(2)
->and($styles)->toHaveKeys(['shared-red', 'shared-blue'])
->and($styles)->not->toHaveKey('')
->and($styles['shared-red']['iconStyle']['scale'])->toBe(1.2)
->and($styles['shared-blue']['iconStyle']['scale'])->toBe(0.8);
});
Loading