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\CalculateFlightMilesParameters; |
9
|
|
|
|
10
|
|
|
class CalculateFlightMilesParametersFactory implements ValueObjectFactoryInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param array $parameters |
14
|
|
|
* @return CalculateFlightMilesParameters |
15
|
|
|
* @throws \Exception |
16
|
|
|
*/ |
17
|
|
|
public static function createFromArray(array $parameters) : CalculateFlightMilesParameters |
18
|
|
|
{ |
19
|
|
|
$calculateFlightMilesParameters = new CalculateFlightMilesParameters( |
20
|
|
|
$parameters['origin'], |
21
|
|
|
$parameters['destination'] |
22
|
|
|
); |
23
|
|
|
if (array_key_exists('cabin_code', $parameters) && $parameters['cabin_code'] === 'Y') { |
24
|
|
|
$calculateFlightMilesParameters = $calculateFlightMilesParameters->withCabinCode(); |
25
|
|
|
} |
26
|
|
|
if (array_key_exists('class_code', $parameters) && $parameters['class_code'] === 'Y') { |
27
|
|
|
$calculateFlightMilesParameters = $calculateFlightMilesParameters->withClassCode(); |
28
|
|
|
} |
29
|
|
|
if (array_key_exists('marketingClassCode', $parameters)) { |
30
|
|
|
$calculateFlightMilesParameters = $calculateFlightMilesParameters |
31
|
|
|
->withMarketingClassCode($parameters['marketingClassCode']); |
32
|
|
|
} |
33
|
|
|
if (array_key_exists('card_type', $parameters)) { |
34
|
|
|
$calculateFlightMilesParameters = $calculateFlightMilesParameters |
35
|
|
|
->withCardType($parameters['card_type']); |
36
|
|
|
} |
37
|
|
|
if (array_key_exists('flightDate', $parameters)) { |
38
|
|
|
$calculateFlightMilesParameters = $calculateFlightMilesParameters |
39
|
|
|
->withFlightDate(DateTimeImmutable::createFromFormat('d.m.Y', $parameters['flightDate'])); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
if (array_key_exists('operatingFlightNumber', $parameters)) { |
42
|
|
|
$calculateFlightMilesParameters = $calculateFlightMilesParameters |
43
|
|
|
->withOperatingFlightNumber($parameters['operatingFlightNumber']); |
44
|
|
|
} |
45
|
|
|
if (array_key_exists('marketingFlightNumber', $parameters)) { |
46
|
|
|
$calculateFlightMilesParameters = $calculateFlightMilesParameters |
47
|
|
|
->withMarketingFlightNumber($parameters['marketingFlightNumber']); |
48
|
|
|
} |
49
|
|
|
return $calculateFlightMilesParameters; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $json |
55
|
|
|
* @return CalculateFlightMilesParameters |
56
|
|
|
* @throws InvalidArgumentException |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
|
|
public static function createFromJson(string $json) : CalculateFlightMilesParameters |
60
|
|
|
{ |
61
|
|
|
$parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY); |
62
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
63
|
|
|
throw new InvalidArgumentException( |
64
|
|
|
'CalculateFlightMilesParametersFactory Error: ' . json_last_error_msg() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
return self::createFromArray($parameters); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|