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

TXT   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 94.59%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 88
ccs 35
cts 37
cp 0.9459
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getText() 0 3 1
A setText() 0 9 2
A fromText() 0 11 2
A toText() 0 5 1
A toWire() 0 3 1
A fromWire() 0 5 1
A handleTxt() 0 15 5
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