Skip to content

Commit 0469c38

Browse files
committed
Add PTR to supported record types.
1 parent 8e33cd9 commit 0469c38

File tree

7 files changed

+131
-1
lines changed

7 files changed

+131
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ composer require spatie/dns
4848

4949
## Usage
5050

51-
The class can get these record types: `A`, `AAAA`, `CNAME`, `NS`, `SOA`, `MX`, `SRV`, `TXT`, `DNSKEY`, `CAA`, `NAPTR`.
51+
The class can get these record types: `A`, `AAAA`, `CNAME`, `NS`, `PTR`, `SOA`, `MX`, `SRV`, `TXT`, `DNSKEY`, `CAA`, `NAPTR`.
5252

5353
```php
5454
use Spatie\Dns\Dns;

src/Records/PTR.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Spatie\Dns\Records;
4+
5+
/**
6+
* @method string reversDnsName()
7+
*/
8+
class PTR extends Record
9+
{
10+
protected string $reversDnsName;
11+
protected string $name;
12+
13+
public static function parse(string $line): ?self
14+
{
15+
$attributes = static::lineToArray($line, 5);
16+
17+
if (count($attributes) < 5) {
18+
return null;
19+
}
20+
21+
return static::make([
22+
'reversDnsName' => $attributes[0],
23+
'ttl' => $attributes[1],
24+
'class' => $attributes[2],
25+
'type' => $attributes[3],
26+
'name' => $attributes[4],
27+
]);
28+
}
29+
30+
public function __toString(): string
31+
{
32+
return "{$this->reversDnsName}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t{$this->name}";
33+
}
34+
35+
public function toArray()
36+
{
37+
return [
38+
'reversDnsName' => $this->reversDnsName,
39+
'ttl' => $this->ttl,
40+
'class' => $this->class,
41+
'type' => $this->type,
42+
'name' => $this->name,
43+
];
44+
}
45+
46+
protected function castReversDnsName(string $value): string
47+
{
48+
return $this->prepareDomain($value);
49+
}
50+
}

src/Support/Types.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static function getTypes()
1111
DNS_AAAA => 'AAAA',
1212
DNS_CNAME => 'CNAME',
1313
DNS_NS => 'NS',
14+
DNS_PTR => 'PTR',
1415
DNS_SOA => 'SOA',
1516
DNS_MX => 'MX',
1617
DNS_SRV => 'SRV',

tests/DnsTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ public function it_can_use_custom_handlers()
161161
'custom-handler-results-AAAA',
162162
'custom-handler-results-CNAME',
163163
'custom-handler-results-NS',
164+
'custom-handler-results-PTR',
164165
'custom-handler-results-SOA',
165166
'custom-handler-results-MX',
166167
'custom-handler-results-SRV',

tests/Records/PTRTest.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Spatie\Dns\Test\Records;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Spatie\Dns\Records\PTR;
7+
8+
class PTRTest extends TestCase
9+
{
10+
/** @test */
11+
public function it_can_parse_string()
12+
{
13+
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.');
14+
15+
$this->assertSame('1.73.1.5.in-addr.arpa', $record->reversDnsName());
16+
$this->assertSame(3600, $record->ttl());
17+
$this->assertSame('IN', $record->class());
18+
$this->assertSame('PTR', $record->type());
19+
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->name());
20+
}
21+
22+
/** @test */
23+
public function it_can_make_from_array()
24+
{
25+
$record = PTR::make([
26+
'reversDnsName' => '1.73.1.5.in-addr.arpa.',
27+
'class' => 'IN',
28+
'ttl' => 3600,
29+
'type' => 'PTR',
30+
'name' => 'ae0.452.fra.as205948.creoline.net.',
31+
]);
32+
33+
$this->assertSame('1.73.1.5.in-addr.arpa', $record->reversDnsName());
34+
$this->assertSame(3600, $record->ttl());
35+
$this->assertSame('IN', $record->class());
36+
$this->assertSame('PTR', $record->type());
37+
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->name());
38+
}
39+
40+
/** @test */
41+
public function it_can_transform_to_string()
42+
{
43+
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.');
44+
45+
$this->assertSame("1.73.1.5.in-addr.arpa.\t\t3600\tIN\tPTR\tae0.452.fra.as205948.creoline.net.", strval($record));
46+
}
47+
48+
/** @test */
49+
public function it_can_be_converted_to_an_array()
50+
{
51+
$record = PTR::make([
52+
'reversDnsName' => '1.73.1.5.in-addr.arpa.',
53+
'class' => 'IN',
54+
'ttl' => 3600,
55+
'type' => 'PTR',
56+
'name' => 'ae0.452.fra.as205948.creoline.net.',
57+
]);
58+
59+
$data = $record->toArray();
60+
$this->assertSame('1.73.1.5.in-addr.arpa', $data['reversDnsName']);
61+
$this->assertSame(3600, $data['ttl']);
62+
$this->assertSame('IN', $data['class']);
63+
$this->assertSame('PTR', $data['type']);
64+
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $data['name']);
65+
}
66+
67+
/** @test */
68+
public function it_return_null_for_to_few_attributes()
69+
{
70+
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR');
71+
72+
$this->assertNull($record);
73+
}
74+
}

tests/Support/FactoryTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Spatie\Dns\Records\CNAME;
1010
use Spatie\Dns\Records\MX;
1111
use Spatie\Dns\Records\NS;
12+
use Spatie\Dns\Records\PTR;
1213
use Spatie\Dns\Records\SOA;
1314
use Spatie\Dns\Records\SRV;
1415
use Spatie\Dns\Records\TXT;
@@ -42,6 +43,7 @@ public function dnsRecords(): array
4243
[CNAME::class, 'www.spatie.be. 300 IN CNAME spatie.be.'],
4344
[MX::class, 'spatie.be. 1665 IN MX 10 ASPMX.L.GOOGLE.COM.'],
4445
[NS::class, 'spatie.be. 82516 IN NS ns1.openprovider.nl.'],
46+
[PTR::class, '1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.'],
4547
[SOA::class, 'spatie.be. 82393 IN SOA ns1.openprovider.nl. dns.openprovider.eu. 2020100801 10800 3600 604800 3600'],
4648
[SRV::class, '_http._tcp.mxtoolbox.com. 3600 IN SRV 10 100 80 mxtoolbox.com.'],
4749
[TXT::class, 'spatie.be. 594 IN TXT "v=spf1 include:eu.mailgun.org include:spf.factuursturen.be include:sendgrid.net a mx ~all"'],

tests/Support/TypesTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function it_can_transform_flag_to_name()
2727
$this->assertSame([DNS_CNAME => 'CNAME'], $this->types->toNames(DNS_CNAME));
2828
$this->assertSame([DNS_MX => 'MX'], $this->types->toNames(DNS_MX));
2929
$this->assertSame([DNS_NS => 'NS'], $this->types->toNames(DNS_NS));
30+
$this->assertSame([DNS_PTR => 'PTR'], $this->types->toNames(DNS_PTR));
3031
$this->assertSame([DNS_SOA => 'SOA'], $this->types->toNames(DNS_SOA));
3132
$this->assertSame([DNS_SRV => 'SRV'], $this->types->toNames(DNS_SRV));
3233
$this->assertSame([DNS_TXT => 'TXT'], $this->types->toNames(DNS_TXT));
@@ -50,6 +51,7 @@ public function it_can_transform_name_to_flag()
5051
$this->assertSame(DNS_CNAME, $this->types->toFlags(['CNAME']));
5152
$this->assertSame(DNS_MX, $this->types->toFlags(['MX']));
5253
$this->assertSame(DNS_NS, $this->types->toFlags(['NS']));
54+
$this->assertSame(DNS_PTR, $this->types->toFlags(['PTR']));
5355
$this->assertSame(DNS_SOA, $this->types->toFlags(['SOA']));
5456
$this->assertSame(DNS_SRV, $this->types->toFlags(['SRV']));
5557
$this->assertSame(DNS_TXT, $this->types->toFlags(['TXT']));

0 commit comments

Comments
 (0)