*/
protected $waypoints;
- /**
- * Create a new instance.
- */
- public function __construct()
- {
- $this->links = new TypedDoublyLinkedList('libgpx\Link');
- $this->keywords = new TypedDoublyLinkedList('string');
- $this->metadataExtensions = new TypedDoublyLinkedList('string');
- $this->waypoints = new TypedDoublyLinkedList('libgpx\Point');
- }
-
-
/**
* Fetch the name of the program that created the GPX file.
*
/**
* Fetch links to other resources.
*
+ * @param boolean $create Create the list if it does not already exist.
* @return TypedDoublyLinkedList
*/
- public function getLinks()
+ public function getLinks(bool $create = true)
{
+ if ($create && $this->links === null)
+ $this->links = new TypedDoublyLinkedList('libgpx\Link');
return $this->links;
}
/**
* Fetch a list of keywords for this GPX file.
*
+ * @param boolean $create Create the list if it does not already exist.
* @return TypedDoublyLinkedList The keywords
*/
- public function getKeywords()
+ public function getKeywords(bool $create = true)
{
+ if ($create && $this->keywords === null)
+ $this->keywords = new TypedDoublyLinkedList('string');
return $this->keywords;
}
/**
* Fetch a list of XML snippets describing unsupported metadata.
*
+ * @param boolean $create Create the list if it does not already exist.
* @return TypedDoublyLinkedList The keywords
*/
- public function getMetadataExtensions()
+ public function getMetadataExtensions(bool $create = true)
{
+ if ($create && $this->metadataExtensions === null)
+ $this->metadataExtensions = new TypedDoublyLinkedList('string');
return $this->metadataExtensions;
}
/**
* Fetch a list of waypoints.
*
+ * @param boolean $create Create the list if it does not already exist.
* @return TypedDoublyLinkedList A list of waypoints.
*/
- public function getWaypoints()
+ public function getWaypoints(bool $create = true)
{
+ if ($create && $this->waypoints === null)
+ $this->waypoints = new TypedDoublyLinkedList('libgpx\Point');
return $this->waypoints;
}
$namespace . ' ' . $schema
);
$this->writeMetadata($gpx, $xml);
- foreach ($gpx->getWaypoints() as $wpt) {
- $this->writePoint($wpt, 'wpt', $xml);
+ $waypoints = $gpx->getWaypoints(false);
+ if ($waypoints !== null) {
+ foreach ($waypoints as $wpt) {
+ $this->writePoint($wpt, 'wpt', $xml);
+ }
}
$xml->endElement();
$xml->endDocument();
if ($copyright instanceof Copyright)
$this->writeCopyright($copyright, $xml);
}
- foreach($gpx->getLinks() as $link) {
- $this->writeLink($link, $xml);
- if ($this->format == '1.0')
- break;
+ $links = $gpx->getLinks(false);
+ if ($links !== null) {
+ foreach($links as $link) {
+ $this->writeLink($link, $xml);
+ if ($this->format == '1.0')
+ break;
+ }
}
$xml->writeElement('time', $this->createTimestamp(new DateTime()));
- if (!empty($gpx->getKeywords()))
+ $keywords = $gpx->getKeywords(false);
+ if ($keywords !== null && !$keywords->isEmpty())
$xml->writeElement(
'keywords',
implode(', ', $gpx->getKeywords()->toArray())
$xml->endElement();
}
if ($this->format == '1.1') {
- $extensions = $gpx->getMetadataExtensions();
- if (!$extensions->isEmpty()) {
+ $extensions = $gpx->getMetadataExtensions(false);
+ if ($extensions !== null && !$extensions->isEmpty()) {
$xml->startElement('extensions');
foreach ($extensions as $extension) {
$xml->writeRaw($extension);