]> git.street.me.uk Git - andy/gpx.git/commitdiff
Improve usability by implementing ArrayAccess, Countable & IteratorAggregate
authorAndy Street <andy@street.me.uk>
Wed, 19 Dec 2018 20:58:32 +0000 (20:58 +0000)
committerAndy Street <andy@street.me.uk>
Wed, 19 Dec 2018 20:58:32 +0000 (20:58 +0000)
src/libgpx/route.php
src/libgpx/track.php
src/libgpx/tracksegment.php

index 6cecabc311feb599e707b9697a9d45ceec0a5a4f..e0364a73e0fe85a00df47f454973a66b48e935bc 100644 (file)
 
 namespace libgpx;
 
+use \ArrayAccess;
+use \Countable;
 use \DomainException;
+use \IteratorAggregate;
 
 /**
  * The GPX route type.
@@ -33,7 +36,8 @@ use \DomainException;
  *
  * @author Andy Street <andy@street.me.uk>
  */
-class Route extends DataType implements Geographic
+class Route extends DataType
+  implements Geographic, ArrayAccess, Countable, IteratorAggregate
 {
 
   /**
@@ -132,4 +136,76 @@ class Route extends DataType implements Geographic
     return $length;
   }
 
+  /**
+   * Check if an offset holds a value.
+   *
+   * @see ArrayAccess::offsetExists
+   *
+   * @param mixed $offset The offset to search
+   * @return boolean
+   */
+  public function offsetExists($offset)
+  {
+    return $this->getPoints()->offsetExists($offset);
+  }
+
+  /**
+   * Fetch a point from this route.
+   *
+   * @see ArrayAccess::offsetGet
+   *
+   * @param mixed $offset The offset to retrieve
+   * @return Point
+   */
+  public function offsetGet($offset)
+  {
+    return $this->getPoints()->offsetGet($offset);
+  }
+
+  /**
+   * Set the point at an offset.
+   *
+   * @see ArrayAccess::offsetSet
+   *
+   * @param mixed $offset Where to store the point.
+   * @param Point $value The point to store.
+   * @return void
+   */
+  public function offsetSet($offset, $value)
+  {
+    return $this->getPoints()->offsetSet($offset, $value);
+  }
+
+  /**
+   * Remove a point.
+   *
+   * @see ArrayAccess::offsetUnset
+   *
+   * @param mixed $offset Which point to remove
+   * @return void
+   */
+  public function offsetUnset($offset)
+  {
+    return $this->getPoints()->offsetUnset($offset);
+  }
+
+  /**
+   * Count the number of points in this route.
+   *
+   * @return int
+   */
+  public function count()
+  {
+    return ($this->points === null ? 0 : $this->points->count());
+  }
+
+  /**
+   * Fetch an iterator for the points contained in this route.
+   *
+   * @return \Iterator
+   */
+  public function getIterator() {
+    return $this->getPoints();
+  }
+
 }
index ea19a1d55b11041779a8e47c619f08ecb8f9dc24..0eb8b4b7f33db38497a93bdf1d69d9f22b223516 100644 (file)
 
 namespace libgpx;
 
+use \ArrayAccess;
+use \Countable;
 use \DomainException;
+use \IteratorAggregate;
 
 /**
  * A GPX trk type.
  *
  * @author Andy Street <andy@street.me.uk>
  */
-class Track extends DataType implements Geographic
+class Track extends DataType
+  implements Geographic, ArrayAccess, Countable, IteratorAggregate
 {
 
   /**
@@ -75,6 +79,23 @@ class Track extends DataType implements Geographic
     $this->number = $number;
   }
 
+  /**
+   * Count the number of track points.
+   *
+   * @return The number of track points contained in all the segments of this
+   *         track.
+   */
+  public function countTrackPoints()
+  {
+    $count = 0;
+    if ($this->segments !== null) {
+      foreach ($this->segments as $segment) {
+        $count += count($segment);
+      }
+    }
+    return $count;
+  }
+
   /**
    * Fetch an ordered list of track segments.
    *
@@ -123,4 +144,76 @@ class Track extends DataType implements Geographic
     return $length;
   }
 
+  /**
+   * Check if an offset holds a value.
+   *
+   * @see ArrayAccess::offsetExists
+   *
+   * @param mixed $offset The offset to search
+   * @return boolean
+   */
+  public function offsetExists($offset)
+  {
+    return $this->getSegments()->offsetExists($offset);
+  }
+
+  /**
+   * Fetch a segment from this track.
+   *
+   * @see ArrayAccess::offsetGet
+   *
+   * @param mixed $offset The offset to retrieve
+   * @return TrackSegment
+   */
+  public function offsetGet($offset)
+  {
+    return $this->getSegments()->offsetGet($offset);
+  }
+
+  /**
+   * Set the segment at an offset.
+   *
+   * @see ArrayAccess::offsetSet
+   *
+   * @param mixed $offset Where to store the segment.
+   * @param TrackSegment $value The segment to store.
+   * @return void
+   */
+  public function offsetSet($offset, $value)
+  {
+    return $this->getSegments()->offsetSet($offset, $value);
+  }
+
+  /**
+   * Remove a segment.
+   *
+   * @see ArrayAccess::offsetUnset
+   *
+   * @param mixed $offset Which segment to remove
+   * @return void
+   */
+  public function offsetUnset($offset)
+  {
+    return $this->getSegments()->offsetUnset($offset);
+  }
+
+  /**
+   * Count the number of segments in this track.
+   *
+   * @return int
+   */
+  public function count()
+  {
+    return ($this->segments === null ? 0 : $this->segments->count());
+  }
+
+  /**
+   * Fetch an iterator for the segments contained in this track.
+   *
+   * @return \Iterator
+   */
+  public function getIterator() {
+    return $this->getSegments();
+  }
+
 }
index c48b70d45be93cc127a79f49f424908224a3e05a..92ea5ea69f1d867fcf0ab05d2b4bd4a0a7853bfe 100644 (file)
 
 namespace libgpx;
 
+use \ArrayAccess;
+use \Countable;
+use \IteratorAggregate;
+
 /**
  * A GPX trkseg type.
  *
  * @author Andy Street <andy@street.me.uk>
  */
-class TrackSegment implements Geographic
+class TrackSegment
+  implements Geographic, ArrayAccess, Countable, IteratorAggregate
 {
 
   /**
@@ -115,4 +120,76 @@ class TrackSegment implements Geographic
     return $length;
   }
 
+  /**
+   * Check if an offset holds a value.
+   *
+   * @see ArrayAccess::offsetExists
+   *
+   * @param mixed $offset The offset to search
+   * @return boolean
+   */
+  public function offsetExists($offset)
+  {
+    return $this->getPoints()->offsetExists($offset);
+  }
+
+  /**
+   * Fetch a point from this segment.
+   *
+   * @see ArrayAccess::offsetGet
+   *
+   * @param mixed $offset The offset to retrieve
+   * @return Point
+   */
+  public function offsetGet($offset)
+  {
+    return $this->getPoints()->offsetGet($offset);
+  }
+
+  /**
+   * Set the point at an offset.
+   *
+   * @see ArrayAccess::offsetSet
+   *
+   * @param mixed $offset Where to store the point.
+   * @param Point $value The point to store.
+   * @return void
+   */
+  public function offsetSet($offset, $value)
+  {
+    return $this->getPoints()->offsetSet($offset, $value);
+  }
+
+  /**
+   * Remove a point.
+   *
+   * @see ArrayAccess::offsetUnset
+   *
+   * @param mixed $offset Which point to remove
+   * @return void
+   */
+  public function offsetUnset($offset)
+  {
+    return $this->getPoints()->offsetUnset($offset);
+  }
+
+  /**
+   * Count the number of points in this track segment.
+   *
+   * @return int
+   */
+  public function count()
+  {
+    return ($this->points === null ? 0 : $this->points->count());
+  }
+
+  /**
+   * Fetch an iterator for the points contained in this segment.
+   *
+   * @return \Iterator
+   */
+  public function getIterator() {
+    return $this->getPoints();
+  }
+
 }