]> git.street.me.uk Git - andy/gpx.git/commitdiff
Only create lists as required
authorAndy Street <andy@street.me.uk>
Tue, 18 Dec 2018 16:04:20 +0000 (16:04 +0000)
committerAndy Street <andy@street.me.uk>
Tue, 18 Dec 2018 16:04:20 +0000 (16:04 +0000)
src/libgpx/gpx.php
src/libgpx/gpxwriter.php

index 4a6e1ffe878394a99719f4ac49d83f69e88e34b7..33fc47978930c1ee7c0bb13223a771735863d7ee 100644 (file)
@@ -106,18 +106,6 @@ class GPX implements Geographic
    */
   protected $waypoints;
 
    */
   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.
    *
   /**
    * Fetch the name of the program that created the GPX file.
    *
@@ -226,10 +214,13 @@ class GPX implements Geographic
   /**
    * Fetch links to other resources.
    *
   /**
    * Fetch links to other resources.
    *
+   * @param boolean $create Create the list if it does not already exist.
    * @return TypedDoublyLinkedList
    */
    * @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;
   }
 
     return $this->links;
   }
 
@@ -256,30 +247,39 @@ class GPX implements Geographic
   /**
    * Fetch a list of keywords for this GPX file.
    *
   /**
    * 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
    */
    * @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.
    *
     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
    */
    * @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.
    *
     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.
    */
    * @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;
   }
 
     return $this->waypoints;
   }
 
index 7622fdb9586e62ae5332e47a193fadfdb6726fe4..710d02764f5b813518e881588741abe5f64f4c65 100644 (file)
@@ -233,8 +233,11 @@ class GPXWriter
       $namespace . ' ' . $schema
     );
     $this->writeMetadata($gpx, $xml);
       $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();
     }
     $xml->endElement();
     $xml->endDocument();
@@ -266,13 +269,17 @@ class GPXWriter
       if ($copyright instanceof Copyright)
         $this->writeCopyright($copyright, $xml);
     }
       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()));
     }
     $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())
       $xml->writeElement(
         'keywords',
         implode(', ', $gpx->getKeywords()->toArray())
@@ -287,8 +294,8 @@ class GPXWriter
       $xml->endElement();
     }
     if ($this->format == '1.1') {
       $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);
         $xml->startElement('extensions');
         foreach ($extensions as $extension) {
           $xml->writeRaw($extension);