From e0c26559f3012d356503bfe68ebc1fecd6f4b552 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Wed, 19 Dec 2018 18:48:47 +0000 Subject: [PATCH] Add extensions for GPX type --- docs/samples/full-1.0.gpx | 1 + docs/samples/full-1.1.gpx | 3 +++ src/libgpx/gpx.php | 20 ++++++++++++++++++++ src/libgpx/gpxreader.php | 7 +++++++ src/libgpx/gpxwriter.php | 10 ++++++++++ 5 files changed, 41 insertions(+) diff --git a/docs/samples/full-1.0.gpx b/docs/samples/full-1.0.gpx index ab9c5f6..86381c5 100644 --- a/docs/samples/full-1.0.gpx +++ b/docs/samples/full-1.0.gpx @@ -61,4 +61,5 @@ + Jane Doe diff --git a/docs/samples/full-1.1.gpx b/docs/samples/full-1.1.gpx index 414a45c..a061ed8 100644 --- a/docs/samples/full-1.1.gpx +++ b/docs/samples/full-1.1.gpx @@ -95,4 +95,7 @@ + + Jane Doe + diff --git a/src/libgpx/gpx.php b/src/libgpx/gpx.php index f71f7f1..61e9e58 100644 --- a/src/libgpx/gpx.php +++ b/src/libgpx/gpx.php @@ -120,6 +120,13 @@ class GPX implements Geographic */ protected $tracks; + /** + * A list of XML snippets describing unsupported elements. + * + * @var TypedDoublyLinkedList (string) + */ + protected $extensions; + /** * Fetch the name of the program that created the GPX file. * @@ -323,6 +330,19 @@ class GPX implements Geographic return $this->tracks; } + /** + * Fetch a list of protocol extensions as raw XML. + * + * @param boolean $create Create the list if it does not already exist. + * @return TypedDoublyLinkedList A list of protocol extensions. + */ + public function getExtensions(bool $create = true) + { + if ($create && $this->extensions === null) + $this->extensions = new TypedDoublyLinkedList('string'); + return $this->extensions; + } + /** * Fetch a bounding box that covers the feature. * diff --git a/src/libgpx/gpxreader.php b/src/libgpx/gpxreader.php index 4df8d1d..8e9c615 100644 --- a/src/libgpx/gpxreader.php +++ b/src/libgpx/gpxreader.php @@ -193,6 +193,9 @@ class GPXReader $struct['elements']['metadata'] = function ($gpx) { $this->readMetadata($gpx); }; + $struct['elements']['extensions'] = function ($gpx) { + $this->readExtension($gpx->getExtensions()); + }; } else { $struct['elements']['name'] = function ($gpx) { $gpx->setName($this->readXSDString()); @@ -249,6 +252,10 @@ class GPXReader $keywords[] = trim($word); }; $struct['elements']['bounds'] = false; + $struct['extensions'] = function ($gpx) { + $gpx->getExtensions()[] = $this->xml->readOuterXML(); + $this->xml->next(); + }; } $this->readStruct($struct, $result); diff --git a/src/libgpx/gpxwriter.php b/src/libgpx/gpxwriter.php index 1e7878c..567a771 100644 --- a/src/libgpx/gpxwriter.php +++ b/src/libgpx/gpxwriter.php @@ -251,6 +251,16 @@ class GPXWriter $this->writeTrack($track, $xml); } } + $extensions = $gpx->getExtensions(false); + if ($extensions !== null && !$extensions->isEmpty()) { + if ($this->format == '1.1') + $xml->startElement('extensions'); + foreach ($extensions as $extension) { + $xml->writeRaw($extension); + } + if ($this->format == '1.1') + $xml->endElement(); + } $xml->endElement(); $xml->endDocument(); $xml->flush(); -- 2.39.5