Completed
Pull Request — master (#14)
by Christian
02:24
created

DNSRecord::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace RemotelyLiving\PHPDNS\Entities;
3
4
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable;
5
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;
6
7
class DNSRecord extends EntityAbstract implements Arrayable, Serializable
8
{
9
    /**
10
     * @var \RemotelyLiving\PHPDNS\Entities\DNSRecordType
11
     */
12
    private $recordType;
13
14
    /**
15
     * @var \RemotelyLiving\PHPDNS\Entities\Hostname
16
     */
17
    private $hostname;
18
19
    /**
20
     * @var int
21
     */
22
    private $TTL;
23
24
    /**
25
     * @var \RemotelyLiving\PHPDNS\Entities\IPAddress|null
26
     */
27
    private $IPAddress;
28
29
    /**
30
     * @var string
31
     */
32
    private $class;
33
34
    /**
35
     * @var \RemotelyLiving\PHPDNS\Entities\DataAbstract|null
36
     */
37
    private $data;
38
39
    public function __construct(
40
        DNSRecordType $recordType,
41
        Hostname $hostname,
42
        int $ttl,
43
        IPAddress $IPAddress = null,
44
        string $class = 'IN',
45
        DataAbstract $data = null
46
    ) {
47
        $this->recordType = $recordType;
48
        $this->hostname = $hostname;
49
        $this->TTL = $ttl;
50
        $this->IPAddress = $IPAddress;
51
        $this->class = $class;
52
        $this->data = $data;
53
    }
54
55
    public static function createFromPrimitives(
56
        string $recordType,
57
        string $hostname,
58
        int $ttl,
59
        string $IPAddress = null,
60
        string $class = 'IN',
61
        string $data = null
62
    ): DNSRecord {
63
        $type = DNSRecordType::createFromString($recordType);
64
        $hostname = Hostname::createFromString($hostname);
65
        $data = ($data !== null)
66
            ? DataAbstract::createFromTypeAndString($type, $data)
67
            : null;
68
69
        return new static(
70
            $type,
71
            $hostname,
72
            $ttl,
73
            $IPAddress ? IPAddress::createFromString($IPAddress): null,
74
            $class,
75
            $data
76
        );
77
    }
78
79
    public function getType(): DNSRecordType
80
    {
81
        return $this->recordType;
82
    }
83
84
    public function getHostname(): Hostname
85
    {
86
        return $this->hostname;
87
    }
88
89
    public function getTTL(): int
90
    {
91
        return $this->TTL;
92
    }
93
94
    public function getIPAddress(): ?IPAddress
95
    {
96
        return $this->IPAddress;
97
    }
98
99
    public function getClass(): string
100
    {
101
        return $this->class;
102
    }
103
104
    public function getData(): ?DataAbstract
105
    {
106
        return $this->data;
107
    }
108
109
    public function setTTL(int $ttl) : self
110
    {
111
112
        $this->TTL = $ttl;
113
        return $this;
114
    }
115
116
    public function toArray(): array
117
    {
118
        $formatted = [
119
            'hostname' => (string)$this->hostname,
120
            'type' => (string)$this->recordType,
121
            'TTL' => $this->TTL,
122
            'class' => $this->class,
123
        ];
124
125
        if ($this->IPAddress) {
126
            $formatted['IPAddress'] = (string)$this->IPAddress;
127
        }
128
129
        if ($this->data) {
130
            $formatted['data'] = (string)$this->data;
131
        }
132
133
        return $formatted;
134
    }
135
136
    public function equals(DNSRecord $record): bool
137
    {
138
        return $this->hostname->equals($record->getHostname())
139
            && $this->recordType->equals($record->getType())
140
            && (string)$this->data === (string)$record->getData() // could be null
141
            && (string)$this->IPAddress === (string)$record->getIPAddress(); // could be null
142
    }
143
144
    public function serialize(): string
145
    {
146
        return \serialize($this->toArray());
147
    }
148
149
    /**
150
     * @param string $serialized
151
     */
152
    public function unserialize($serialized): void
153
    {
154
        $unserialized = \unserialize($serialized);
155
156
        $rawIPAddres = $unserialized['IPAddress'] ?? null;
157
        $this->recordType = DNSRecordType::createFromString($unserialized['type']);
158
        $this->hostname = Hostname::createFromString($unserialized['hostname']);
159
        $this->TTL = (int) $unserialized['TTL'];
160
        $this->IPAddress = $rawIPAddres ? IPAddress::createFromString($rawIPAddres): null;
161
        $this->class = $unserialized['class'];
162
        $this->data = (isset($unserialized['data']))
163
         ? DataAbstract::createFromTypeAndString($this->recordType, $unserialized['data'])
164
         : null;
165
    }
166
167
    public function jsonSerialize(): array
168
    {
169
        return $this->toArray();
170
    }
171
}
172