From: Andy Street Date: Tue, 18 Dec 2018 16:04:20 +0000 (+0000) Subject: Only create lists as required X-Git-Url: https://git.street.me.uk/andy/gpx.git/commitdiff_plain/57c75891d94c6f465d366189b85586388004153e?hp=f528d2481e20c97020c2504ad9c2d5e2bc5ec653 Only create lists as required --- diff --git a/src/libgpx/gpx.php b/src/libgpx/gpx.php index 4a6e1ff..33fc479 100644 --- a/src/libgpx/gpx.php +++ b/src/libgpx/gpx.php @@ -106,18 +106,6 @@ class GPX implements Geographic */ protected $waypoints; - /** - * Create a new instance. - */ - public function __construct() - { - $this->links = new TypedDoublyLinkedList('libgpx\Link'); - $this->keywords = new TypedDoublyLinkedList('string'); - $this->metadataExtensions = new TypedDoublyLinkedList('string'); - $this->waypoints = new TypedDoublyLinkedList('libgpx\Point'); - } - - /** * Fetch the name of the program that created the GPX file. * @@ -226,10 +214,13 @@ class GPX implements Geographic /** * Fetch links to other resources. * + * @param boolean $create Create the list if it does not already exist. * @return TypedDoublyLinkedList */ - public function getLinks() + public function getLinks(bool $create = true) { + if ($create && $this->links === null) + $this->links = new TypedDoublyLinkedList('libgpx\Link'); return $this->links; } @@ -256,30 +247,39 @@ class GPX implements Geographic /** * Fetch a list of keywords for this GPX file. * + * @param boolean $create Create the list if it does not already exist. * @return TypedDoublyLinkedList The keywords */ - public function getKeywords() + public function getKeywords(bool $create = true) { + if ($create && $this->keywords === null) + $this->keywords = new TypedDoublyLinkedList('string'); return $this->keywords; } /** * Fetch a list of XML snippets describing unsupported metadata. * + * @param boolean $create Create the list if it does not already exist. * @return TypedDoublyLinkedList The keywords */ - public function getMetadataExtensions() + public function getMetadataExtensions(bool $create = true) { + if ($create && $this->metadataExtensions === null) + $this->metadataExtensions = new TypedDoublyLinkedList('string'); return $this->metadataExtensions; } /** * Fetch a list of waypoints. * + * @param boolean $create Create the list if it does not already exist. * @return TypedDoublyLinkedList A list of waypoints. */ - public function getWaypoints() + public function getWaypoints(bool $create = true) { + if ($create && $this->waypoints === null) + $this->waypoints = new TypedDoublyLinkedList('libgpx\Point'); return $this->waypoints; } diff --git a/src/libgpx/gpxwriter.php b/src/libgpx/gpxwriter.php index 7622fdb..710d027 100644 --- a/src/libgpx/gpxwriter.php +++ b/src/libgpx/gpxwriter.php @@ -233,8 +233,11 @@ class GPXWriter $namespace . ' ' . $schema ); $this->writeMetadata($gpx, $xml); - foreach ($gpx->getWaypoints() as $wpt) { - $this->writePoint($wpt, 'wpt', $xml); + $waypoints = $gpx->getWaypoints(false); + if ($waypoints !== null) { + foreach ($waypoints as $wpt) { + $this->writePoint($wpt, 'wpt', $xml); + } } $xml->endElement(); $xml->endDocument(); @@ -266,13 +269,17 @@ class GPXWriter if ($copyright instanceof Copyright) $this->writeCopyright($copyright, $xml); } - foreach($gpx->getLinks() as $link) { - $this->writeLink($link, $xml); - if ($this->format == '1.0') - break; + $links = $gpx->getLinks(false); + if ($links !== null) { + foreach($links as $link) { + $this->writeLink($link, $xml); + if ($this->format == '1.0') + break; + } } $xml->writeElement('time', $this->createTimestamp(new DateTime())); - if (!empty($gpx->getKeywords())) + $keywords = $gpx->getKeywords(false); + if ($keywords !== null && !$keywords->isEmpty()) $xml->writeElement( 'keywords', implode(', ', $gpx->getKeywords()->toArray()) @@ -287,8 +294,8 @@ class GPXWriter $xml->endElement(); } if ($this->format == '1.1') { - $extensions = $gpx->getMetadataExtensions(); - if (!$extensions->isEmpty()) { + $extensions = $gpx->getMetadataExtensions(false); + if ($extensions !== null && !$extensions->isEmpty()) { $xml->startElement('extensions'); foreach ($extensions as $extension) { $xml->writeRaw($extension);