Skip to content

Commit 1534497

Browse files
author
Christian Thomas
authored
Merge pull request #43 from lynnverse/master
Map PTR queries
2 parents b0fb2bb + 3b5c360 commit 1534497

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-0
lines changed

src/Entities/DataAbstract.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public static function createFromTypeAndString(DNSRecordType $recordType, string
6969
);
7070
}
7171

72+
if ($recordType->isA(DNSRecordType::TYPE_PTR)) {
73+
return new PTRData(new Hostname($data));
74+
}
75+
7276
throw new InvalidArgumentException("{$data} could not be created with type {$recordType}");
7377
}
7478

src/Entities/PTRData.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace RemotelyLiving\PHPDNS\Entities;
4+
5+
use function serialize;
6+
use function unserialize;
7+
8+
final class PTRData extends DataAbstract
9+
{
10+
private Hostname $hostname;
11+
12+
public function __construct(Hostname $hostname)
13+
{
14+
$this->hostname = $hostname;
15+
}
16+
17+
public function __toString(): string
18+
{
19+
return (string)$this->hostname;
20+
}
21+
22+
public function getHostname(): Hostname
23+
{
24+
return $this->hostname;
25+
}
26+
27+
public function toArray(): array
28+
{
29+
return [
30+
'hostname' => (string)$this->hostname,
31+
];
32+
}
33+
34+
public function serialize(): string
35+
{
36+
return serialize($this->toArray());
37+
}
38+
39+
/**
40+
* @param string $serialized
41+
*/
42+
public function unserialize($serialized): void
43+
{
44+
$unserialized = unserialize($serialized);
45+
$this->hostname = new Hostname($unserialized['hostname']);
46+
}
47+
}

tests/Unit/Entities/PTRDataTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace RemotelyLiving\PHPDNS\Tests\Unit\Entities;
4+
5+
use RemotelyLiving\PHPDNS\Entities\Hostname;
6+
use RemotelyLiving\PHPDNS\Entities\PTRData;
7+
use RemotelyLiving\PHPDNS\Tests\Unit\BaseTestAbstract;
8+
9+
class PTRDataTest extends BaseTestAbstract
10+
{
11+
/**
12+
* @var \RemotelyLiving\PHPDNS\Entities\Hostname
13+
*/
14+
private $hostname;
15+
16+
/**
17+
* @var \RemotelyLiving\PHPDNS\Entities\PTRData
18+
*/
19+
private $PTRData;
20+
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->hostname = new Hostname('google.com');
26+
$this->PTRData = new PTRData($this->hostname);
27+
}
28+
29+
/**
30+
* @test
31+
*/
32+
public function knowsIfEquals(): void
33+
{
34+
$this->assertTrue($this->PTRData->equals($this->PTRData));
35+
$this->assertFalse($this->PTRData->equals(new PTRData(new Hostname('boop.com'))));
36+
}
37+
38+
/**
39+
* @test
40+
*/
41+
public function isArrayable(): void
42+
{
43+
$this->assertArrayableAndEquals(
44+
['hostname' => (string)$this->hostname],
45+
$this->PTRData
46+
);
47+
}
48+
49+
/**
50+
* @test
51+
*/
52+
public function isJsonSerializable(): void
53+
{
54+
$this->assertJsonSerializeableAndEquals(
55+
['hostname' => (string)$this->hostname],
56+
$this->PTRData
57+
);
58+
}
59+
60+
/**
61+
* @test
62+
*/
63+
public function isSerializable(): void
64+
{
65+
$this->assertSerializable($this->PTRData);
66+
}
67+
68+
/**
69+
* @test
70+
*/
71+
public function isStringable(): void
72+
{
73+
$this->assertStringableAndEquals('google.com.', $this->PTRData);
74+
$this->assertEquals($this->PTRData, unserialize(serialize($this->PTRData)));
75+
}
76+
77+
/**
78+
* @test
79+
*/
80+
public function hasBasicGetters(): void
81+
{
82+
$this->assertSame($this->hostname, $this->PTRData->getHostname());
83+
}
84+
}

tests/Unit/Mappers/GoogleDNSTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ public function mapsAGoogleDNSRecordVariants(): void
6161
DNSRecord::createFromPrimitives('TXT', 'facebook.com', 365, null, 'IN', 'txtval'),
6262
$mappedRecord
6363
);
64+
65+
$mappedRecord = $this->mapper->mapFields([
66+
'type' => 12,
67+
'TTL' => 18930,
68+
'name' => '8.8.8.8.in-addr.arpa.',
69+
'data' => 'dns.google.'
70+
])->toDNSRecord();
71+
72+
$this->assertEquals(
73+
DNSRecord::createFromPrimitives('PTR', '8.8.8.8.in-addr.arpa.', 18930, null, 'IN', 'dns.google.'),
74+
$mappedRecord
75+
);
6476
}
6577

6678
/**

tests/Unit/Mappers/LocalSystemTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ class LocalSystemTest extends BaseTestAbstract
9595
'class' => 'IN',
9696
'weight' => 200,
9797
'port' => 9999,
98+
],
99+
[
100+
'host' => '8.8.8.8.in-addr.arpa',
101+
'class' => 'IN',
102+
'ttl' => 15248,
103+
'type' => 'PTR',
104+
'target' => 'dns.google'
105+
98106
]
99107
];
100108

0 commit comments

Comments
 (0)