Passed
Push — master ( 491824...c1e54a )
by Christian
42s queued 11s
created

CAAData::normalizeValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace RemotelyLiving\PHPDNS\Entities;
4
5
use RemotelyLiving\PHPDNS\Exceptions;
6
7
class CAAData extends DataAbstract
8
{
9
    /**
10
     * @var int
11
     */
12
    private $flags;
13
14
    /**
15
     * @var string
16
     */
17
    private $tag;
18
19
    /**
20
     * @var string|null
21
     */
22
    private $value;
23
24
    public function __construct(int $flags, string $tag, string $value = null)
25
    {
26
        $this->flags = $flags;
27
        $this->tag = $tag;
28
        $this->value = ($value)
29
            ? $this->normalizeValue($value)
30
            : null;
31
    }
32
33
    public function __toString(): string
34
    {
35
        return "{$this->flags} {$this->tag} \"{$this->value}\"";
36
    }
37
38
    public function getFlags(): int
39
    {
40
        return $this->flags;
41
    }
42
43
    public function getTag(): string
44
    {
45
        return $this->tag;
46
    }
47
48
    public function getValue(): ?string
49
    {
50
        return $this->value;
51
    }
52
53
    public function toArray(): array
54
    {
55
        return [
56
            'flags' => $this->flags,
57
            'tag' => $this->tag,
58
            'value' => $this->value,
59
        ];
60
    }
61
62
    public function serialize(): string
63
    {
64
        return \serialize($this->toArray());
65
    }
66
67
    /**
68
     * @param string $serialized
69
     */
70
    public function unserialize($serialized): void
71
    {
72
        $unserialized = \unserialize($serialized);
73
        $this->flags = $unserialized['flags'];
74
        $this->tag = $unserialized['tag'];
75
        $this->value = $unserialized['value'];
76
    }
77
78
    private function normalizeValue(string $value): string
79
    {
80
        $normalized = trim(str_ireplace('"', '', $value));
81
82
        if (preg_match('/\s/m', $normalized)) {
83
            throw new Exceptions\InvalidArgumentException("$value is not a valid CAA value");
84
        }
85
86
        return $normalized;
87
    }
88
}
89