Completed
Push — master ( ff806b...dfe0cd )
by Sam
07:35 queued 04:17
created

TXT   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 89
ccs 36
cts 38
cp 0.9474
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getText() 0 3 1
A setText() 0 9 2
A toText() 0 7 1
A toWire() 0 3 1
A fromWire() 0 5 1
A fromText() 0 11 2
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 31
    public function setText(?string $text): void
35
    {
36 31
        if (null === $text) {
37
            $this->text = null;
38
39
            return;
40
        }
41
42 31
        $this->text = stripslashes($text);
43 31
    }
44
45 8
    public function getText(): ?string
46
    {
47 8
        return (string) $this->text ?? '';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 9
    public function toText(): string
54
    {
55 9
        $chunks = array_map(function (string $chunk) {
56 9
            return sprintf('"%s"', addcslashes($chunk, Tokens::DOUBLE_QUOTES.Tokens::BACKSLASH));
57 9
        }, str_split($this->text ?? '', 255));
58
59 9
        return implode(' ', $chunks);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function toWire(): string
66
    {
67 1
        return $this->text ?? '';
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 2
    public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
74
    {
75 2
        $rdLength = $rdLength ?? strlen($rdata);
76 2
        $this->setText(substr($rdata, $offset, $rdLength));
77 2
        $offset += $rdLength;
78 2
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 13
    public function fromText(string $text): void
84
    {
85 13
        $string = new StringIterator($text);
86 13
        $txt = new StringIterator();
87
88 13
        while ($string->valid()) {
89 13
            self::handleTxt($string, $txt);
90 13
            $string->next();
91
        }
92
93 13
        $this->setText((string) $txt);
94 13
    }
95
96 15
    public static function handleTxt(StringIterator $string, StringIterator $txt): void
97
    {
98 15
        if ($string->isNot(Tokens::DOUBLE_QUOTES)) {
99 3
            return;
100
        }
101
102 15
        $string->next();
103
104 15
        while ($string->isNot(Tokens::DOUBLE_QUOTES) && $string->valid()) {
105 15
            if ($string->is(Tokens::BACKSLASH)) {
106 9
                $string->next();
107
            }
108
109 15
            $txt->append($string->current());
110 15
            $string->next();
111
        }
112 15
    }
113
}
114