-
-
Notifications
You must be signed in to change notification settings - Fork 55
Add the line part of the style extension #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
f95ad54
020645f
3553d3a
5f4009a
717f1dc
5ed1e17
eb6e8bf
7a8e240
6060b52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'; | ||
|
|
||
| /** | ||
| * @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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess these more complicated types want implementing at some point? |
||
| ]; | ||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| namespace UnitTests\phpGPX\Parsers; | ||
|
|
||
| use phpGPX\Models\Extensions; | ||
| use phpGPX\Models\Extensions\StyleExtension; | ||
| use phpGPX\Models\Extensions\TrackPointExtension; | ||
| use phpGPX\Parsers\ExtensionParser; | ||
|
|
||
|
|
@@ -27,8 +28,15 @@ public static function createTestInstance() | |
| $trackpoint->hr = (float) 152; | ||
| $trackpoint->heartRate = (float) 152; | ||
|
|
||
| $style = new StyleExtension(); | ||
| $style->color = (string) "FF0000"; | ||
| $style->opacity = (float) 0.5; | ||
| $style->width = (float) 1; | ||
| $style->linecap = (string) "square"; | ||
|
|
||
| $extensions = new Extensions(); | ||
| $extensions->trackPointExtension = $trackpoint; | ||
| $extensions->styleExtension = $style; | ||
|
|
||
| return $extensions; | ||
| } | ||
|
|
@@ -46,6 +54,7 @@ public function testParse() | |
|
|
||
| $this->assertEquals($this->testModelInstance->unsupported, $extensions->unsupported); | ||
| $this->assertEquals($this->testModelInstance->trackPointExtension, $extensions->trackPointExtension); | ||
| $this->assertEquals($this->testModelInstance->styleExtension, $extensions->styleExtension); | ||
|
|
||
| $this->assertEquals($this->testModelInstance->toArray(), $extensions->toArray()); | ||
| } | ||
|
|
@@ -71,8 +80,9 @@ public function testToXML() | |
| $attributes = [ | ||
| 'xmlns' => 'http://www.topografix.com/GPX/1/1', | ||
| 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance', | ||
| 'xsi:schemaLocation' => 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd', | ||
| 'xsi:schemaLocation' => 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2.xsd', | ||
|
Comment on lines
-74
to
+83
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't see a good reason for all this duplication within xsi:schemaLocation but I'm no expert... |
||
| 'xmlns:gpxtpx' => 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1', | ||
| 'xmlns:gpxstyle' => 'http://www.topografix.com/GPX/gpx_style/0/2', | ||
| 'xmlns:gpxx' => 'http://www.garmin.com/xmlschemas/GpxExtensions/v3', | ||
| ]; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,15 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <document xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> | ||
| <document xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2.xsd" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" xmlns:gpxstyle="http://www.topografix.com/GPX/gpx_style/0/2" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3"> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't see a good reason for all this duplication within xsi:schemaLocation but I'm no expert... |
||
| <extensions> | ||
| <gpxtpx:TrackPointExtension> | ||
| <gpxtpx:atemp>14</gpxtpx:atemp> | ||
| <gpxtpx:hr>152</gpxtpx:hr> | ||
| </gpxtpx:TrackPointExtension> | ||
| <gpxstyle:line> | ||
| <gpxstyle:color>FF0000</gpxstyle:color> | ||
| <gpxstyle:opacity>0.5</gpxstyle:opacity> | ||
| <gpxstyle:width>1</gpxstyle:width> | ||
| <gpxstyle:linecap>square</gpxstyle:linecap> | ||
| </gpxstyle:line> | ||
| </extensions> | ||
| </document> | ||
There was a problem hiding this comment.
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?