/**
  * Get a workout from a string.
  *
  * @param string $string The data.
  * @return \SportTrackerConnector\Workout\Workout
  */
 public function fromString($string)
 {
     $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) {
         $track = new Track();
         // Sport.
         if (isset($simpleXMLTrack->type)) {
             $track->setSport(SportGuesser::getSportFromCode($simpleXMLTrack->type));
         }
         // Track points.
         foreach ($simpleXMLTrack->trkseg->trkpt as $point) {
             $attributes = $point->attributes();
             $dateTime = new DateTime($point->time);
             $trackPoint = new TrackPoint((string) $attributes['lat'], (string) $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;
 }
 /**
  * Test setting/getting the author.
  */
 public function testSetGetAuthor()
 {
     $workout = new Workout();
     $this->assertNull($workout->getAuthor());
     $author = new Author();
     $workout->setAuthor($author);
     $this->assertSame($author, $workout->getAuthor());
 }
 /**
  * Test dumping a workout to a GPX string.
  */
 public function testDumpToStringMultiTrack()
 {
     $workout = new Workout();
     $workout->addTrack(new Track(array($this->getTrackPoint('53.551075', '9.993672', '2014-05-30T17:12:58+00:00', 11, 78), $this->getTrackPoint('53.550085', '9.992682', '2014-05-30T17:12:59+00:00', 10, 88)), SportMapperInterface::RUNNING));
     $workout->addTrack(new Track(array($this->getTrackPoint('53.549075', '9.991672', '2014-05-30T17:13:00+00:00', 9, 98), $this->getTrackPoint('53.548085', '9.990682', '2014-05-30T17:13:01+00:00', 8, 108)), SportMapperInterface::SWIMMING));
     $workout->setAuthor(new Author('John Doe'));
     $gpx = new GPX();
     $actual = $gpx->dumpToString($workout);
     $this->assertXmlStringEqualsXmlFile(__DIR__ . '/Expected/testDumpToStringMultiTrack.gpx', $actual);
 }
 /**
  * Test loading a workout from a GPX string with multiple tracks.
  */
 public function testFromStringMultiTrack()
 {
     $expected = new Workout();
     $expected->addTrack(new Track(array($this->getTrackPoint('53.551075', '9.993672', '2014-05-30T17:12:58+00:00', 11, 78), $this->getTrackPoint('53.550085', '9.992682', '2014-05-30T17:12:59+00:00', 10, 88)), SportMapperInterface::RUNNING));
     $expected->addTrack(new Track(array($this->getTrackPoint('53.549075', '9.991672', '2014-05-30T17:13:00+00:00', 9, 98), $this->getTrackPoint('53.548085', '9.990682', '2014-05-30T17:13:01+00:00', 8, 108)), SportMapperInterface::SWIMMING));
     $expected->setAuthor(new Author('John Doe'));
     $gpx = new GPX();
     $actual = $gpx->fromString(file_get_contents(__DIR__ . '/Fixtures/testFromStringMultiTrack.gpx'));
     $this->assertEquals($expected, $actual);
 }