PolymorphicRdata::fromText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Rdata;
15
16
/**
17
 * Used to create RData of types that have not yet been implemented in the library.
18
 */
19
class PolymorphicRdata implements RdataInterface
20
{
21
    /**
22
     * The RData type.
23
     *
24
     * @var string
25
     */
26
    private $type;
27
28
    /**
29
     * @var string|null
30
     */
31
    private $data;
32
33
    /**
34
     * @var int
35
     */
36
    private $typeCode = 0;
37
38
    /**
39
     * PolymorphicRdata constructor.
40
     */
41 2
    public function __construct(?string $type = null, ?string $data = null)
42
    {
43 2
        if (null !== $type) {
44 2
            $this->setType($type);
45
        }
46
47 2
        if (null !== $data) {
48 2
            $this->setData($data);
49
        }
50 2
    }
51
52 2
    public function setType(string $type): void
53
    {
54
        try {
55 2
            $this->typeCode = Types::getTypeCode($type);
56
        } catch (UnsupportedTypeException $e) {
57
            $this->typeCode = 0;
58
        }
59 2
        $this->type = $type;
60 2
    }
61
62 2
    public function getType(): string
63
    {
64 2
        return $this->type;
65
    }
66
67
    public function setTypeCode(int $typeCode): void
68
    {
69
        $this->typeCode = $typeCode;
70
    }
71
72 1
    public function getTypeCode(): int
73
    {
74 1
        return $this->typeCode;
75
    }
76
77 2
    public function setData(string $data): void
78
    {
79 2
        $this->data = $data;
80 2
    }
81
82 2
    public function getData(): ?string
83
    {
84 2
        return $this->data;
85
    }
86
87 1
    public function toText(): string
88
    {
89 1
        return $this->getData() ?? '';
90
    }
91
92
    public function toWire(): string
93
    {
94
        return $this->data ?? '';
95
    }
96
97
    public function fromText(string $text): void
98
    {
99
        $this->setData($text);
100
    }
101
102
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
103
    {
104
        $this->setData(substr($rdata, $offset, $rdLength ?? strlen($rdata)));
105
    }
106
}
107