Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaszczyk committed Apr 25, 2016
1 parent 07ca9f9 commit 2ee5e33
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,33 @@ public function parse()
$xmlReader->xml($this->xml);
$timestamp = $this->getStartTimestamp($xmlReader);

$item = [];
$trackpoint = new Trackpoint();
while ($xmlReader->read()) {
if ($xmlReader->nodeType == XMLReader::ELEMENT) {
switch ($xmlReader->name) {
case 'LatitudeDegrees':
$node = $xmlReader->expand();
$item['latitude'] = $node->nodeValue;
$trackpoint->latitude = $node->nodeValue;
break;
case 'LongitudeDegrees':
$node = $xmlReader->expand();
$item['longitude'] = $node->nodeValue;
$trackpoint->longitude = $node->nodeValue;
break;
case 'AltitudeMeters':
$node = $xmlReader->expand();
$item['altitude'] = $node->nodeValue;
$trackpoint->altitude = $node->nodeValue;
break;
case 'Time':
$node = $xmlReader->expand();
$item['timestamp'] = strtotime($node->nodeValue) - $timestamp;
$trackpoint->timestamp = strtotime($node->nodeValue) - $timestamp;
break;
}
}

if (isset($item['latitude']) && isset($item['longitude']) && isset($item['altitude'])) {
$results[] = $item;
$item = [];
if ($trackpoint->isComplete()) {
$results[] = $trackpoint->serialize();
unset($trackpoint);
$trackpoint = new Trackpoint();
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/Trackpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Created by PhpStorm.
* User: Mateusz
* Date: 25.04.2016
* Time: 14:45
*/

namespace MateuszBlaszczyk\TcxToJson;


class Trackpoint implements \Serializable
{
public $latitude;

public $longitude;

public $altitude;

public $timestamp;

public function isComplete()
{
if ($this->latitude && $this->longitude && $this->altitude) {
return true;
}

return false;
}

public function serialize()
{
return [
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'altitude' => $this->altitude,
'timestamp' => $this->timestamp,
];
}

public function unserialize($serialized)
{
$item = new Trackpoint();
$item->latitude = $serialized['latitude'];
$item->longitude = $serialized['longitude'];
$item->altitude = $serialized['altitude'];
$item->timestamp = $serialized['timestamp'];

return $item;
}
}

0 comments on commit 2ee5e33

Please sign in to comment.