diff --git a/src/KmlParser.php b/src/KmlParser.php index aced1e4..1061449 100755 --- a/src/KmlParser.php +++ b/src/KmlParser.php @@ -6,12 +6,14 @@ use PlinCode\KmlParser\Enums\GeometryType; use PlinCode\KmlParser\Exceptions\KmlParserException; use PlinCode\KmlParser\Traits\ParsesCoordinates; +use PlinCode\KmlParser\Traits\ReadsPackageConfig; use PlinCode\KmlParser\Validators\KmlValidator; use SimpleXMLElement; class KmlParser { use ParsesCoordinates; + use ReadsPackageConfig; protected ?SimpleXMLElement $xml = null; @@ -21,7 +23,7 @@ class KmlParser public function __construct() { - $this->namespace = config('kml-parser.namespace', $this->namespace); + $this->namespace = $this->packageConfig('kml-parser.namespace', $this->namespace); $this->validator = new KmlValidator($this->supportedNamespaces()); } @@ -33,7 +35,7 @@ public function __construct() */ protected function supportedNamespaces(): array { - $supported = config('kml-parser.supported_namespaces', KmlValidator::DEFAULT_NAMESPACES); + $supported = $this->packageConfig('kml-parser.supported_namespaces', KmlValidator::DEFAULT_NAMESPACES); return array_values(array_unique(array_merge([$this->namespace], (array) $supported))); } diff --git a/src/Traits/ReadsPackageConfig.php b/src/Traits/ReadsPackageConfig.php new file mode 100644 index 0000000..2110513 --- /dev/null +++ b/src/Traits/ReadsPackageConfig.php @@ -0,0 +1,24 @@ +bound('config')) { + return $default; + } + + return config($key, $default); + } +} diff --git a/tests/StandaloneUsageTest.php b/tests/StandaloneUsageTest.php new file mode 100644 index 0000000..9fb60d4 --- /dev/null +++ b/tests/StandaloneUsageTest.php @@ -0,0 +1,81 @@ + + + + Standalone + + Lago Blu + + 7.7300965,45.8635629,0 + + + + +XML; + +it('can be constructed with no config repository bound', function () { + expect(withoutBoundConfig(fn () => new KmlParser))->toBeInstanceOf(KmlParser::class); +}); + +it('parses with no config repository bound', function () use ($kml) { + $placemarks = withoutBoundConfig(fn () => (new KmlParser)->loadFromString($kml)->getPlacemarks()); + + expect($placemarks)->toHaveCount(1) + ->and($placemarks[0]['name'])->toBe('Lago Blu'); +}); + +it('falls back to the default namespaces with no config repository bound', function () { + $legacy = <<<'XML' + + + + + + 7.7,45.8,0 + + + + +XML; + + $placemarks = withoutBoundConfig(fn () => (new KmlParser)->loadFromString($legacy)->getPlacemarks()); + + expect($placemarks)->toHaveCount(1); +}); + +it('still reads the config when the application provides one', function () { + config()->set('kml-parser.supported_namespaces', ['http://example.test/kml']); + + $kml = str_replace('http://www.opengis.net/kml/2.2', 'http://example.test/kml', <<<'XML' + + + + + + 7.7,45.8,0 + + + + +XML); + + expect((new KmlParser)->loadFromString($kml)->getPlacemarks())->toHaveCount(1); +});