1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (c) Ne-Lexa |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @see https://github.com/Ne-Lexa/guzzle-doh-middleware |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Nelexa\Doh\Storage; |
15
|
|
|
|
16
|
|
|
use LibDNS\Records\ResourceTypes; |
17
|
|
|
|
18
|
|
|
class DnsRecord implements \Countable |
19
|
|
|
{ |
20
|
|
|
/** @var bool */ |
21
|
|
|
public $cacheHit = false; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $domainName; |
25
|
|
|
|
26
|
|
|
/** @var string[] */ |
27
|
|
|
private $data; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
private $resourceType; |
31
|
|
|
|
32
|
|
|
/** @var \DateInterval */ |
33
|
|
|
private $ttl; |
34
|
|
|
|
35
|
|
|
/** @var \DateTimeImmutable */ |
36
|
|
|
private $expiredAt; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $domainName |
40
|
|
|
* @param string[] $data |
41
|
|
|
* @param int $resourceType |
42
|
|
|
* @param mixed $ttl |
43
|
|
|
*/ |
44
|
13 |
|
public function __construct(string $domainName, array $data, int $resourceType, $ttl) |
45
|
|
|
{ |
46
|
13 |
|
$this->domainName = $domainName; |
47
|
13 |
|
$this->data = $data; |
48
|
13 |
|
$this->resourceType = $resourceType; |
49
|
|
|
|
50
|
13 |
|
if (\is_int($ttl)) { |
51
|
13 |
|
$ttl .= ' seconds'; |
52
|
|
|
} |
53
|
|
|
|
54
|
13 |
|
if (\is_string($ttl)) { |
55
|
|
|
/** @var \DateInterval|false $interval */ |
56
|
13 |
|
$interval = \DateInterval::createFromDateString($ttl); |
57
|
|
|
|
58
|
13 |
|
if ($interval === false) { |
59
|
|
|
throw new \RuntimeException('Error create DateInterval from ' . $ttl); |
60
|
|
|
} |
61
|
|
|
|
62
|
13 |
|
$ttl = $interval; |
63
|
|
|
} |
64
|
|
|
|
65
|
13 |
|
if (!$ttl instanceof \DateInterval) { |
66
|
|
|
throw new \InvalidArgumentException('Invalid ttl type. Support only integer, string or DateInterval type.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
13 |
|
$this->ttl = $ttl; |
70
|
13 |
|
$this->expiredAt = (new \DateTimeImmutable())->add($ttl); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
13 |
|
public function getDomainName(): string |
77
|
|
|
{ |
78
|
13 |
|
return $this->domainName; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string[] |
83
|
|
|
*/ |
84
|
13 |
|
public function getData(): array |
85
|
|
|
{ |
86
|
13 |
|
return $this->data; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return \DateInterval |
91
|
|
|
*/ |
92
|
2 |
|
public function getTTL(): \DateInterval |
93
|
|
|
{ |
94
|
2 |
|
return $this->ttl; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return \DateTimeImmutable |
99
|
|
|
*/ |
100
|
3 |
|
public function getExpiredAt(): \DateTimeImmutable |
101
|
|
|
{ |
102
|
3 |
|
return $this->expiredAt; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
public function getResourceType(): int |
109
|
|
|
{ |
110
|
|
|
return $this->resourceType; |
111
|
|
|
} |
112
|
|
|
|
113
|
13 |
|
public function isCnameRecord(): bool |
114
|
|
|
{ |
115
|
13 |
|
return $this->resourceType === ResourceTypes::CNAME; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function isARecord(): bool |
119
|
|
|
{ |
120
|
|
|
return $this->resourceType === ResourceTypes::A || $this->resourceType === ResourceTypes::AAAA; |
121
|
|
|
} |
122
|
|
|
|
123
|
13 |
|
public function count(): int |
124
|
|
|
{ |
125
|
13 |
|
return \count($this->data); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|