Skip to content

Commit

Permalink
Merge pull request #94 from lwohn/add-ptr-record-type
Browse files Browse the repository at this point in the history
Add PTR to supported record types.
  • Loading branch information
freekmurze authored Jul 14, 2022
2 parents 8e33cd9 + 0469c38 commit ffb0d25
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ composer require spatie/dns

## Usage

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

```php
use Spatie\Dns\Dns;
Expand Down
50 changes: 50 additions & 0 deletions src/Records/PTR.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Spatie\Dns\Records;

/**
* @method string reversDnsName()
*/
class PTR extends Record
{
protected string $reversDnsName;
protected string $name;

public static function parse(string $line): ?self
{
$attributes = static::lineToArray($line, 5);

if (count($attributes) < 5) {
return null;
}

return static::make([
'reversDnsName' => $attributes[0],
'ttl' => $attributes[1],
'class' => $attributes[2],
'type' => $attributes[3],
'name' => $attributes[4],
]);
}

public function __toString(): string
{
return "{$this->reversDnsName}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t{$this->name}";
}

public function toArray()
{
return [
'reversDnsName' => $this->reversDnsName,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'name' => $this->name,
];
}

protected function castReversDnsName(string $value): string
{
return $this->prepareDomain($value);
}
}
1 change: 1 addition & 0 deletions src/Support/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static function getTypes()
DNS_AAAA => 'AAAA',
DNS_CNAME => 'CNAME',
DNS_NS => 'NS',
DNS_PTR => 'PTR',
DNS_SOA => 'SOA',
DNS_MX => 'MX',
DNS_SRV => 'SRV',
Expand Down
1 change: 1 addition & 0 deletions tests/DnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function it_can_use_custom_handlers()
'custom-handler-results-AAAA',
'custom-handler-results-CNAME',
'custom-handler-results-NS',
'custom-handler-results-PTR',
'custom-handler-results-SOA',
'custom-handler-results-MX',
'custom-handler-results-SRV',
Expand Down
74 changes: 74 additions & 0 deletions tests/Records/PTRTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Spatie\Dns\Test\Records;

use PHPUnit\Framework\TestCase;
use Spatie\Dns\Records\PTR;

class PTRTest extends TestCase
{
/** @test */
public function it_can_parse_string()
{
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.');

$this->assertSame('1.73.1.5.in-addr.arpa', $record->reversDnsName());
$this->assertSame(3600, $record->ttl());
$this->assertSame('IN', $record->class());
$this->assertSame('PTR', $record->type());
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->name());
}

/** @test */
public function it_can_make_from_array()
{
$record = PTR::make([
'reversDnsName' => '1.73.1.5.in-addr.arpa.',
'class' => 'IN',
'ttl' => 3600,
'type' => 'PTR',
'name' => 'ae0.452.fra.as205948.creoline.net.',
]);

$this->assertSame('1.73.1.5.in-addr.arpa', $record->reversDnsName());
$this->assertSame(3600, $record->ttl());
$this->assertSame('IN', $record->class());
$this->assertSame('PTR', $record->type());
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $record->name());
}

/** @test */
public function it_can_transform_to_string()
{
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.');

$this->assertSame("1.73.1.5.in-addr.arpa.\t\t3600\tIN\tPTR\tae0.452.fra.as205948.creoline.net.", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = PTR::make([
'reversDnsName' => '1.73.1.5.in-addr.arpa.',
'class' => 'IN',
'ttl' => 3600,
'type' => 'PTR',
'name' => 'ae0.452.fra.as205948.creoline.net.',
]);

$data = $record->toArray();
$this->assertSame('1.73.1.5.in-addr.arpa', $data['reversDnsName']);
$this->assertSame(3600, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('PTR', $data['type']);
$this->assertSame('ae0.452.fra.as205948.creoline.net.', $data['name']);
}

/** @test */
public function it_return_null_for_to_few_attributes()
{
$record = PTR::parse('1.73.1.5.in-addr.arpa. 3600 IN PTR');

$this->assertNull($record);
}
}
2 changes: 2 additions & 0 deletions tests/Support/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Spatie\Dns\Records\CNAME;
use Spatie\Dns\Records\MX;
use Spatie\Dns\Records\NS;
use Spatie\Dns\Records\PTR;
use Spatie\Dns\Records\SOA;
use Spatie\Dns\Records\SRV;
use Spatie\Dns\Records\TXT;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function dnsRecords(): array
[CNAME::class, 'www.spatie.be. 300 IN CNAME spatie.be.'],
[MX::class, 'spatie.be. 1665 IN MX 10 ASPMX.L.GOOGLE.COM.'],
[NS::class, 'spatie.be. 82516 IN NS ns1.openprovider.nl.'],
[PTR::class, '1.73.1.5.in-addr.arpa. 3600 IN PTR ae0.452.fra.as205948.creoline.net.'],
[SOA::class, 'spatie.be. 82393 IN SOA ns1.openprovider.nl. dns.openprovider.eu. 2020100801 10800 3600 604800 3600'],
[SRV::class, '_http._tcp.mxtoolbox.com. 3600 IN SRV 10 100 80 mxtoolbox.com.'],
[TXT::class, 'spatie.be. 594 IN TXT "v=spf1 include:eu.mailgun.org include:spf.factuursturen.be include:sendgrid.net a mx ~all"'],
Expand Down
2 changes: 2 additions & 0 deletions tests/Support/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function it_can_transform_flag_to_name()
$this->assertSame([DNS_CNAME => 'CNAME'], $this->types->toNames(DNS_CNAME));
$this->assertSame([DNS_MX => 'MX'], $this->types->toNames(DNS_MX));
$this->assertSame([DNS_NS => 'NS'], $this->types->toNames(DNS_NS));
$this->assertSame([DNS_PTR => 'PTR'], $this->types->toNames(DNS_PTR));
$this->assertSame([DNS_SOA => 'SOA'], $this->types->toNames(DNS_SOA));
$this->assertSame([DNS_SRV => 'SRV'], $this->types->toNames(DNS_SRV));
$this->assertSame([DNS_TXT => 'TXT'], $this->types->toNames(DNS_TXT));
Expand All @@ -50,6 +51,7 @@ public function it_can_transform_name_to_flag()
$this->assertSame(DNS_CNAME, $this->types->toFlags(['CNAME']));
$this->assertSame(DNS_MX, $this->types->toFlags(['MX']));
$this->assertSame(DNS_NS, $this->types->toFlags(['NS']));
$this->assertSame(DNS_PTR, $this->types->toFlags(['PTR']));
$this->assertSame(DNS_SOA, $this->types->toFlags(['SOA']));
$this->assertSame(DNS_SRV, $this->types->toFlags(['SRV']));
$this->assertSame(DNS_TXT, $this->types->toFlags(['TXT']));
Expand Down

0 comments on commit ffb0d25

Please sign in to comment.