Skip to content
Draft
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions src/phpGPX/Models/Bounds.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,26 @@ class Bounds implements Summarizable
*/
public $maxLongitude;

/**
* @param ?float $minLatitude
* @param ?float $minLongitude
* @param ?float $maxLatitude
* @param ?float $maxLongitude
*/
public function __construct(?float $minLatitude, ?float $minLongitude, ?float $maxLatitude, ?float $maxLongitude)
{
$this->minLatitude = $minLatitude;
$this->minLongitude = $minLongitude;
$this->maxLatitude = $maxLatitude;
$this->maxLongitude = $maxLongitude;
}

/**
* @param ?float $minLatitude
* @param ?float $minLongitude
* @param ?float $maxLatitude
* @param ?float $maxLongitude
*/
public function __construct(?float $minLatitude, ?float $minLongitude, ?float $maxLatitude, ?float $maxLongitude)
{
$this->minLatitude = $minLatitude;
$this->minLongitude = $minLongitude;
$this->maxLatitude = $maxLatitude;
$this->maxLongitude = $maxLongitude;
}

/**
/**
* Serialize object to array
* @return array
*/
public function toArray(): array
{
{
return [
'minlat' => $this->minLatitude,
'minlon' => $this->minLongitude,
Expand Down
9 changes: 9 additions & 0 deletions src/phpGPX/Models/Extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace phpGPX\Models;

use phpGPX\Helpers\SerializationHelper;
use phpGPX\Models\Extensions\StyleExtension;
use phpGPX\Models\Extensions\TrackPointExtension;

/**
Expand All @@ -23,6 +24,13 @@ class Extensions implements Summarizable
*/
public $trackPointExtension;

/**
* GPX Style Extension v2
* @see 'http://www.topografix.com/GPX/gpx_style/0/2/'
* @var StyleExtension
*/
public $styleExtension;

/**
* @var []
*/
Expand All @@ -36,6 +44,7 @@ public function toArray()
{
return [
'trackpoint' => SerializationHelper::serialize($this->trackPointExtension),
'line' => SerializationHelper::serialize($this->styleExtension),
'unsupported' => $this->unsupported,
];
}
Expand Down
74 changes: 74 additions & 0 deletions src/phpGPX/Models/Extensions/StyleExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Created 27/02/26 23:09
* @author Peter Newman
*/

namespace phpGPX\Models\Extensions;

use phpGPX\Helpers\SerializationHelper;

/**
* Class StyleExtension
* Extension version: v2
* Based on namespace: http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd
* @package phpGPX\Models\Extensions
*/
class StyleExtension extends AbstractExtension
{
const EXTENSION_NAMESPACE = 'http://www.topografix.com/GPX/gpx_style/0/2';
const EXTENSION_NAMESPACE_XSD = 'http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd';

const EXTENSION_NAME = 'line';
const EXTENSION_NAMESPACE_PREFIX = 'gpxstyle';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there's an official defined prefix for this, should it be gpx_style to match the namespace they use in the URL?


/**
* @var string
*/
public $color;

/**
* @var float
*/
public $opacity;

/**
* @var float
*/
public $width;

/**
* @var string
*/
public $pattern;

/**
* @var string
*/
public $linecap;

/**
* StyleExtension constructor.
*/
public function __construct()
{
parent::__construct(self::EXTENSION_NAMESPACE, self::EXTENSION_NAME);
}

/**
* Serialize object to array
* @return array
*/
public function toArray()
{
return [
'color' => SerializationHelper::stringOrNull($this->color),
'opacity' => SerializationHelper::floatOrNull($this->opacity),
'width' => SerializationHelper::floatOrNull($this->width),
// 'pattern' => SerializationHelper::stringOrNull($this->pattern),
'linecap' => SerializationHelper::stringOrNull($this->linecap),
// 'dasharray' => SerializationHelper::stringOrNull($this->dasharray),
// 'extensions' => SerializationHelper::stringOrNull($this->extensions),
Comment on lines +68 to +71

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess these more complicated types want implementing at some point?

];
}
}
2 changes: 1 addition & 1 deletion src/phpGPX/Models/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct()
* @return Point[]
*/
public function getPoints(): array
{
{
/** @var Point[] $points */
$points = [];

Expand Down
2 changes: 1 addition & 1 deletion src/phpGPX/Models/Track.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct()
* @return Point[]
*/
public function getPoints(): array
{
{
/** @var Point[] $points */
$points = [];

Expand Down
13 changes: 13 additions & 0 deletions src/phpGPX/Parsers/ExtensionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
namespace phpGPX\Parsers;

use phpGPX\Models\Extensions;
use phpGPX\Models\Extensions\StyleExtension;
use phpGPX\Models\Extensions\TrackPointExtension;
use phpGPX\Parsers\Extensions\StyleExtensionParser;
use phpGPX\Parsers\Extensions\TrackPointExtensionParser;

/**
Expand Down Expand Up @@ -39,6 +41,12 @@ public static function parse($nodes)
$extensions->trackPointExtension = TrackPointExtensionParser::parse($node);
}
break;
case StyleExtension::EXTENSION_NAMESPACE:
$node = $nodes->children($namespace)->{StyleExtension::EXTENSION_NAME};
if (!empty($node)) {
$extensions->styleExtension = StyleExtensionParser::parse($node);
}
break;
default:
foreach ($nodes->children($namespace) as $child_key => $value) {
$extensions->unsupported[$key ? "$key:$child_key" : "$child_key"] = (string) $value;
Expand All @@ -64,6 +72,11 @@ public static function toXML(Extensions $extensions, \DOMDocument &$document)
$node->appendChild($child);
}

if (null !== $extensions->styleExtension) {
$child = StyleExtensionParser::toXML($extensions->styleExtension, $document);
$node->appendChild($child);
}

if (!empty($extensions->unsupported)) {
foreach ($extensions->unsupported as $key => $value) {
$child = $document->createElement($key, $value);
Expand Down
83 changes: 83 additions & 0 deletions src/phpGPX/Parsers/Extensions/StyleExtensionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Created 27/02/26 23:09
* @author Peter Newman
*/

namespace phpGPX\Parsers\Extensions;

use phpGPX\Models\Extensions\StyleExtension;
use phpGPX\Parsers\ExtensionParser;

class StyleExtensionParser
{
private static $attributeMapper = [
'color' => [
'name' => 'color',
'type' => 'string'
],
'opacity' => [
'name' => 'opacity',
'type' => 'float'
],
'width' => [
'name' => 'width',
'type' => 'float'
],
'pattern' => [
'name' => 'pattern',
'type' => 'string'
],
'linecap' => [
'name' => 'linecap',
'type' => 'string'
],
];

/**
* @param \SimpleXMLElement $node
* @return StyleExtension
*/
public static function parse($node)
{
$extension = new StyleExtension();

foreach (self::$attributeMapper as $key => $attribute) {
$extension->{$attribute['name']} = isset($node->$key) ? $node->$key : null;
if (!is_null($extension->{$attribute['name']})) {
settype($extension->{$attribute['name']}, $attribute['type']);
}
}

return $extension;
}

/**
* @param StyleExtension $extension
* @param \DOMDocument $document
* @return \DOMElement
*/
public static function toXML(StyleExtension $extension, \DOMDocument &$document)
{
$node = $document->createElement("gpxstyle:line");

ExtensionParser::$usedNamespaces[StyleExtension::EXTENSION_NAME] = [
'namespace' => StyleExtension::EXTENSION_NAMESPACE,
'xsd' => StyleExtension::EXTENSION_NAMESPACE_XSD,
'name' => StyleExtension::EXTENSION_NAME,
'prefix' => StyleExtension::EXTENSION_NAMESPACE_PREFIX
];

foreach (self::$attributeMapper as $key => $attribute) {
if (!is_null($extension->{$attribute['name']})) {
$child = $document->createElement(
sprintf("%s:%s", StyleExtension::EXTENSION_NAMESPACE_PREFIX, $key),
$extension->{$attribute['name']}
);
$node->appendChild($child);
}
}

return $node;
}
}
6 changes: 4 additions & 2 deletions tests/CreateWaypointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,21 @@ public function createWaypointFile()
}

public function setUp(): void
{
{
$this->waypoint_created_file = dirname(__FILE__)."/waypoint_test.gpx";
$this->waypoint_saved_file = dirname(__FILE__).'/output_waypoint_test.gpx';
// remove any test file hanging around
system("rm -f {$this->waypoint_created_file}");
// now create the test file
$this->createWaypointFile();
}

public function tearDown(): void
{
{
system("rm -f {$this->waypoint_created_file}");
system("rm -f {$this->waypoint_saved_file}");
}

public function test_waypoints_load()
{
$origFile = $this->waypoint_created_file;
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTests/phpGPX/Parsers/AbstractParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AbstractParserTest extends TestCase
protected $testParserClass;

protected function setUp(): void
{
{
$reflection = new \ReflectionClass($this->testParserClass);

$this->testXmlFile = simplexml_load_file(sprintf("%s/%sTest.xml", __DIR__, $reflection->getShortName()));
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTests/phpGPX/Parsers/BoundsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static function createTestInstance()
}

protected function setUp(): void
{
{
parent::setUp();

$this->testModelInstance = self::createTestInstance();
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTests/phpGPX/Parsers/CopyrightParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function createTestInstance()
}

protected function setUp(): void
{
{
parent::setUp();

$this->testModelInstance = self::createTestInstance();
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTests/phpGPX/Parsers/EmailParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function createTestInstance()
}

protected function setUp(): void
{
{
parent::setUp();

$this->testModelInstance = self::createTestInstance();
Expand Down
6 changes: 6 additions & 0 deletions tests/UnitTests/phpGPX/Parsers/ExtensionParserTest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
"course": null,
"bearing": null
},
"line": {
"color": "FF0000",
"opacity": 0.5,
"width": 1,
"linecap": "square"
},
"unsupported": []
}
Loading