1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TK\API\ValueObject; |
5
|
|
|
|
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use TK\API\Exception\InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
class CalculateAwardMilesWithTaxParameters implements ValueObjectInterface |
10
|
|
|
{ |
11
|
|
|
public const AWARD_TYPE_ECONOMY = 'E'; |
12
|
|
|
public const AWARD_TYPE_BUSINESS = 'B'; |
13
|
|
|
public const AWARD_TYPE_FIRST_CLASS = 'C'; |
14
|
|
|
|
15
|
|
|
private static $awardTypeEnum = ['E', 'B', 'C']; |
16
|
|
|
|
17
|
|
|
private $awardType; |
18
|
|
|
private $wantMoreMiles; |
19
|
|
|
private $isOneWay = 'F'; |
20
|
|
|
private $departureOrigin; |
21
|
|
|
private $departureDestination; |
22
|
|
|
private $departureDate; |
23
|
|
|
private $arrivalOrigin; |
24
|
|
|
private $arrivalDestination; |
25
|
|
|
private $arrivalDate; |
26
|
|
|
private $ptcType; |
27
|
|
|
|
28
|
|
|
public function __construct(string $awardType) |
29
|
|
|
{ |
30
|
|
|
if (!\in_array($awardType, self::$awardTypeEnum, true)) { |
31
|
|
|
throw new InvalidArgumentException( |
32
|
|
|
'Invalid awardType value. Possible values are "' . |
33
|
|
|
implode(', ', self::$awardTypeEnum) . '"' . |
34
|
|
|
' but provided value is "' . $awardType . '"' |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
$this->awardType = $awardType; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function withDepartureOrigin(string $iataCode) : CalculateAwardMilesWithTaxParameters |
41
|
|
|
{ |
42
|
|
|
$this->departureOrigin = $this->getIataCode($iataCode); |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function withDepartureDate(DateTimeImmutable $date) : CalculateAwardMilesWithTaxParameters |
47
|
|
|
{ |
48
|
|
|
$this->departureDate = $date; |
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function withDepartureDestination(string $iataCode) : CalculateAwardMilesWithTaxParameters |
53
|
|
|
{ |
54
|
|
|
$this->departureDestination = $this->getIataCode($iataCode); |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function withArrivalOrigin(string $iataCode) : CalculateAwardMilesWithTaxParameters |
59
|
|
|
{ |
60
|
|
|
$this->arrivalOrigin = $this->getIataCode($iataCode); |
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function withArrivalDate(DateTimeImmutable $date) : CalculateAwardMilesWithTaxParameters |
65
|
|
|
{ |
66
|
|
|
$this->arrivalDate = $date; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function withArrivalDestination(string $iataCode) : CalculateAwardMilesWithTaxParameters |
71
|
|
|
{ |
72
|
|
|
$this->arrivalDestination = $this->getIataCode($iataCode); |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function withPassengerType(string $passengerType) : CalculateAwardMilesWithTaxParameters |
77
|
|
|
{ |
78
|
|
|
$this->ptcType = $passengerType; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function withOneWay() : CalculateAwardMilesWithTaxParameters |
83
|
|
|
{ |
84
|
|
|
$this->isOneWay = 'T'; |
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function withSeatGuaranteed() : CalculateAwardMilesWithTaxParameters |
89
|
|
|
{ |
90
|
|
|
$this->wantMoreMiles = 'T'; |
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function getIataCode(string $iataCode) : string |
95
|
|
|
{ |
96
|
|
|
if (!preg_match('/^[A-Z]{3}$/', $iataCode)) { |
97
|
|
|
{ |
98
|
|
|
throw new InvalidArgumentException( |
99
|
|
|
sprintf( |
100
|
|
|
'Invalid OriginLocation.LocationCode value (%s) provided. Valid IATA code must be used', |
101
|
|
|
$iataCode |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return $iataCode; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getValue() : array |
110
|
|
|
{ |
111
|
|
|
$calculateAwardMilesWithTaxParameters = [ |
112
|
|
|
'awardType' => $this->awardType |
113
|
|
|
]; |
114
|
|
|
if ($this->wantMoreMiles !== null) { |
115
|
|
|
$calculateAwardMilesWithTaxParameters['wantMoreMiles'] = 'T'; |
116
|
|
|
} |
117
|
|
|
if ($this->isOneWay !== null) { |
118
|
|
|
$calculateAwardMilesWithTaxParameters['isOneWay'] = 'T'; |
119
|
|
|
} |
120
|
|
|
if ($this->departureOrigin !== null) { |
121
|
|
|
$calculateAwardMilesWithTaxParameters['departureOrigin'] = $this->departureOrigin; |
122
|
|
|
} |
123
|
|
|
if ($this->departureDestination !== null) { |
124
|
|
|
$calculateAwardMilesWithTaxParameters['departureDestination'] = $this->departureDestination; |
125
|
|
|
} |
126
|
|
|
if ($this->departureDate !== null) { |
127
|
|
|
$calculateAwardMilesWithTaxParameters['departureDateDay'] = (int) $this->departureDate->format('j'); |
128
|
|
|
$calculateAwardMilesWithTaxParameters['departureDateMonth'] = (int) $this->departureDate->format('n'); |
129
|
|
|
$calculateAwardMilesWithTaxParameters['departureDateYear'] = (int) $this->departureDate->format('Y'); |
130
|
|
|
} |
131
|
|
|
if ($this->arrivalOrigin !== null) { |
132
|
|
|
$calculateAwardMilesWithTaxParameters['arrivalOrigin'] = $this->arrivalOrigin; |
133
|
|
|
} |
134
|
|
|
if ($this->arrivalDestination !== null) { |
135
|
|
|
$calculateAwardMilesWithTaxParameters['arrivalDestination'] = $this->arrivalDestination; |
136
|
|
|
} |
137
|
|
|
if ($this->arrivalDate !== null) { |
138
|
|
|
$calculateAwardMilesWithTaxParameters['arrivalDateDay'] = (int) $this->arrivalDate->format('j'); |
139
|
|
|
$calculateAwardMilesWithTaxParameters['arrivalDateMonth'] = (int) $this->arrivalDate->format('n'); |
140
|
|
|
$calculateAwardMilesWithTaxParameters['arrivalDateYear'] = (int) $this->arrivalDate->format('Y'); |
141
|
|
|
} |
142
|
|
|
if ($this->ptcType !== null) { |
143
|
|
|
$calculateAwardMilesWithTaxParameters['ptcType'] = $this->ptcType; |
144
|
|
|
} |
145
|
|
|
return $calculateAwardMilesWithTaxParameters; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|