Passed
Pull Request — master (#1320)
by
unknown
02:40
created

ExclusionPolicy::__construct()   B

Complexity

Conditions 7
Paths 15

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.7656

Importance

Changes 0
Metric Value
cc 7
eloc 14
c 0
b 0
f 0
nc 15
nop 2
dl 0
loc 27
rs 8.8333
ccs 3
cts 4
cp 0.75
crap 7.7656
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Annotation;
6
7
use JMS\Serializer\Exception\RuntimeException;
8
9
/**
10
 * @Annotation
11
 * @Target("CLASS")
12
 */
13
#[\Attribute(\Attribute::TARGET_CLASS)]
14
final class ExclusionPolicy
15
{
16
    public const NONE = 'NONE';
17
    public const ALL = 'ALL';
18
19
    /**
20 25
     * @var string|null
21
     */
22 25
    public $policy = null;
23
24
    public function __construct(array $values = [], ?string $policy = null)
25
    {
26 25
        if ([] !== $values) {
27
            $value = self::NONE;
28 25
29
            if (array_key_exists('value', $values)) {
30
                $value = $values['value'];
31 25
            }
32
33
            if (array_key_exists('policy', $values)) {
34
                $value = $values['policy'];
35
            }
36
        } else {
37
            $value = $policy;
38
        }
39
40
        if (!\is_string($value)) {
41
            throw new RuntimeException('Exclusion policy value must be of string type.');
42
        }
43
44
        $value = strtoupper($value);
45
46
        if (self::NONE !== $value && self::ALL !== $value) {
47
            throw new RuntimeException('Exclusion policy must either be "ALL", or "NONE".');
48
        }
49
50
        $this->policy = $value;
51
    }
52
}
53