1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TK\API\ValueObject\Factory; |
5
|
|
|
|
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use TK\API\Exception\InvalidArgumentException; |
8
|
|
|
use TK\API\ValueObject\CalculateAwardMilesWithTaxParameters; |
9
|
|
|
|
10
|
|
|
class CalculateAwardMilesWithTaxParametersFactory implements ValueObjectFactoryInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param array $parameters |
14
|
|
|
* @return CalculateAwardMilesWithTaxParameters |
15
|
|
|
* @throws \Exception |
16
|
|
|
*/ |
17
|
|
|
public static function createFromArray(array $parameters) : CalculateAwardMilesWithTaxParameters |
18
|
|
|
{ |
19
|
|
|
$calculateAwardMilesWithTaxParameters = new CalculateAwardMilesWithTaxParameters( |
20
|
|
|
$parameters['awardType'] |
21
|
|
|
); |
22
|
|
|
if (array_key_exists('wantMoreMiles', $parameters) && $parameters['wantMoreMiles'] === 'T') { |
23
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters->withSeatGuaranteed(); |
24
|
|
|
} |
25
|
|
|
if (array_key_exists('isOneWay', $parameters) && $parameters['isOneWay'] === 'T') { |
26
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters->withOneWay(); |
27
|
|
|
} |
28
|
|
|
if (array_key_exists('departureOrigin', $parameters)) { |
29
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters |
30
|
|
|
->withDepartureOrigin($parameters['departureOrigin']); |
31
|
|
|
} |
32
|
|
|
if (array_key_exists('departureDestination', $parameters)) { |
33
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters |
34
|
|
|
->withDepartureDestination($parameters['departureDestination']); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (array_key_exists('departureDateDay', $parameters) && |
38
|
|
|
array_key_exists('departureDateMonth', $parameters) && |
39
|
|
|
array_key_exists('departureDateYear', $parameters) |
40
|
|
|
) { |
41
|
|
|
$departureDate = $parameters['departureDateYear'] . '-' . $parameters['departureDateMonth'] . |
42
|
|
|
'-' . $parameters['departureDateDay']; |
43
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters |
44
|
|
|
->withDepartureDate(DateTimeImmutable::createFromFormat('Y-m-d', $departureDate)); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (array_key_exists('arrivalOrigin', $parameters)) { |
48
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters |
49
|
|
|
->withArrivalOrigin($parameters['arrivalOrigin']); |
50
|
|
|
} |
51
|
|
|
if (array_key_exists('arrivalDestination', $parameters)) { |
52
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters |
53
|
|
|
->withArrivalDestination($parameters['arrivalDestination']); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (array_key_exists('arrivalDateDay', $parameters) && |
57
|
|
|
array_key_exists('arrivalDateMonth', $parameters) && |
58
|
|
|
array_key_exists('arrivalDateYear', $parameters) |
59
|
|
|
) { |
60
|
|
|
$arrivalDate = $parameters['arrivalDateYear'] . '-' . $parameters['arrivalDateMonth'] . |
61
|
|
|
'-' . $parameters['arrivalDateDay']; |
62
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters |
63
|
|
|
->withArrivalDate(DateTimeImmutable::createFromFormat('Y-m-d', $arrivalDate)); |
|
|
|
|
64
|
|
|
} |
65
|
|
|
if (array_key_exists('ptcType', $parameters)) { |
66
|
|
|
$calculateAwardMilesWithTaxParameters = $calculateAwardMilesWithTaxParameters |
67
|
|
|
->withPassengerType($parameters['ptcType']); |
68
|
|
|
} |
69
|
|
|
return $calculateAwardMilesWithTaxParameters; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $json |
74
|
|
|
* @return CalculateAwardMilesWithTaxParameters |
75
|
|
|
* @throws InvalidArgumentException |
76
|
|
|
* @throws \Exception |
77
|
|
|
*/ |
78
|
|
|
public static function createFromJson(string $json) : CalculateAwardMilesWithTaxParameters |
79
|
|
|
{ |
80
|
|
|
$parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY); |
81
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
82
|
|
|
throw new InvalidArgumentException( |
83
|
|
|
'CalculateAwardMilesWithTaxParametersFactory Error: ' . json_last_error_msg() |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
return self::createFromArray($parameters); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|