|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace TK\SDK\ValueObject\Factory; |
| 5 | + |
| 6 | +use TK\SDK\ValueObject\GetAvailabilityParameters; |
| 7 | +use TK\SDK\ValueObject\Location; |
| 8 | +use TK\SDK\ValueObject\DepartureDateTime; |
| 9 | +use TK\SDK\ValueObject\OriginDestinationInformation; |
| 10 | +use TK\SDK\ValueObject\PassengerTypeQuantity; |
| 11 | +use DateTimeImmutable; |
| 12 | +use TK\SDK\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->withOriginDestinationInformation($originDestinationInformationObject); |
| 69 | + } |
| 70 | + return $getAvailabilityParameters; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param string $json |
| 75 | + * @return GetAvailabilityParameters |
| 76 | + * @throws InvalidArgumentException |
| 77 | + * @throws \Exception |
| 78 | + */ |
| 79 | + public static function createFromJson(string $json) : GetAvailabilityParameters |
| 80 | + { |
| 81 | + $parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY); |
| 82 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 83 | + throw new InvalidArgumentException( |
| 84 | + 'GetAvailabilityParametersFactory Error: ' . json_last_error_msg() |
| 85 | + ); |
| 86 | + } |
| 87 | + return self::createFromArray($parameters); |
| 88 | + } |
| 89 | +} |
0 commit comments