1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class for working with Trips. |
7
|
|
|
*/ |
8
|
|
|
class TripSorter |
9
|
|
|
{ |
10
|
|
|
/** @var BoardingCard[] */ |
11
|
|
|
private $cards; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* TripSorter constructor. |
15
|
|
|
* |
16
|
|
|
* @param BoardingCard[] $cards |
17
|
|
|
*/ |
18
|
|
|
public function __construct(array $cards = []) |
19
|
|
|
{ |
20
|
|
|
$this->setCards($cards); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function getCards() |
27
|
|
|
{ |
28
|
|
|
$cards = []; |
29
|
|
|
foreach ($this->cards as $card) { |
30
|
|
|
$cards[] = $card->toArray(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $cards; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param BoardingCard[] $cards |
38
|
|
|
* |
39
|
|
|
* @return TripSorter |
40
|
|
|
*/ |
41
|
|
|
public function setCards(array $cards = []) |
42
|
|
|
{ |
43
|
|
|
$this->cards = []; |
44
|
|
|
foreach ($cards as $card) { |
45
|
|
|
$this->cards[] = new BoardingCard($card); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sort an array of trip cards. |
53
|
|
|
* |
54
|
|
|
* Example: |
55
|
|
|
* $cards = [ |
56
|
|
|
* ['from' => 'Madrid', 'to' => 'Barcelona', 'type' => 'train', 'number' => '78A', 'info' => 'Sit in seat 45B.'], |
57
|
|
|
* ['from' => 'Barcelona', 'to' => 'Gerona Airport', 'type' => 'airport bus', 'number' => '', 'info' => 'No seat assignment.'], |
58
|
|
|
* ['from' => 'Gerona Airport', 'to' => 'Stockholm', 'type' => 'flight', 'number' => 'SK455', 'seat' => '3A', 'info' => 'Gate 45B, seat 3A. Baggage drop at ticket counter 344.'], |
59
|
|
|
* ['from' => 'Stockholm', 'to' => 'New York JFK', 'type' => 'flight', 'number' => 'SK455', 'seat' => '7B', 'info' => 'Gate 22. Baggage will we automatically transferred from your last leg.'], |
60
|
|
|
* ]; |
61
|
|
|
* $result = TripSorter::sort($cards); |
62
|
|
|
* |
63
|
|
|
* returns |
64
|
|
|
* [ |
65
|
|
|
* 'Take train 78A from Madrid to Barcelona. Sit in seat 45B.', |
66
|
|
|
* 'Take airport bus from Barcelona to Gerona Airport. No seat assignment.', |
67
|
|
|
* 'From Gerona Airport, take flight SK455 to Stockholm. Gate 45B, seat 3A. Baggage drop at ticket counter 344.', |
68
|
|
|
* 'From Stockholm, take flight SK455 to New York JFK. Gate 22. Baggage will we automatically transferred from your last leg.', |
69
|
|
|
* 'You have arrived at your final destination.', |
70
|
|
|
* ]; |
71
|
|
|
* |
72
|
|
|
* @param array $cards |
73
|
|
|
* |
74
|
|
|
* @static |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public static function sort(array $cards = []) |
78
|
|
|
{ |
79
|
|
|
$instance = new self($cards); |
80
|
|
|
$instance->sortCards(); |
81
|
|
|
$result = $instance->output(); |
82
|
|
|
|
83
|
|
|
return $result; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sorts the boarding cards. |
88
|
|
|
*/ |
89
|
|
|
public function sortCards() |
90
|
|
|
{ |
91
|
|
|
$count = 0; |
92
|
|
|
$fromCity = []; |
93
|
|
|
$toCity = []; |
94
|
|
|
foreach ($this->cards as $card) { |
95
|
|
|
$toCity[$card->getTo()] = $count; |
96
|
|
|
$fromCity[$card->getFrom()] = $count++; |
97
|
|
|
} |
98
|
|
|
if (empty($fromCity)) { |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
$diff = array_keys(array_diff_key($fromCity, $toCity)); |
102
|
|
|
if (count($diff) !== 1) { |
103
|
|
|
throw new \RuntimeException('There should be one starting point'); |
104
|
|
|
} |
105
|
|
|
$city = $diff[0]; |
106
|
|
|
$sorted = []; |
107
|
|
|
while (isset($fromCity[$city])) { |
108
|
|
|
$index = $fromCity[$city]; |
109
|
|
|
$sorted[] = $this->cards[$index]; |
110
|
|
|
$city = $this->cards[$index]->getTo(); // Barcelona |
111
|
|
|
} |
112
|
|
|
$this->cards = $sorted; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Formats the output. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function output() |
123
|
|
|
{ |
124
|
|
|
$output = []; |
125
|
|
|
foreach ($this->cards as $card) { |
126
|
|
|
if ($card->getType() === 'flight') { |
127
|
|
|
$output[] = 'From ' . $card->getFrom() |
128
|
|
|
. ', take ' . $card->getType() |
129
|
|
|
. ' ' . $card->getNumber() |
130
|
|
|
. ' to ' . $card->getTo() |
131
|
|
|
. '. ' . $card->getInfo(); |
132
|
|
|
} else { |
133
|
|
|
$output[] = 'Take ' . $card->getType() |
134
|
|
|
. ' ' . $card->getNumber() |
135
|
|
|
. ' from ' . $card->getFrom() |
136
|
|
|
. ' to ' . $card->getTo() |
137
|
|
|
. '. ' . $card->getInfo(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
$output[] = 'You have arrived at your final destination.'; |
141
|
|
|
|
142
|
|
|
return $output; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|