/** * {@inheritdoc} */ public function fromString($string) : Workout { $simpleXML = new \SimpleXMLElement($string); $workout = new Workout(); if (isset($simpleXML->metadata->author->name)) { $workout->setAuthor(new Author($simpleXML->metadata->author->name)); } foreach ($simpleXML->trk as $simpleXMLTrack) { // Sport. $sport = SportMapperInterface::class; if (isset($simpleXMLTrack->type)) { $sport = SportGuesser::sportFromCode((string) $simpleXMLTrack->type); } $track = new Track(array(), $sport); // Track points. foreach ($simpleXMLTrack->trkseg->trkpt as $point) { $attributes = $point->attributes(); $dateTime = new \DateTime((string) $point->time); $trackPoint = new TrackPoint((double) $attributes['lat'], (double) $attributes['lon'], $dateTime); $trackPoint->setElevation((int) $point->ele); if (isset($point->extensions)) { $trackPoint->setExtensions($this->parseExtensions($point->extensions)); } $track->addTrackPoint($trackPoint); } $workout->addTrack($track); } return $workout; }
/** * {@inheritdoc} */ public function fromString($string) : Workout { $simpleXML = new \SimpleXMLElement($string); $workout = new Workout(); foreach ($simpleXML->Activities[0] as $simpleXMLActivity) { // Sport. $attributes = $simpleXMLActivity->attributes(); $sport = SportMapperInterface::class; if (isset($attributes['Sport'])) { $sport = SportGuesser::sportFromCode((string) $attributes['Sport']); } $workoutTrack = new Track(array(), $sport); // Track points. foreach ($simpleXMLActivity->Lap as $lap) { foreach ($lap->Track as $track) { foreach ($track->Trackpoint as $trackPoint) { $dateTime = new \DateTime((string) $trackPoint->Time); $latitude = (double) $trackPoint->Position->LatitudeDegrees; $longitude = (double) $trackPoint->Position->LongitudeDegrees; $workoutTrackPoint = new TrackPoint($latitude, $longitude, $dateTime); $workoutTrackPoint->setElevation(round((double) $trackPoint->AltitudeMeters, 2)); if ($trackPoint->DistanceMeters) { $workoutTrackPoint->setDistance($trackPoint->DistanceMeters); } $extensions = $this->parseExtensions($trackPoint); $workoutTrackPoint->setExtensions($extensions); $workoutTrack->addTrackPoint($workoutTrackPoint); } } } $workout->addTrack($workoutTrack); } return $workout; }
/** * Test setting/getting the author. */ public function testSetGetAuthor() { $workout = new Workout(); self::assertNull($workout->author()); $author = new Author('author'); $workout->setAuthor($author); self::assertSame($author, $workout->author()); }
/** * {@inheritdoc} */ public function toString(Workout $workout) : string { $data = array(); $tracks = $workout->tracks(); foreach ($tracks as $track) { $data[] = array('workout' => array('points' => $this->writeTrackPoints($track->trackPoints()))); } return json_encode($data, JSON_PRETTY_PRINT); }
/** * Test dumping a workout to a TCX string. */ public function testToStringMultiActivity() { $workout = new Workout(); $workout->addTrack(new Track(array($this->getTrackPoint(53.551075, 9.993672, '2014-05-30T17:12:58+00:00', 11, null, 78), $this->getTrackPoint(53.550085, 9.992682, '2014-05-30T17:12:59+00:00', 10, 128.0, 88)), SportMapperInterface::RUNNING)); $workout->addTrack(new Track(array($this->getTrackPoint(53.549075, 9.991671999999999, '2014-05-30T17:13:00+00:00', 9, 258.0, 98), $this->getTrackPoint(53.548085, 9.990682, '2014-05-30T17:13:01+00:00', 8, 456.0, 108)), SportMapperInterface::SWIMMING)); $filesystemMock = $this->createMock(FilesystemInterface::class); $tcx = new TCX($filesystemMock); $actual = $tcx->toString($workout); self::assertXmlStringEqualsXmlFile(__DIR__ . '/Expected/' . $this->getName() . '.tcx', $actual); }
/** * Test loading a workout from a string with multiple activities. */ public function testFromStringMultiActivity() { $expected = new Workout(); $expected->addTrack(new Track(array($this->getTrackPoint(53.551075, 9.993672, '2014-05-30T17:12:58+00:00', 11, 0, 78), $this->getTrackPoint(53.550085, 9.992682, '2014-05-30T17:12:59+00:00', 10, 128, 88)), SportMapperInterface::RUNNING)); $expected->addTrack(new Track(array($this->getTrackPoint(53.549075, 9.991671999999999, '2014-05-30T17:13:00+00:00', 9, null, 98), $this->getTrackPoint(53.548085, 9.990682, '2014-05-30T17:13:01+00:00', 8, 258, 108)), SportMapperInterface::SWIMMING)); $fileSystemMock = $this->createMock(FilesystemInterface::class); $tcx = new TCX($fileSystemMock); $actual = $tcx->fromString(file_get_contents(__DIR__ . '/Fixtures/testFromStringMultiActivity.tcx')); self::assertEquals($expected, $actual); }
/** * Test dumping a workout to a GPX string. */ public function testToStringMultiTrack() { $workout = new Workout(); $workout->addTrack(new Track(array($this->getTrackPoint(53.551075, 9.993672, '2014-05-30T17:12:58+00:00', 11, 0, 78), $this->getTrackPoint(53.550085, 9.992682, '2014-05-30T17:12:59+00:00', 10, 10, 88)), SportMapperInterface::RUNNING)); $workout->addTrack(new Track(array($this->getTrackPoint(53.549075, 9.991671999999999, '2014-05-30T17:13:00+00:00', 9, null, 98), $this->getTrackPoint(53.548085, 9.990682, '2014-05-30T17:13:01+00:00', 8, null, 108)), SportMapperInterface::SWIMMING)); $workout->setAuthor(new Author('John Doe')); $filesystemMock = $this->createMock(FilesystemInterface::class); $gpx = new JSON($filesystemMock); $actual = $gpx->toString($workout); self::assertJsonStringEqualsJsonFile(__DIR__ . '/Expected/' . $this->getName() . '.json', $actual); }
/** * Write the tracks to the TCX. * * @param \XMLWriter $xmlWriter The XML writer. * @param Workout $workout The workout. */ private function writeTracks(\XMLWriter $xmlWriter, Workout $workout) { $xmlWriter->startElement('Activities'); foreach ($workout->tracks() as $track) { $xmlWriter->startElement('Activity'); $xmlWriter->writeAttribute('Sport', ucfirst($track->sport())); // Use the start date time as the ID. This could be anything. $xmlWriter->writeElement('Id', $this->formatDateTime($track->startDateTime())); $xmlWriter->startElement('Lap'); $xmlWriter->writeAttribute('StartTime', $this->formatDateTime($track->startDateTime())); $xmlWriter->writeElement('TotalTimeSeconds', (string) $track->duration()->totalSeconds()); $xmlWriter->writeElement('DistanceMeters', (string) $track->length()); $this->writeLapHeartRateDate($xmlWriter, $track); $xmlWriter->startElement('Track'); $this->writeTrackPoints($xmlWriter, $track->trackPoints()); $xmlWriter->endElement(); $xmlWriter->endElement(); $xmlWriter->endElement(); } $xmlWriter->endElement(); }
/** * Write the metadata in the GPX. * * @param \XMLWriter $xmlWriter The XML writer. * @param Workout $workout The workout. */ protected function writeMetaData(\XMLWriter $xmlWriter, Workout $workout) { $xmlWriter->startElement('metadata'); if ($workout->author() !== null) { $xmlWriter->startElement('author'); $xmlWriter->writeElement('name', $workout->author()->name()); $xmlWriter->endElement(); } $xmlWriter->endElement(); }