From eba7c836b9d74e3322cdef1b49561da463b032dd Mon Sep 17 00:00:00 2001 From: bijanmmarkes Date: Thu, 2 Jun 2022 10:38:37 -0700 Subject: [PATCH 1/2] Creating new Gtfs Directions support (gtfs+), custom methods to get bound and cardinal strings. --- .../Gtfs/Model/Entities/Directions.php | 156 ++++++++++++++++++ .../Gtfs/Model/Files/GtfsDirectionsFile.php | 31 ++++ src/Trafiklab/Gtfs/Model/GtfsArchive.php | 9 + 3 files changed, 196 insertions(+) create mode 100644 src/Trafiklab/Gtfs/Model/Entities/Directions.php create mode 100644 src/Trafiklab/Gtfs/Model/Files/GtfsDirectionsFile.php diff --git a/src/Trafiklab/Gtfs/Model/Entities/Directions.php b/src/Trafiklab/Gtfs/Model/Entities/Directions.php new file mode 100644 index 0000000..00200b2 --- /dev/null +++ b/src/Trafiklab/Gtfs/Model/Entities/Directions.php @@ -0,0 +1,156 @@ + $value) { + $this->$variable = $value; + } + $this->archive = $archive; + } + + /** + * @return string + */ + public function getRouteId(): string + { + return $this->route_id; + } + + /** + * @param string $route_id + */ + public function setRouteId(string $route_id): void + { + $this->route_id = $route_id; + } + + /** + * @return int + */ + public function getDirectionId(): int + { + return $this->direction_id; + } + + /** + * @param int $direction_id + */ + public function setDirectionId(int $direction_id): void + { + $this->direction_id = $direction_id; + } + + /** + * @return string + */ + public function getDirection(): string + { + return $this->direction; + } + + /** + * @param string $direction + */ + public function setDirection(string $direction): void + { + $this->direction = $direction; + } + + /** + * @return GtfsArchive + */ + public function getArchive(): GtfsArchive + { + return $this->archive; + } + + /** + * @param GtfsArchive $archive + */ + public function setArchive(GtfsArchive $archive): void + { + $this->archive = $archive; + } + + /** + * Get the Abbreviation of the Cardinal Direction. + * - Ex: North = N, West = W, South = S, Southeast = SE. + * @param bool $adjective - Whether or not to use NorthBound (NB) + * - Ex: North = NB, NorthEast = NB, South = SB, SouthWest = SB. + * @return string + * * Possible Values: + * - North, South, East, West, Northeast, Northwest, Southeast, Southwest + */ + public function getCardinalDirectionAbrv(bool $adjective = false): string + { + switch (strtolower($this->getDirection())) { + case 'north': + case 'northeast': + case 'northwest': + return $adjective ? 'NB' : 'N'; + case 'south': + case 'southeast': + case 'southwest': + return $adjective ? 'SB' : 'S'; + case 'west': + return $adjective ? 'WB' : 'W'; + case 'east': + return $adjective ? 'EB' : 'E'; + default: + return ''; + } + } + + /** + * Return the value of Inbound(1) or Outbound(0) depending on the value of direction_id. + * Inbound assumes that the vehicle is arriving at said stop or location. + * Outbound assumes that the vehicle is leaving said stop or location. + * @return string + */ + public function getBoundDirection(): string + { + return $this->getDirectionId() === 0 ? 'Outbound' : 'Inbound'; + } +} diff --git a/src/Trafiklab/Gtfs/Model/Files/GtfsDirectionsFile.php b/src/Trafiklab/Gtfs/Model/Files/GtfsDirectionsFile.php new file mode 100644 index 0000000..d40b333 --- /dev/null +++ b/src/Trafiklab/Gtfs/Model/Files/GtfsDirectionsFile.php @@ -0,0 +1,31 @@ +dataRows = GtfsParserUtil::deserializeCSV( + $parent, + $filePath, + Directions::class + ); + } + + /** + * Get the file data as an array of its rows. + * + * @return Directions[] + */ + public function getDirections(): array + { + return $this->dataRows; + } +} diff --git a/src/Trafiklab/Gtfs/Model/GtfsArchive.php b/src/Trafiklab/Gtfs/Model/GtfsArchive.php index b43408b..4dc20a2 100644 --- a/src/Trafiklab/Gtfs/Model/GtfsArchive.php +++ b/src/Trafiklab/Gtfs/Model/GtfsArchive.php @@ -8,6 +8,7 @@ use Trafiklab\Gtfs\Model\Files\GtfsAgencyFile; use Trafiklab\Gtfs\Model\Files\GtfsCalendarDatesFile; use Trafiklab\Gtfs\Model\Files\GtfsCalendarFile; +use Trafiklab\Gtfs\Model\Files\GtfsDirectionsFile; use Trafiklab\Gtfs\Model\Files\GtfsFeedInfoFile; use Trafiklab\Gtfs\Model\Files\GtfsFrequenciesFile; use Trafiklab\Gtfs\Model\Files\GtfsRoutesFile; @@ -38,6 +39,7 @@ class GtfsArchive private const PATHWAYS_TXT = "pathways.txt"; // Unsupported at this moment private const LEVELS_TXT = "levels.txt"; // Unsupported at this moment private const FEED_INFO_TXT = "feed_info.txt"; + private const DIRECTIONS_TXT = "directions.txt"; // Experimental GTFS+ feed. private const TEMP_ROOT = "/tmp/gtfs/"; @@ -320,6 +322,13 @@ public function getFrequenciesFile(): GtfsFrequenciesFile { return $this->loadGtfsFileThroughCache(__METHOD__, self::FREQUENCIES_TXT, GtfsFrequenciesFile::class); } + /** + * @return GtfsDirectionsFile|null + */ + public function getDirectionsFile(): ?GtfsDirectionsFile + { + return $this->loadGtfsFileThroughCache(__METHOD__, self::DIRECTIONS_TXT, GtfsDirectionsFile::class); + } /** * Delete the uncompressed files. This should be done as a cleanup when you're ready. From b3fd8bc169d8ead7f789abddab82e105be75c0fc Mon Sep 17 00:00:00 2001 From: bijanmmarkes Date: Thu, 2 Jun 2022 10:39:02 -0700 Subject: [PATCH 2/2] Adding directions.txt to test and test cases for methods. --- tests/Resources/Gtfs/minified-test-feed.zip | Bin 211100 -> 211814 bytes .../Gtfs/GtfsArchiveIntegrationTest.php | 29 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tests/Resources/Gtfs/minified-test-feed.zip b/tests/Resources/Gtfs/minified-test-feed.zip index 84dd5e9687cad8cd0b80ff3e28fd99036e0dc76c..a19330a74f4858ac6345e0cfea8d917f55110efd 100644 GIT binary patch delta 1110 zcmbR9k>}Yro(YER9xrDk0r5r)CPpy3`}K^;j*R!%Jt5+g)tC;$)J*=s)Ls9^?@$N> z6nqBKd<+T9*0WckX-z`!B`)DhZi z&3DLvr>%T`6LZQ6?%c$dE=JCS2M%;7Y`nrDve+bWeg9-3#cl8FZ_Z0zFx&K0guhip zSv^z0LaUAZ%=K5Ce*cz8&@7mElRK1l@0zZR@?}{}zA!JjI>K=Y6#&Q{~@c>6tP!?>DTyP-a-HzGwDN<^XSY z4pp9xcawqc1Nkbzn;Ga<4j@GKuPh^j9)l#%!}0OHj?VtU5&C-Zc>E3tO6~e=1-4I; zsgfN`eTs^X0%8%zg%!5B1RvrNeV(2m;D7G?Rc{@Q6P{POJ=ba!p7lQGfAWlX`-Qa{uiz_C?%ZRe5=Ide)xp(a{Z>d?t9!!F3)dbvCAf{K(Gn za$(%xjX?j(GEUBCHiJj{8sGuVW3P0!Vuk8fx6`w*clj#GZG7s6lTdH6bhO%TQRXVPWLrumP84X z>7C}xa==iZYQ5SSJyd d1T6>9GjV`7E6`gE47@}>X&$&QTo*d2i4_CU58(_uLK15@|pCRQyt ze>3YLIJ<^zD~!F_f}N=f&T(wiV+C`7CN`I~wU@OqZZB(Ny0#7`I6c^u`5c_1Va9wA z=BVi(%$Q?ft^#pnrstb8+ro^7h@3ZPHiwH`uwb@gDrBC{W63N57X)i`w`7)Mwq;?M zoX94EDl;D_!}OD7`hH7hQB;}lKpCZfbqox}CHV#MC7HRY#d;+bC7~g_4D4OvVM)+1 zg195tidjz9q6MNDp-!gzQ4B~O5JS|h0;*FKG-hTfDN4*MPD?F9s9hxiF%zm*b~>9i jv#jhZu-eQ5gt~P|>Kv?@CHS}*xEY=@GcYVP0TK)VycUdZ diff --git a/tests/Trafiklab/Gtfs/GtfsArchiveIntegrationTest.php b/tests/Trafiklab/Gtfs/GtfsArchiveIntegrationTest.php index 67a6e2d..96ace02 100644 --- a/tests/Trafiklab/Gtfs/GtfsArchiveIntegrationTest.php +++ b/tests/Trafiklab/Gtfs/GtfsArchiveIntegrationTest.php @@ -224,4 +224,33 @@ public function testGetAgency() self::assertNull($agencies[1]->getAgencyEmail(), 'Agency Email is not null!'); self::assertEquals('000-000-0000', $agencies[1]->getAgencyPhone(), 'Agency Phone is not equal to 000-000-0000'); } + + /** + * Test the Directions GTFSArchive. + * @return void + */ + public function testGetDirections() + { + /** Directions file should be retrieved. */ + $directions = $this->gtfsArchive->getDirectionsFile()->getDirections(); + /** Test correct number of directions parsed. */ + self::assertEquals(17, count($directions), 'Parsed incorrect number of Directions.'); + /** Test Types */ + self::assertIsString($directions[0]->getRouteId(), 'route_id should be a string.'); + self::assertIsString($directions[0]->getDirection(), 'direction should be a string.'); + self::assertIsNumeric($directions[0]->getDirectionId(), 'direction_id should be numeric (0,1).'); + /** Test Getters/Validity */ + self::assertEquals(0, $directions[16]->getDirectionId(), 'direction_id should be 0.'); + self::assertEquals('North', $directions[0]->getDirection(), 'Should Equal North.'); + self::assertEquals('1', $directions[0]->getRouteId(), 'Should equal 1'); + self::assertEquals('Northeast', $directions[4]->getDirection(), 'Should Equal Northeast.'); + self::assertEquals('A Loop', $directions[15]->getDirection(), 'Should Equal A Loop.'); + /** Test Abbreviations method. */ + self::assertEquals('N', $directions[0]->getCardinalDirectionAbrv(), 'Should Equal N'); + self::assertEquals('NB', $directions[0]->getCardinalDirectionAbrv(true), 'Should Equal NB'); + self::assertEquals('SB', $directions[6]->getCardinalDirectionAbrv(true), 'Should Equal SB'); + /** Test DirectionBound method (Inbound/Outbound) */ + self::assertEquals("Inbound", $directions[0]->getBoundDirection(), 'Should be equal to Inbound.'); + self::assertEquals("Outbound", $directions[16]->getBoundDirection(), 'Should be equal to Outbound.'); + } }