Completed
Pull Request — master (#396)
by Luc
04:25
created

SapiVersion::guardSapiVersion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\ValueObject;
4
5
class SapiVersion
6
{
7
    const V2 = 'v2';
8
    const V3 = 'v3';
9
10
    /**
11
     * @var string
12
     */
13
    private $version;
14
15
    /**
16
     * @param string $version
17
     */
18
    public function __construct(string $version)
19
    {
20
        $this->guardSapiVersion($version);
21
22
        $this->version = $version;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function toNative(): string
29
    {
30
        return $this->version;
31
    }
32
33
    /**
34
     * @param SapiVersion $sapiVersion
35
     * @return bool
36
     */
37
    public function equals(SapiVersion $sapiVersion): bool
38
    {
39
        return $this->version === $sapiVersion->toNative();
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    private function allowedValues(): array
46
    {
47
        return [
48
            self::V2,
49
            self::V3,
50
        ];
51
    }
52
53
    /**
54
     * @param string $version
55
     * @throws \InvalidArgumentException
56
     */
57
    private function guardSapiVersion(string $version)
58
    {
59
        if (!in_array($version, $this->allowedValues())) {
60
            throw new \InvalidArgumentException(
61
                'Sapi version "' . $version . '" is not allowed.'
62
            );
63
        }
64
    }
65
}
66