Completed
Branch Message (d15af3)
by Sam
05:12
created

TXT::fromText()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 14
ccs 9
cts 9
cp 1
crap 2
rs 10
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
use Badcow\DNS\Parser\StringIterator;
17
use Badcow\DNS\Parser\Tokens;
18
19
/**
20
 * @see https://tools.ietf.org/html/rfc1035#section-3.3.14
21
 */
22
class TXT implements RdataInterface
23
{
24
    use RdataTrait;
25
26
    const TYPE = 'TXT';
27
    const TYPE_CODE = 16;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $text;
33
34
    /**
35
     * @param string|null $text
36
     */
37 26
    public function setText(?string $text): void
38
    {
39 26
        if (null === $text) {
40
            $this->text = null;
41
42
            return;
43
        }
44
45 26
        $this->text = stripslashes($text);
46 26
    }
47
48
    /**
49
     * @return string|null
50
     */
51 7
    public function getText(): ?string
52
    {
53 7
        return (string) $this->text ?? '';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 5
    public function toText(): string
60
    {
61 5
        return sprintf('"%s"', addslashes($this->text ?? ''));
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function toWire(): string
68
    {
69 1
        return $this->text ?? '';
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 2
    public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface
76
    {
77 2
        $rdLength = $rdLength ?? strlen($rdata);
78 2
        $txt = new static();
79 2
        $txt->setText(substr($rdata, $offset, $rdLength));
80 2
        $offset += $rdLength;
81
82 2
        return $txt;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 9
    public static function fromText(string $text): RdataInterface
89
    {
90 9
        $string = new StringIterator($text);
91 9
        $txt = new StringIterator();
92
93 9
        while ($string->valid()) {
94 9
            self::handleTxt($string, $txt);
95 9
            $string->next();
96
        }
97
98 9
        $rdata = new static();
99 9
        $rdata->setText((string) $txt);
100
101 9
        return $rdata;
102
    }
103
104
    /**
105
     * @param StringIterator $string
106
     * @param StringIterator $txt
107
     */
108 11
    public static function handleTxt(StringIterator $string, StringIterator $txt): void
109
    {
110 11
        if ($string->isNot(Tokens::DOUBLE_QUOTES)) {
111 3
            return;
112
        }
113
114 11
        $string->next();
115
116 11
        while ($string->isNot(Tokens::DOUBLE_QUOTES) && $string->valid()) {
117 11
            if ($string->is(Tokens::BACKSLASH)) {
118 5
                $string->next();
119
            }
120
121 11
            $txt->append($string->current());
122 11
            $string->next();
123
        }
124 11
    }
125
}
126