Passed
Pull Request — master (#11)
by Christian
04:46
created

DNSRecord::jsonSerialize()   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 toArray(): array
110
    {
111
        $formatted = [
112
            'hostname' => (string)$this->hostname,
113
            'type' => (string)$this->recordType,
114
            'TTL' => $this->TTL,
115
            'class' => $this->class,
116
        ];
117
118
        if ($this->IPAddress) {
119
            $formatted['IPAddress'] = (string)$this->IPAddress;
120
        }
121
122
        if ($this->data) {
123
            $formatted['data'] = (string)$this->data;
124
        }
125
126
        return $formatted;
127
    }
128
129
    public function equals(DNSRecord $record): bool
130
    {
131
        return $this->hostname->equals($record->getHostname())
132
            && $this->recordType->equals($record->getType())
133
            && (string)$this->data === (string)$record->getData() // could be null
134
            && (string)$this->IPAddress === (string)$record->getIPAddress(); // could be null
135
    }
136
137
    public function serialize(): string
138
    {
139
        return \serialize($this->toArray());
140
    }
141
142
    /**
143
     * @param string $serialized
144
     */
145
    public function unserialize($serialized): void
146
    {
147
        $unserialized = \unserialize($serialized);
148
149
        $rawIPAddres = $unserialized['IPAddress'] ?? null;
150
        $this->recordType = DNSRecordType::createFromString($unserialized['type']);
151
        $this->hostname = Hostname::createFromString($unserialized['hostname']);
152
        $this->TTL = (int) $unserialized['TTL'];
153
        $this->IPAddress = $rawIPAddres ? IPAddress::createFromString($rawIPAddres) : null;
154
        $this->class = $unserialized['class'];
155
        $this->data = (isset($unserialized['data']))
156
         ? DataAbstract::createFromTypeAndString($this->recordType, $unserialized['data'])
157
         : null;
158
    }
159
160
    public function jsonSerialize(): array
161
    {
162
        return $this->toArray();
163
    }
164
}
165