Skip to content

Commit 3fb233d

Browse files
committed
Remove AbstractExtension, simplify constructors across models, and replace Point constants with PointType enum 🫃
1 parent 9a43028 commit 3fb233d

30 files changed

Lines changed: 209 additions & 749 deletions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ $file->save('output.json', phpGPX::JSON_FORMAT);
100100

101101
use phpGPX\Models\GpxFile;
102102
use phpGPX\Models\Point;
103+
use phpGPX\Models\PointType;
103104
use phpGPX\Models\Segment;
104105
use phpGPX\Models\Track;
105106
use phpGPX\Models\Extensions;
@@ -113,7 +114,7 @@ $track->type = 'RUN';
113114

114115
$segment = new Segment();
115116

116-
$point = new Point(Point::TRACKPOINT);
117+
$point = new Point(PointType::Trackpoint);
117118
$point->latitude = 54.9328621088893;
118119
$point->longitude = 9.860624216140083;
119120
$point->elevation = 0;

docs/01_Usage/02_Creating_Files.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ You can build GPX files programmatically.
88
use phpGPX\Models\GpxFile;
99
use phpGPX\Models\Metadata;
1010
use phpGPX\Models\Point;
11+
use phpGPX\Models\PointType;
1112
use phpGPX\Models\Segment;
1213
use phpGPX\Models\Track;
1314
use phpGPX\phpGPX;
@@ -34,7 +35,7 @@ $points = [
3435
];
3536

3637
foreach ($points as $data) {
37-
$point = new Point(Point::TRACKPOINT);
38+
$point = new Point(PointType::Trackpoint);
3839
$point->latitude = $data['lat'];
3940
$point->longitude = $data['lon'];
4041
$point->elevation = $data['ele'];
@@ -67,6 +68,7 @@ echo "Distance: " . round($gpxFile->tracks[0]->stats->distance) . " m\n";
6768
```php
6869
use phpGPX\Models\GpxFile;
6970
use phpGPX\Models\Point;
71+
use phpGPX\Models\PointType;
7072
use phpGPX\Models\Route;
7173
use phpGPX\phpGPX;
7274

@@ -82,7 +84,7 @@ $waypoints = [
8284
];
8385

8486
foreach ($waypoints as $data) {
85-
$point = new Point(Point::ROUTEPOINT);
87+
$point = new Point(PointType::Routepoint);
8688
$point->latitude = $data['lat'];
8789
$point->longitude = $data['lon'];
8890
$point->elevation = $data['ele'];
@@ -100,11 +102,12 @@ $gpxFile->save('trail.gpx', phpGPX::XML_FORMAT);
100102
use phpGPX\Models\GpxFile;
101103
use phpGPX\Models\Link;
102104
use phpGPX\Models\Point;
105+
use phpGPX\Models\PointType;
103106
use phpGPX\phpGPX;
104107

105108
$gpxFile = new GpxFile();
106109

107-
$waypoint = new Point(Point::WAYPOINT);
110+
$waypoint = new Point(PointType::Waypoint);
108111
$waypoint->latitude = 48.8566;
109112
$waypoint->longitude = 2.3522;
110113
$waypoint->elevation = 35;

docs/01_Usage/05_Extensions.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,9 @@ To add support for a new GPX extension type, implement two interfaces:
118118
### 1. Extension model — `ExtensionInterface`
119119

120120
```php
121-
use phpGPX\Models\Extensions\AbstractExtension;
122121
use phpGPX\Models\Extensions\ExtensionInterface;
123122

124-
class MyExtension extends AbstractExtension implements ExtensionInterface
123+
class MyExtension implements ExtensionInterface
125124
{
126125
public const NAMESPACE = 'http://example.com/ext/v1';
127126
public const XSD = 'http://example.com/ext/v1/schema.xsd';

examples/CreateFileFromScratch.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use phpGPX\Models\Link;
88
use phpGPX\Models\Metadata;
99
use phpGPX\Models\Point;
10+
use phpGPX\Models\PointType;
1011
use phpGPX\Models\Segment;
1112
use phpGPX\Models\Track;
1213
use phpGPX\Models\Extensions;
@@ -84,7 +85,7 @@
8485

8586
foreach ($sample_data as $sample_point) {
8687
// Creating trackpoint
87-
$point = new Point(Point::TRACKPOINT);
88+
$point = new Point(PointType::Trackpoint);
8889
$point->latitude = $sample_point['latitude'];
8990
$point->longitude = $sample_point['longitude'];
9091
$point->elevation = $sample_point['elevation'];
@@ -106,7 +107,7 @@
106107
$gpx_file->tracks[] = $track;
107108

108109
// Create waypoint
109-
$point = new Point(Point::WAYPOINT);
110+
$point = new Point(PointType::Waypoint);
110111
$point->name = 'Example Waypoint';
111112
$point->latitude = $sample_point['latitude'];
112113
$point->longitude = $sample_point['longitude'];

examples/waypoints_create.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use phpGPX\Models\Link;
88
use phpGPX\Models\Metadata;
99
use phpGPX\Models\Point;
10+
use phpGPX\Models\PointType;
1011
use phpGPX\Models\Segment;
1112
use phpGPX\Models\Track;
1213

@@ -75,7 +76,7 @@
7576
$wp = [];
7677
foreach ($sample_data as $sample_point) {
7778
// Creating trackpoint
78-
$point = new Point(Point::WAYPOINT);
79+
$point = new Point(PointType::Waypoint);
7980
$point->latitude = $sample_point['latitude'];
8081
$point->longitude = $sample_point['longitude'];
8182
$point->elevation = $sample_point['elevation'];

src/phpGPX/Models/Bounds.php

Lines changed: 30 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,40 @@
11
<?php
2-
/**
3-
* Created 16/02/2017 22:03
4-
* @author Jakub Dubec <jakub.dubec@gmail.com>
5-
*/
62

73
namespace phpGPX\Models;
84

5+
/**
6+
* Two lat/lon pairs defining the extent of an element.
7+
*/
98
class Bounds implements \JsonSerializable
109
{
11-
public const TAG_NAME = 'bounds';
12-
13-
/**
14-
* Minimal latitude in file.
15-
* @var float|null
16-
*/
17-
public ?float $minLatitude;
18-
19-
/**
20-
* Minimal longitude in file.
21-
* @var float|null
22-
*/
23-
public ?float $minLongitude;
10+
public const TAG_NAME = 'bounds';
2411

25-
/**
26-
* Maximal latitude in file.
27-
* @var float|null
28-
*/
29-
public ?float $maxLatitude;
12+
public function __construct(
13+
public ?float $minLatitude = null,
14+
public ?float $minLongitude = null,
15+
public ?float $maxLatitude = null,
16+
public ?float $maxLongitude = null,
17+
) {}
3018

3119
/**
32-
* Maximal longitude in file.
33-
* @var float|null
20+
* GeoJSON bbox: [minLon, minLat, maxLon, maxLat]
3421
*/
35-
public ?float $maxLongitude;
36-
37-
/**
38-
* @param float|null $minLatitude
39-
* @param float|null $minLongitude
40-
* @param float|null $maxLatitude
41-
* @param float|null $maxLongitude
42-
*/
43-
public function __construct(?float $minLatitude, ?float $minLongitude, ?float $maxLatitude, ?float $maxLongitude)
44-
{
45-
$this->minLatitude = $minLatitude;
46-
$this->minLongitude = $minLongitude;
47-
$this->maxLatitude = $maxLatitude;
48-
$this->maxLongitude = $maxLongitude;
49-
}
50-
51-
/**
52-
* GeoJSON serializer
53-
* @return array
54-
*/
55-
public function jsonSerialize(): array
56-
{
57-
return [$this->minLongitude, $this->minLatitude, $this->maxLongitude, $this->maxLatitude];
58-
}
59-
60-
public static function parse(\SimpleXMLElement $node): ?Bounds
61-
{
62-
if ($node->getName() != self::TAG_NAME) {
63-
return null;
64-
}
65-
66-
return new Bounds(
67-
(float) $node['minlat'],
68-
(float) $node['minlon'],
69-
(float) $node['maxlat'],
70-
(float) $node['maxlon']
71-
);
72-
}
73-
}
22+
public function jsonSerialize(): array
23+
{
24+
return [$this->minLongitude, $this->minLatitude, $this->maxLongitude, $this->maxLatitude];
25+
}
26+
27+
public static function parse(\SimpleXMLElement $node): ?Bounds
28+
{
29+
if ($node->getName() != self::TAG_NAME) {
30+
return null;
31+
}
32+
33+
return new Bounds(
34+
(float) $node['minlat'],
35+
(float) $node['minlon'],
36+
(float) $node['maxlat'],
37+
(float) $node['maxlon']
38+
);
39+
}
40+
}

src/phpGPX/Models/Collection.php

Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,39 @@
11
<?php
2-
/**
3-
* Created 26/08/16 14:21
4-
* @author Jakub Dubec <jakub.dubec@gmail.com>
5-
*/
62

73
namespace phpGPX\Models;
84

95
/**
10-
* Class Collection
11-
* @package phpGPX\Models
6+
* Abstract base class for Track and Route.
127
*/
138
abstract class Collection implements \JsonSerializable
149
{
10+
/** GPS name of route / track. */
11+
public ?string $name = null;
1512

16-
/**
17-
* GPS name of route / track.
18-
* An original GPX 1.1 attribute.
19-
* @var string|null
20-
*/
21-
public ?string $name;
22-
23-
/**
24-
* GPS comment for route.
25-
* An original GPX 1.1 attribute.
26-
* @var string|null
27-
*/
28-
public ?string $comment;
29-
30-
/**
31-
* Text description of route/track for user. Not sent to GPS.
32-
* An original GPX 1.1 attribute.
33-
* @var string|null
34-
*/
35-
public ?string $description;
36-
37-
/**
38-
* Source of data. Included to give user some idea of reliability and accuracy of data.
39-
* An original GPX 1.1 attribute.
40-
* @var string|null
41-
*/
42-
public ?string $source;
13+
/** GPS comment for route. */
14+
public ?string $comment = null;
4315

44-
/**
45-
* Links to external information about the route/track.
46-
* An original GPX 1.1 attribute.
47-
* @var Link[]
48-
*/
49-
public array $links;
16+
/** Text description of route/track for user. */
17+
public ?string $description = null;
5018

51-
/**
52-
* GPS route/track number.
53-
* An original GPX 1.1 attribute.
54-
* @var int|null
55-
*/
56-
public ?int $number;
19+
/** Source of data. */
20+
public ?string $source = null;
5721

58-
/**
59-
* Type (classification) of route/track.
60-
* An original GPX 1.1 attribute.
61-
* @var string|null
62-
*/
63-
public ?string $type;
22+
/** @var Link[] Links to external information. */
23+
public array $links = [];
6424

65-
/**
66-
* You can add extend GPX by adding your own elements from another schema here.
67-
* An original GPX 1.1 attribute.
68-
* @var Extensions|null
69-
*/
70-
public ?Extensions $extensions;
25+
/** GPS route/track number. */
26+
public ?int $number = null;
7127

72-
/**
73-
* Objects contains calculated statistics for collection.
74-
* @var Stats|null
75-
*/
76-
public ?Stats $stats;
28+
/** Type (classification) of route/track. */
29+
public ?string $type = null;
7730

78-
/**
79-
* Collection constructor.
80-
*/
81-
public function __construct()
82-
{
83-
$this->name = null;
84-
$this->comment = null;
85-
$this->description = null;
86-
$this->source = null;
87-
$this->links = [];
88-
$this->number = null;
89-
$this->type = null;
90-
$this->extensions = null;
91-
$this->stats = null;
92-
}
31+
/** GPX extensions. */
32+
public ?Extensions $extensions = null;
9333

34+
/** Calculated statistics. */
35+
public ?Stats $stats = null;
9436

95-
/**
96-
* Return all points in collection.
97-
* @return Point[]
98-
*/
37+
/** @return Point[] */
9938
abstract public function getPoints(): array;
100-
}
39+
}

0 commit comments

Comments
 (0)