Passed
Pull Request — master (#82)
by
unknown
11:19
created

TXT::fromWire()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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