|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PlinCode\KmlParser\Validators; |
| 4 | + |
| 5 | +use PlinCode\KmlParser\Enums\GeometryType; |
| 6 | +use PlinCode\KmlParser\Exceptions\KmlException; |
| 7 | +use SimpleXMLElement; |
| 8 | + |
| 9 | +class KmlValidator |
| 10 | +{ |
| 11 | + protected string $namespace = 'http://www.opengis.net/kml/2.2'; |
| 12 | + |
| 13 | + protected SimpleXMLElement $xml; |
| 14 | + |
| 15 | + public function validate(string $content): void |
| 16 | + { |
| 17 | + libxml_use_internal_errors(true); |
| 18 | + |
| 19 | + try { |
| 20 | + $this->xml = new SimpleXMLElement($content); |
| 21 | + |
| 22 | + $namespaces = $this->xml->getDocNamespaces(); |
| 23 | + if (! isset($namespaces['']) || $namespaces[''] !== $this->namespace) { |
| 24 | + throw new KmlException('Invalid or missing KML namespace'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->xml->registerXPathNamespace('kml', $this->namespace); |
| 28 | + |
| 29 | + if (empty($this->xml->Document)) { |
| 30 | + throw new KmlException('Missing required element: Document'); |
| 31 | + } |
| 32 | + |
| 33 | + $placemarks = $this->xml->xpath('//kml:Placemark'); |
| 34 | + if (! empty($placemarks)) { |
| 35 | + foreach ($placemarks as $placemark) { |
| 36 | + $this->validatePlacemark($placemark); |
| 37 | + } |
| 38 | + } |
| 39 | + } catch (KmlException $e) { |
| 40 | + throw $e; |
| 41 | + } catch (\Exception $e) { |
| 42 | + throw new KmlException('Invalid KML content: '.$e->getMessage()); |
| 43 | + } finally { |
| 44 | + libxml_clear_errors(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + protected function validatePlacemark(SimpleXMLElement $placemark): void |
| 49 | + { |
| 50 | + $hasGeometry = false; |
| 51 | + foreach (GeometryType::cases() as $type) { |
| 52 | + if ($placemark->{$type->value}) { |
| 53 | + $hasGeometry = true; |
| 54 | + $this->validateGeometryCoordinates($placemark->{$type->value}, $type->value); |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (! $hasGeometry) { |
| 60 | + throw new KmlException('Found Placemark without valid geometry'); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected function validateGeometryCoordinates(SimpleXMLElement $geometry, string $type): void |
| 65 | + { |
| 66 | + if ($type === 'Polygon') { |
| 67 | + if (empty($geometry->outerBoundaryIs->LinearRing->coordinates)) { |
| 68 | + throw new KmlException('Empty coordinates in Polygon geometry'); |
| 69 | + } |
| 70 | + $coordinates = (string) $geometry->outerBoundaryIs->LinearRing->coordinates; |
| 71 | + } else { |
| 72 | + if (empty($geometry->coordinates)) { |
| 73 | + throw new KmlException('Empty coordinates in geometry'); |
| 74 | + } |
| 75 | + $coordinates = (string) $geometry->coordinates; |
| 76 | + } |
| 77 | + |
| 78 | + if (empty(trim($coordinates))) { |
| 79 | + throw new KmlException('Empty coordinates in geometry'); |
| 80 | + } |
| 81 | + |
| 82 | + $coords = preg_split('/\s+/', trim($coordinates)); |
| 83 | + foreach ($coords as $coord) { |
| 84 | + if (empty(trim($coord))) { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + $parts = explode(',', trim($coord)); |
| 89 | + if (count($parts) < 2 || count($parts) > 3) { |
| 90 | + throw new KmlException('Invalid coordinate format'); |
| 91 | + } |
| 92 | + |
| 93 | + if (! is_numeric($parts[0]) || $parts[0] < -180 || $parts[0] > 180) { |
| 94 | + throw new KmlException("Invalid longitude value: {$parts[0]}"); |
| 95 | + } |
| 96 | + |
| 97 | + if (! is_numeric($parts[1]) || $parts[1] < -90 || $parts[1] > 90) { |
| 98 | + throw new KmlException("Invalid latitude value: {$parts[1]}"); |
| 99 | + } |
| 100 | + |
| 101 | + if (isset($parts[2]) && ! is_numeric($parts[2])) { |
| 102 | + throw new KmlException("Invalid altitude value: {$parts[2]}"); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments