]> git.street.me.uk Git - andy/gpx.git/blobdiff - src/libgpx/gpx.php
Add extensions for GPX type
[andy/gpx.git] / src / libgpx / gpx.php
index 87e0b9055b77b147a20470407c82067878607f19..61e9e58db098aeb26e4636026bd387a88b1b2624 100644 (file)
@@ -74,9 +74,9 @@ class GPX implements Geographic
   /**
    * Links to other resources that describe this GPX file.
    *
-   * @var Link[]
+   * @var TypedDoublyLinkedList (Link)
    */
-  protected $links = [];
+  protected $links;
 
   /**
    * The creation time of the file.
@@ -88,9 +88,44 @@ class GPX implements Geographic
   /**
    * A list of keywords classifying this GPX file.
    *
-   * @var string[]
+   * @var TypedDoublyLinkedList (string)
    */
-  protected $keywords = [];
+  protected $keywords;
+
+  /**
+   * A list of XML snippets describing unsupported metadata.
+   *
+   * @var TypedDoublyLinkedList (string)
+   */
+  protected $metadataExtensions;
+
+  /**
+   * A list of waypoints.
+   *
+   * @var TypedDoublyLinkedList (Point)
+   */
+  protected $waypoints;
+
+  /**
+   * A list of routes.
+   *
+   * @var TypedDoublyLinkedList (Route)
+   */
+  protected $routes;
+
+  /**
+   * A list of tracks.
+   *
+   * @var TypedDoublyLinkedList (Track)
+   */
+  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.
@@ -200,91 +235,112 @@ class GPX implements Geographic
   /**
    * Fetch links to other resources.
    *
-   * @return Link[]
+   * @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;
   }
 
   /**
-   * Add a link to an related resource.
+   * Fetch the time the GPX file was created.
    *
-   * @param Link $link The link to add.
-   * @return void
+   * @return DateTimeInterface|null A timestamp or null if not set.
+   */
+  public function getTime()
+  {
+    return $this->time;
+  }
+
+  /**
+   * Set the time at which the file was created.
+   *
+   * @param DateTimeInterface $time The creation time or null to delete.
    */
-  public function addLink(Link $link)
+  public function setTime(DateTimeInterface $time = null)
   {
-    if (!in_array($link, $this->links, true))
-      $this->links[] = $link;
+    $this->time = $time;
   }
 
   /**
-   * Remove an existing link from the list.
+   * Fetch a list of keywords for this GPX file.
    *
-   * @param Link $link The link to remove.
-   * @return void
+   * @param boolean $create Create the list if it does not already exist.
+   * @return TypedDoublyLinkedList The keywords
    */
-  public function removeLink(Link $link)
+  public function getKeywords(bool $create = true)
   {
-    $key = array_search($link, $this->links, true);
-    if ($key !== null)
-      unset($this->links[$key]);
+    if ($create && $this->keywords === null)
+      $this->keywords = new TypedDoublyLinkedList('string');
+    return $this->keywords;
   }
 
   /**
-   * Fetch the time the GPX file was created.
+   * Fetch a list of XML snippets describing unsupported metadata.
    *
-   * @return DateTimeInterface|null A timestamp or null if not set.
+   * @param boolean $create Create the list if it does not already exist.
+   * @return TypedDoublyLinkedList The keywords
    */
-  public function getTime()
+  public function getMetadataExtensions(bool $create = true)
   {
-    return $this->time;
+    if ($create && $this->metadataExtensions === null)
+        $this->metadataExtensions = new TypedDoublyLinkedList('string');
+    return $this->metadataExtensions;
   }
 
   /**
-   * Set the time at which the file was created.
+   * Fetch a list of waypoints.
    *
-   * @param DateTimeInterface $time The creation time or null to delete.
+   * @param boolean $create Create the list if it does not already exist.
+   * @return TypedDoublyLinkedList A list of waypoints.
    */
-  public function setTime(DateTimeInterface $time = null)
+  public function getWaypoints(bool $create = true)
   {
-    $this->time = $time;
+    if ($create && $this->waypoints === null)
+      $this->waypoints = new TypedDoublyLinkedList('libgpx\Point');
+    return $this->waypoints;
   }
 
   /**
-   * Fetch a list of keywords for this GPX file.
+   * Fetch a list of routes.
    *
-   * @return string[] The keywords
+   * @param boolean $create Create the list if it does not already exist.
+   * @return TypedDoublyLinkedList A list of routes.
    */
-  public function getKeywords()
+  public function getRoutes(bool $create = true)
   {
-    return $this->keywords;
+    if ($create && $this->routes === null)
+      $this->routes = new TypedDoublyLinkedList('libgpx\Route');
+    return $this->routes;
   }
 
   /**
-   * Add a keyword for this GPX file.
+   * Fetch a list of tracks.
    *
-   * @param string $keyword The keyword to add.
-   * @return void
+   * @param boolean $create Create the list if it does not already exist.
+   * @return TypedDoublyLinkedList A list of tracks.
    */
-  public function addKeyword(string $keyword)
+  public function getTracks(bool $create = true)
   {
-    if (!in_array($keyword, $this->keywords))
-      $this->keywords[] = $keyword;
+    if ($create && $this->tracks === null)
+      $this->tracks = new TypedDoublyLinkedList('libgpx\Track');
+    return $this->tracks;
   }
 
   /**
-   * Remove an existing keyword from the list.
+   * Fetch a list of protocol extensions as raw XML.
    *
-   * @param string $keyword The keyword to remove.
-   * @return void
+   * @param boolean $create Create the list if it does not already exist.
+   * @return TypedDoublyLinkedList A list of protocol extensions.
    */
-  public function removeKeyword(string $keyword)
+  public function getExtensions(bool $create = true)
   {
-    $key = array_search($keyword, $this->keywords);
-    if ($key !== null)
-      unset($this->keywords[$key]);
+    if ($create && $this->extensions === null)
+      $this->extensions = new TypedDoublyLinkedList('string');
+    return $this->extensions;
   }
 
   /**
@@ -295,9 +351,21 @@ class GPX implements Geographic
    */
   public function getBounds()
   {
-    //TODO: Implement this properly when waypoints, routes and tracks have
-    //      been implemented.
-    return null;
+    $result = null;
+    $lists = [
+      $this->waypoints,
+      $this->routes,
+      $this->tracks
+    ];
+    foreach ($lists as $list) {
+      if ($list === null) continue;
+      foreach ($list as $geo) {
+        $bounds = $geo->getBounds();
+        if ($bounds === null) continue;
+        $result = ($result === null ? $bounds : $result->extend($bounds));
+      }
+    }
+    return $result;
   }
 
 }