1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TK\API\ValueObject\Factory; |
5
|
|
|
|
6
|
|
|
use TK\API\ValueObject\GetAvailabilityParameters; |
7
|
|
|
use TK\API\ValueObject\Location; |
8
|
|
|
use TK\API\ValueObject\DepartureDateTime; |
9
|
|
|
use TK\API\ValueObject\OriginDestinationInformation; |
10
|
|
|
use TK\API\ValueObject\PassengerTypeQuantity; |
11
|
|
|
use DateTimeImmutable; |
12
|
|
|
use TK\API\Exception\InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
final class GetAvailabilityParametersFactory implements ValueObjectFactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param array $parameters |
18
|
|
|
* @return GetAvailabilityParameters |
19
|
|
|
* @throws \Exception |
20
|
|
|
*/ |
21
|
|
|
public static function createFromArray(array $parameters) : GetAvailabilityParameters |
22
|
|
|
{ |
23
|
|
|
$originDestinationInformations = []; |
24
|
|
|
foreach ($parameters['OriginDestinationInformation'] as $originDestinationInformation) { |
25
|
|
|
$originLocation = new Location( |
26
|
|
|
$originDestinationInformation['OriginLocation']['LocationCode'], |
27
|
|
|
$originDestinationInformation['OriginLocation']['MultiAirportCityInd'] |
28
|
|
|
); |
29
|
|
|
$destinationLocation = new Location( |
30
|
|
|
$originDestinationInformation['DestinationLocation']['LocationCode'], |
31
|
|
|
$originDestinationInformation['DestinationLocation']['MultiAirportCityInd'] |
32
|
|
|
); |
33
|
|
|
$departureDateTime = (new DepartureDateTime( |
34
|
|
|
DateTimeImmutable::createFromFormat('dM', $originDestinationInformation['DepartureDateTime']['Date']), |
|
|
|
|
35
|
|
|
$originDestinationInformation['DepartureDateTime']['WindowAfter'], |
36
|
|
|
$originDestinationInformation['DepartureDateTime']['WindowBefore'] |
37
|
|
|
))->withDateFormat('dM'); |
38
|
|
|
$originDestinationInformationObject = new OriginDestinationInformation( |
39
|
|
|
$departureDateTime, |
40
|
|
|
$originLocation, |
41
|
|
|
$destinationLocation |
42
|
|
|
); |
43
|
|
|
if (array_key_exists('CabinPreferences', $originDestinationInformation)) { |
44
|
|
|
foreach ($originDestinationInformation['CabinPreferences'] as $cabinPreference) { |
45
|
|
|
$originDestinationInformationObject = $originDestinationInformationObject |
46
|
|
|
->withCabinPreferences($cabinPreference['Cabin']); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
$originDestinationInformations[] = $originDestinationInformationObject; |
50
|
|
|
} |
51
|
|
|
$passengerTypeQuantityObject = new PassengerTypeQuantity(); |
52
|
|
|
foreach ($parameters['PassengerTypeQuantity'] as $passengerTypeQuantity) { |
53
|
|
|
$passengerTypeQuantityObject->withQuantity( |
54
|
|
|
$passengerTypeQuantity['Code'], |
55
|
|
|
$passengerTypeQuantity['Quantity'] |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
$getAvailabilityParameters = new GetAvailabilityParameters( |
59
|
|
|
$parameters['ReducedDataIndicator'], |
60
|
|
|
$parameters['RoutingType'], |
61
|
|
|
$passengerTypeQuantityObject |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
if (array_key_exists('TargetSource', $parameters)) { |
65
|
|
|
$getAvailabilityParameters = $getAvailabilityParameters->withTargetSource(); |
66
|
|
|
} |
67
|
|
|
foreach ($originDestinationInformations as $originDestinationInformationObject) { |
68
|
|
|
$getAvailabilityParameters = $getAvailabilityParameters |
69
|
|
|
->withOriginDestinationInformation($originDestinationInformationObject); |
70
|
|
|
} |
71
|
|
|
return $getAvailabilityParameters; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $json |
76
|
|
|
* @return GetAvailabilityParameters |
77
|
|
|
* @throws InvalidArgumentException |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
|
|
public static function createFromJson(string $json) : GetAvailabilityParameters |
81
|
|
|
{ |
82
|
|
|
$parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY); |
83
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
84
|
|
|
throw new InvalidArgumentException( |
85
|
|
|
'GetAvailabilityParametersFactory Error: ' . json_last_error_msg() |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
return self::createFromArray($parameters); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|