- Introduction
- Installation
- Running Specifications
- Architecture
- Destinations
- Boarding Cards
- Trip Sorter
To install the TripSorter, simply run the composer install command from the root directory of the project (make sure you have Composer installed):
php composer.phar install --prefer-dist
To run the phpspec specifications, simply execute the following command from the root directory of the project:
bin/phpspec run
The basic interface of destinations is the following:
interface DestinationInterface
{
/**
* Prints the destination in a human-readable form.
*
* @return string
*/
public function __toString();
}
The basic interface of boarding cards is the following:
interface BoardingCardInterface
{
/**
* @return \TripSorter\Destination\DestinationInterface
*/
public function getFrom();
/**
* @return \TripSorter\Destination\DestinationInterface
*/
public function getTo();
}
The architecture is also composed by special types of boarding cards:
- BusBoardingCardInterface
- FlightBoardingCardInterface
- TrainBoardingCardInterface
The class responsible for sorting an array of boarding cards is the TripSorter
:
class TripSorter
{
public function sort(array $boardingCards)
{
$sorted = [array_pop($boardingCards)];
while (count($boardingCards) > 0) {
foreach ($boardingCards as $key => $card) {
if ( ! $card instanceof BoardingCardInterface) {
unset($boardingCards[$key]);
continue;
}
if (end($sorted)->getTo()->__toString() === $card->getFrom()->__toString()) {
array_push($sorted, $card);
} elseif (reset($sorted)->getFrom()->__toString() === $card->getTo()->__toString()) {
array_unshift($sorted, $card);
}
unset($boardingCards[$key]);
}
}
return $sorted;
}
}
by Daniel Ribeiro.