1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TK\API\ValueObject\Factory; |
5
|
|
|
|
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use TK\API\ValueObject\Location; |
8
|
|
|
use TK\API\ValueObject\DepartureDateTime; |
9
|
|
|
use TK\API\ValueObject\OriginDestinationInformation; |
10
|
|
|
use TK\API\ValueObject\AirScheduleRQ; |
11
|
|
|
use TK\API\ValueObject\GetTimetableParameters; |
12
|
|
|
use TK\API\Exception\InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
final class GetTimetableParametersFactory implements ValueObjectFactoryInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param array $parameters |
18
|
|
|
* @return GetTimetableParameters |
19
|
|
|
* @throws \Exception |
20
|
|
|
*/ |
21
|
|
|
public static function createFromArray(array $parameters) : GetTimetableParameters |
22
|
|
|
{ |
23
|
|
|
$originDestinationInformation = $parameters['OTA_AirScheduleRQ']['OriginDestinationInformation']; |
24
|
|
|
$originLocation = new Location( |
25
|
|
|
$originDestinationInformation['OriginLocation']['LocationCode'], |
26
|
|
|
$originDestinationInformation['OriginLocation']['MultiAirportCityInd'] |
27
|
|
|
); |
28
|
|
|
$destinationLocation = new Location( |
29
|
|
|
$originDestinationInformation['DestinationLocation']['LocationCode'], |
30
|
|
|
$originDestinationInformation['DestinationLocation']['MultiAirportCityInd'] |
31
|
|
|
); |
32
|
|
|
$departureDateTime = new DepartureDateTime( |
33
|
|
|
DateTimeImmutable::createFromFormat('Y-m-d', $originDestinationInformation['DepartureDateTime']['Date']), |
|
|
|
|
34
|
|
|
'P3D', |
35
|
|
|
'P3D' |
36
|
|
|
); |
37
|
|
|
$originDestinationInformationObject = new OriginDestinationInformation( |
38
|
|
|
$departureDateTime, |
39
|
|
|
$originLocation, |
40
|
|
|
$destinationLocation |
41
|
|
|
); |
42
|
|
|
$airScheduleRQ = new AirScheduleRQ($originDestinationInformationObject); |
43
|
|
|
if (array_key_exists('AirlineCode', $parameters['OTA_AirScheduleRQ'])) { |
44
|
|
|
$airScheduleRQ = $airScheduleRQ->withAirlineCode($parameters['OTA_AirScheduleRQ']['AirlineCode']); |
45
|
|
|
} |
46
|
|
|
if (array_key_exists('FlightTypePref', $parameters['OTA_AirScheduleRQ']) && |
47
|
|
|
$parameters['OTA_AirScheduleRQ']['FlightTypePref']['DirectAndNonStopOnlyInd'] === true) { |
48
|
|
|
$airScheduleRQ = $airScheduleRQ->withDirectAndNonStopOnlyInd(); |
49
|
|
|
} |
50
|
|
|
$getTimetableParameters = new GetTimetableParameters( |
51
|
|
|
$airScheduleRQ, |
52
|
|
|
$parameters['scheduleType'], |
53
|
|
|
$parameters['tripType'] |
54
|
|
|
); |
55
|
|
|
if (array_key_exists('returnDate', $parameters)) { |
56
|
|
|
$returnDate = DateTimeImmutable::createFromFormat('Y-m-d', $parameters['returnDate']); |
57
|
|
|
$getTimetableParameters = $getTimetableParameters->withReturnDate($returnDate); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
return $getTimetableParameters; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $json |
64
|
|
|
* @return GetTimetableParameters |
65
|
|
|
* @throws InvalidArgumentException |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
public static function createFromJson(string $json) : GetTimetableParameters |
69
|
|
|
{ |
70
|
|
|
$parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY); |
71
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
72
|
|
|
throw new InvalidArgumentException( |
73
|
|
|
'GetTimetableParametersFactory Error: ' . json_last_error_msg() |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
return self::createFromArray($parameters); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|