GetPortListParameters::getValue()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 4
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TK\API\ValueObject;
5
6
use TK\API\Exception\InvalidArgumentException;
7
use function in_array;
8
9
final class GetPortListParameters implements ValueObjectInterface
10
{
11
    public const AIRLINE_CODE_TURKISH_AIRLINES = 'TK';
12
    public const AIRLINE_CODE_ANADOLUJET = 'AJ';
13
    public const LANGUAGE_CODE_TR = 'TR';
14
    public const LANGUAGE_CODE_EN = 'EN';
15
    public const LANGUAGE_CODE_DE = 'DE';
16
17
    private static $airlineCodeEnum = ['TK', 'AJ'];
18
    private static $languageCodeEnum = ['TR', 'EN', 'DE'];
19
20
    private $airlineCode;
21
    private $languageCode;
22
23
    public function __construct(string $airlineCode)
24
    {
25
        $this->setAirlineCode($airlineCode);
26
    }
27
28
    private function setAirlineCode(?string $airlineCode) : void
29
    {
30
        if (! in_array($airlineCode, self::$airlineCodeEnum, true)) {
31
            throw new InvalidArgumentException(
32
                'Invalid AirlineCode provided. Must be one of these: "TK", "AJ"'
33
            );
34
        }
35
        $this->airlineCode = $airlineCode;
36
    }
37
38
    private function setLanguageCode(?string $languageCode) : void
39
    {
40
        if (!\in_array($languageCode, self::$languageCodeEnum, true)) {
41
            throw new InvalidArgumentException(
42
                'Invalid LanguageCode provided. Must be one of these: "TR", "EN", "DE"'
43
            );
44
        }
45
        $this->languageCode = $languageCode;
46
    }
47
48
    public function withLanguageCode(string $languageCode) : GetPortListParameters
49
    {
50
        $this->setLanguageCode($languageCode);
51
        return $this;
52
    }
53
54
    public function getValue() : array
55
    {
56
        $getPortListParameters = [];
57
        if ($this->languageCode !== null) {
58
            $getPortListParameters['languageCode'] = $this->languageCode;
59
        }
60
        if ($this->airlineCode !== null) {
61
            $getPortListParameters['airlineCode'] = $this->airlineCode;
62
        }
63
        return $getPortListParameters;
64
    }
65
}
66