Permalink
Browse files

Transport API

  • Loading branch information...
0 parents commit 0217404902e30eb27bbf238424c225165190ddae @fabian fabian committed Mar 4, 2012
Showing with 15,333 additions and 0 deletions.
  1. +3 −0 .gitmodules
  2. +129 −0 lib/Transport/API.php
  3. +37 −0 lib/Transport/Entity/Coordinate.php
  4. +23 −0 lib/Transport/Entity/Location/Address.php
  5. +70 −0 lib/Transport/Entity/Location/Location.php
  6. +73 −0 lib/Transport/Entity/Location/LocationQuery.php
  7. +23 −0 lib/Transport/Entity/Location/Poi.php
  8. +52 −0 lib/Transport/Entity/Location/Station.php
  9. +24 −0 lib/Transport/Entity/LocationFactory.php
  10. +24 −0 lib/Transport/Entity/Query.php
  11. +35 −0 lib/Transport/Entity/Schedule/Connection.php
  12. +89 −0 lib/Transport/Entity/Schedule/ConnectionQuery.php
  13. +39 −0 lib/Transport/Entity/Schedule/Prognosis.php
  14. +99 −0 lib/Transport/Entity/Schedule/StationBoardJourney.php
  15. +53 −0 lib/Transport/Entity/Schedule/StationBoardQuery.php
  16. +55 −0 lib/Transport/Entity/Schedule/Stop.php
  17. +40 −0 lib/Transport/Entity/Transportations.php
  18. +15 −0 phpunit.xml.dist
  19. BIN silex.phar
  20. +50 −0 test/Transport/Test/APITest.php
  21. +35 −0 test/Transport/Test/Entity/Location/AddressTest.php
  22. +44 −0 test/Transport/Test/Entity/Location/LocationQueryTest.php
  23. +36 −0 test/Transport/Test/Entity/Location/PoiTest.php
  24. +36 −0 test/Transport/Test/Entity/Location/StationTest.php
  25. +37 −0 test/Transport/Test/Entity/LocationFactoryTest.php
  26. +58 −0 test/Transport/Test/Entity/Schedule/ConnectionDelayTest.php
  27. +57 −0 test/Transport/Test/Entity/Schedule/ConnectionTest.php
  28. +45 −0 test/Transport/Test/Entity/Schedule/StationBoardJourneyTest.php
  29. +15 −0 test/Transport/Test/Entity/TransportationsTest.php
  30. +19 −0 test/bootstrap.php
  31. +2,379 −0 test/fixtures/archive/connection-2012-01-16.xml
  32. +2,388 −0 test/fixtures/archive/stationboard-2012-02-20.xml
  33. +1,897 −0 test/fixtures/connection.xml
  34. +42 −0 test/fixtures/location.xml
  35. +66 −0 test/fixtures/stationboard.xml
  36. +42 −0 test/update-fixtures.php
  37. +1 −0 vendor/buzz
  38. +115 −0 web/api.php
  39. +47 −0 web/autocomplete.html
  40. +189 −0 web/index.html
  41. +176 −0 web/media/bootstrap/LICENSE
  42. +2,467 −0 web/media/bootstrap/bootstrap.css
  43. +356 −0 web/media/bootstrap/bootstrap.min.css
  44. +124 −0 web/media/bootstrap/js/bootstrap-alerts.js
  45. +64 −0 web/media/bootstrap/js/bootstrap-buttons.js
  46. +55 −0 web/media/bootstrap/js/bootstrap-dropdown.js
  47. +260 −0 web/media/bootstrap/js/bootstrap-modal.js
  48. +90 −0 web/media/bootstrap/js/bootstrap-popover.js
  49. +107 −0 web/media/bootstrap/js/bootstrap-scrollspy.js
  50. +80 −0 web/media/bootstrap/js/bootstrap-tabs.js
  51. +321 −0 web/media/bootstrap/js/bootstrap-twipsy.js
  52. +26 −0 web/media/bootstrap/lib/bootstrap.less
  53. +479 −0 web/media/bootstrap/lib/forms.less
  54. +222 −0 web/media/bootstrap/lib/mixins.less
  55. +1,060 −0 web/media/bootstrap/lib/patterns.less
  56. +141 −0 web/media/bootstrap/lib/reset.less
  57. +139 −0 web/media/bootstrap/lib/scaffolding.less
  58. +224 −0 web/media/bootstrap/lib/tables.less
  59. +187 −0 web/media/bootstrap/lib/type.less
  60. 0 web/media/bootstrap/lib/variables.css
  61. +60 −0 web/media/bootstrap/lib/variables.less
  62. +1 −0 web/media/css/layout.css
  63. +4 −0 web/media/js/jquery-1.7.1.min.js
  64. +62 −0 web/media/less/layout.less
  65. +147 −0 web/stationboard.html
@@ -0,0 +1,3 @@
+[submodule "vendor/buzz"]
+ path = vendor/buzz
+ url = git://github.com/kriswallsmith/Buzz.git
@@ -0,0 +1,129 @@
+<?php
+
+namespace Transport;
+
+use Buzz\Browser;
+use Transport\Entity\Location\Location;
+use Transport\Entity\Query;
+use Transport\Entity\Location\LocationQuery;
+use Transport\Entity\Schedule\ConnectionQuery;
+use Transport\Entity\Schedule\StationBoardQuery;
+
+class API
+{
+ const URL = 'http://xmlfahrplan.sbb.ch/bin/extxml.exe/';
+
+ const SBB_PROD = 'iPhone3.1';
+ const SBB_VERSION = '2.3';
+ const SBB_ACCESS_ID = 'MJXZ841ZfsmqqmSymWhBPy5dMNoqoGsHInHbWJQ5PTUZOJ1rLTkn8vVZOZDFfSe';
+
+ const DATE_TYPE_DEPARTURE = 0;
+ const DATE_TYPE_ARRIVAL = 1;
+
+ const SEARCH_MODE_NORMAL = 'N';
+ const SEARCH_MODE_ECONOMIC = 'P';
+
+ /**
+ * @var Buzz\Browser
+ */
+ protected $browser;
+
+ /**
+ * @var string
+ */
+ protected $lang;
+
+ public function __construct(Browser $browser = null, $lang = 'EN') {
+ $this->browser = $browser ?: new Browser();
+ $this->lang = $lang;
+ }
+
+ /**
+ * @return Buzz\Message\Response
+ */
+ public function sendQuery(Query $query) {
+
+ $headers = array();
+ $headers[] = 'User-Agent: SBBMobile/4.2 CFNetwork/485.13.9 Darwin/11.0.0';
+ $headers[] = 'Accept: application/xml';
+ $headers[] = 'Content-Type: application/xml';
+
+ return $this->browser->post(self::URL, $headers, $query->toXml());
+ }
+
+ /**
+ * @return array
+ */
+ public function findConnections(ConnectionQuery $query)
+ {
+ // send request
+ $response = $this->sendQuery($query);
+ //header('Content-Type: application/xml');
+ //echo $response->getContent();exit;
+
+ // parse result
+ $result = simplexml_load_string($response->getContent());
+
+ $connections = array();
+ foreach ($result->ConRes->ConnectionList->Connection as $connection) {
+
+ $connections[] = Entity\Schedule\Connection::createFromXml($connection);
+ }
+
+ return $connections;
+ }
+
+ /**
+ * @return array
+ */
+ public function findLocations(LocationQuery $query)
+ {
+ // send request
+ $response = $this->sendQuery($query);
+
+ // parse result
+ $result = simplexml_load_string($response->getContent());
+
+ $locations = array();
+ foreach ($result->LocValRes as $part) {
+
+ $id = (string) $part['id'];
+
+ $locations[$id] = array();
+ foreach ($part->children() as $location) {
+ $locations[$id][] = Entity\LocationFactory::createFromXml($location);
+ }
+ }
+
+ if (count($locations) > 1) {
+ return $locations;
+ }
+ return reset($locations);
+ }
+
+ /**
+ * @param Entity\Station $station
+ * @param string $boardType
+ * @param int $maxJourneys
+ * @param string $dateTime
+ * @param array $transportationTypes
+ */
+ public function getStationBoard(StationBoardQuery $query)
+ {
+ // send request
+ $response = $this->sendQuery($query);
+
+ // parse result
+ $result = simplexml_load_string($response->getContent());
+
+ $journeys = array();
+ if ($result->STBRes->JourneyList->STBJourney) {
+ foreach ($result->STBRes->JourneyList->STBJourney as $journey) {
+
+ $journeys[] = Entity\Schedule\StationBoardJourney::createFromXml($journey);
+ }
+ }
+
+ return $journeys;
+ }
+}
@@ -0,0 +1,37 @@
+<?php
+
+namespace Transport\Entity;
+
+class Coordinate
+{
+ /**
+ * @var string
+ */
+ public $type;
+
+ /**
+ * @var int
+ */
+ public $x;
+
+ /**
+ * @var int
+ */
+ public $y;
+
+ /**
+ * Factory method to create an instance of Coordinate and extract the data from the given xml
+ *
+ * @param \SimpleXMLElement $xml The item xml
+ * @return Coordinate The created instance
+ */
+ static public function createFromXml(\SimpleXMLElement $xml)
+ {
+ $coordinate = new Coordinate();
+ $coordinate->type = (string) $xml['type'];
+ $coordinate->x = (string) $xml['x'];
+ $coordinate->y = (string) $xml['y'];
+
+ return $coordinate;
+ }
+}
@@ -0,0 +1,23 @@
+<?php
+
+namespace Transport\Entity\Location;
+
+/**
+ * Represents a Address we received as response
+ *
+ * <Address name="3011 Bern, Bollwerk 19" type="WGS84" x="7440803" y="46949607"/>
+ */
+class Address extends Location
+{
+ /**
+ * {@inheritDoc}
+ */
+ static public function createFromXml(\SimpleXMLElement $xml, Location $obj = null)
+ {
+ if (!$obj) {
+ $obj = new Address;
+ }
+ return parent::createFromXml($xml, $obj);
+ }
+}
+
@@ -0,0 +1,70 @@
+<?php
+
+namespace Transport\Entity\Location;
+
+use Transport\Entity\Coordinate;
+
+class Location
+{
+ /**
+ * The name of this location
+ * @var string
+ */
+ public $name;
+
+ /**
+ * The score with regard to the search request, the higher the better
+ * @var int
+ */
+ public $score;
+
+ /**
+ * @var Coordinate
+ */
+ public $coordinate;
+
+ /**
+ * @param \SimpleXmlElement $parent The parent element or null to create a new one
+ * @return \SimpleXMLElement
+ */
+ public function toXml(\SimpleXMLElement $parent = null)
+ {
+ // could be improved :)
+ $className = substr(get_class($this), strlen(__NAMESPACE__) + 1);
+
+ if (null !== $parent) {
+ $xml = $parent->addChild($className);
+ } else {
+ $xml = new \SimpleXMLElement(sprintf('<%s />', $className));
+ }
+
+ $xml->addAttribute('name', $this->name);
+ $xml->addAttribute('x', $this->coordinate->x);
+ $xml->addAttribute('y', $this->coordinate->y);
+ return $xml;
+ }
+
+ /**
+ * Factory method to create an instance and extract the data from the given xml
+ *
+ * @param \SimpleXMLElement $xml The item xml
+ * @param Location $obj An object or null to create it
+ * @return Location The created instance
+ */
+ static public function createFromXml(\SimpleXMLElement $xml, Location $obj = null)
+ {
+ if (!is_object($obj)) {
+ throw new \InvalidArgumentException('Argument must be an object');
+ }
+
+ if ($xml['name']) {
+ $obj->name = (string) $xml['name'];
+ }
+ if ($xml['score']) {
+ $obj->score = (string) $xml['score'];
+ }
+ $obj->coordinate = Coordinate::createFromXml($xml);
+
+ return $obj;
+ }
+}
@@ -0,0 +1,73 @@
+<?php
+
+namespace Transport\Entity\Location;
+
+use Transport\Entity\Query;
+use Transport\Entity\Transportations;
+use Transport\Entity\Location\Location;
+
+class LocationQuery extends Query
+{
+ const SBB_SEARCH_MODE = '1';
+
+ private static $locationTypes = array(
+ 'all' => 'ALLTYPE',
+ 'station' => 'ST',
+ 'address' => 'ADR',
+ 'poi' => 'POI',
+ );
+
+ /**
+ * @var array
+ */
+ public $query;
+
+ public $type;
+
+ /**
+ * Finds all stations, locations and poi matching the search query.
+ *
+ * @param string|array $query Search query (e.g. Ber)
+ * @param string $type Location types to return (all, station, address, poi)
+ */
+ public function __construct($query, $type = null) {
+
+ // convert query to array
+ if (!is_array($query)) {
+ $query = array($query);
+ }
+ $this->query = $query;
+
+ $this->type = $type;
+ }
+
+ public function toXml() {
+
+ $request = $this->createRequest();
+
+ foreach ($this->query as $key => $value) {
+
+ $local = $request->addChild('LocValReq');
+ $local['id'] = $key;
+ $local['sMode'] = self::SBB_SEARCH_MODE;
+
+ $location = $local->addChild('ReqLoc');
+ $location['match'] = $value;
+
+ if (!isset(self::$locationTypes[$this->type])) {
+ $this->type = 'all'; // default type
+ }
+ $location['type'] = self::$locationTypes[$this->type];
+ }
+
+ return $request->asXML();
+ }
+
+ /**
+ * @return array
+ */
+ public function getLocationTypes()
+ {
+ return self::$locationTypes;
+ }
+}
@@ -0,0 +1,23 @@
+<?php
+
+namespace Transport\Entity\Location;
+
+/**
+ * Represents a Poi we received as response
+ *
+ * <Poi name="Ittigen, Bahnhof" score="100" type="WGS84" x="7478189" y="46976494" />
+ */
+class Poi extends Location
+{
+ /**
+ * {@inheritDoc}
+ */
+ static public function createFromXml(\SimpleXMLElement $xml, Location $obj = null)
+ {
+ if (!$obj) {
+ $obj = new Poi;
+ }
+ return parent::createFromXml($xml, $obj);
+ }
+}
+
Oops, something went wrong. Retry.

0 comments on commit 0217404

Please sign in to comment.