From 893cb1f31e2ee410415434c915e0241ecabcb447 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Wed, 19 Dec 2018 20:58:32 +0000 Subject: [PATCH] Improve usability by implementing ArrayAccess, Countable & IteratorAggregate --- src/libgpx/route.php | 78 +++++++++++++++++++++++++++++- src/libgpx/track.php | 95 ++++++++++++++++++++++++++++++++++++- src/libgpx/tracksegment.php | 79 +++++++++++++++++++++++++++++- 3 files changed, 249 insertions(+), 3 deletions(-) diff --git a/src/libgpx/route.php b/src/libgpx/route.php index 6cecabc..e0364a7 100644 --- a/src/libgpx/route.php +++ b/src/libgpx/route.php @@ -24,7 +24,10 @@ namespace libgpx; +use \ArrayAccess; +use \Countable; use \DomainException; +use \IteratorAggregate; /** * The GPX route type. @@ -33,7 +36,8 @@ use \DomainException; * * @author Andy Street */ -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(); + } + } diff --git a/src/libgpx/track.php b/src/libgpx/track.php index ea19a1d..0eb8b4b 100644 --- a/src/libgpx/track.php +++ b/src/libgpx/track.php @@ -24,14 +24,18 @@ namespace libgpx; +use \ArrayAccess; +use \Countable; use \DomainException; +use \IteratorAggregate; /** * A GPX trk type. * * @author Andy Street */ -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(); + } + } diff --git a/src/libgpx/tracksegment.php b/src/libgpx/tracksegment.php index c48b70d..92ea5ea 100644 --- a/src/libgpx/tracksegment.php +++ b/src/libgpx/tracksegment.php @@ -24,12 +24,17 @@ namespace libgpx; +use \ArrayAccess; +use \Countable; +use \IteratorAggregate; + /** * A GPX trkseg type. * * @author Andy Street */ -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(); + } + } -- 2.39.5