Passed
Branch master (8940db)
by Sam
02:38
created

HINFO::getCpu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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.2
21
 */
22
class HINFO implements RdataInterface
23
{
24
    use RdataTrait;
25
26
    const TYPE = 'HINFO';
27
    const TYPE_CODE = 13;
28
29
    /**
30
     * @var string|null
31
     */
32
    private $cpu;
33
34
    /**
35
     * @var string|null
36
     */
37
    private $os;
38
39
    /**
40
     * @param string $cpu
41
     */
42 14
    public function setCpu(string $cpu): void
43
    {
44 14
        $this->cpu = $cpu;
45 14
    }
46
47
    /**
48
     * @return string
49
     */
50 4
    public function getCpu(): ?string
51
    {
52 4
        return $this->cpu;
53
    }
54
55
    /**
56
     * @param string $os
57
     */
58 14
    public function setOs(string $os): void
59
    {
60 14
        $this->os = $os;
61 14
    }
62
63
    /**
64
     * @return string
65
     */
66 4
    public function getOs(): ?string
67
    {
68 4
        return $this->os;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 4
    public function toText(): string
75
    {
76 4
        return sprintf('"%s" "%s"', $this->cpu ?? '', $this->os ?? '');
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 1
    public function toWire(): string
83
    {
84 1
        return $this->toText();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     *
90
     * @return HINFO
91
     */
92 1
    public static function fromWire(string $rdata): RdataInterface
93
    {
94 1
        return self::fromText($rdata);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     *
100
     * @return HINFO
101
     */
102 2
    public static function fromText(string $text): RdataInterface
103
    {
104 2
        $string = new StringIterator($text);
105 2
        $hinfo = new self();
106 2
        $hinfo->setCpu(self::extractText($string));
107 2
        $hinfo->setOs(self::extractText($string));
108
109 2
        return $hinfo;
110
    }
111
112
    /**
113
     * @param StringIterator $string
114
     *
115
     * @return string
116
     */
117 2
    private static function extractText(StringIterator $string): string
118
    {
119 2
        $txt = new StringIterator();
120
121 2
        if ($string->is(Tokens::DOUBLE_QUOTES)) {
122 2
            TXT::handleTxt($string, $txt);
123 2
            $string->next();
124
        } else {
125
            while ($string->isNot(Tokens::SPACE) && $string->valid()) {
126
                $txt->append($string->current());
127
                $string->next();
128
            }
129
        }
130
131 2
        if ($string->is(Tokens::SPACE)) {
132 2
            $string->next();
133
        }
134
135 2
        return (string) $txt;
136
    }
137
}
138