Completed
Branch Version3 (6a4ebc)
by Sam
01:27
created

A::toText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
/**
17
 * @see https://tools.ietf.org/html/rfc1035#section-3.4.1
18
 */
19
class A implements RdataInterface
20
{
21
    use RdataTrait;
22
23
    const TYPE = 'A';
24
    const TYPE_CODE = 1;
25
26
    /**
27
     * @var string
28
     */
29
    protected $address;
30
31
    /**
32
     * @param string $address
33
     */
34 27
    public function setAddress(string $address): void
35
    {
36 27
        $this->address = $address;
37 27
    }
38
39
    /**
40
     * @return string
41
     */
42 9
    public function getAddress(): ?string
43
    {
44 9
        return $this->address;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 7
    public function toText(): string
51
    {
52 7
        return $this->address ?? '';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @throws \InvalidArgumentException
59
     */
60 2
    public function toWire(): string
61
    {
62 2
        if (false === $encoded = inet_pton($this->address)) {
63
            throw new \InvalidArgumentException(sprintf('The IP address "%s" cannot be encoded. Check that it is a valid IP address.', $this->address));
64
        }
65
66 2
        return $encoded;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 9
    public static function fromText(string $text): RdataInterface
73
    {
74 9
        $a = new static();
75 9
        $a->setAddress($text);
76
77 9
        return $a;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     *
83
     * @throws DecodeException
84
     */
85 5
    public static function fromWire(string $rdata): RdataInterface
86
    {
87 5
        if (false === $address = @inet_ntop($rdata)) {
88 2
            throw new DecodeException(static::TYPE, $rdata);
89
        }
90
91 3
        $a = new static();
92 3
        $a->setAddress($address);
93
94 3
        return $a;
95
    }
96
}
97