diff --git a/README.md b/README.md index ad7ffa2..a848326 100644 --- a/README.md +++ b/README.md @@ -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; diff --git a/src/Records/PTR.php b/src/Records/PTR.php new file mode 100644 index 0000000..42d692a --- /dev/null +++ b/src/Records/PTR.php @@ -0,0 +1,50 @@ + $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); + } +} diff --git a/src/Support/Types.php b/src/Support/Types.php index 7ddf412..afdd1c3 100644 --- a/src/Support/Types.php +++ b/src/Support/Types.php @@ -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', diff --git a/tests/DnsTest.php b/tests/DnsTest.php index dd481b5..aeddefc 100644 --- a/tests/DnsTest.php +++ b/tests/DnsTest.php @@ -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', diff --git a/tests/Records/PTRTest.php b/tests/Records/PTRTest.php new file mode 100644 index 0000000..8f45a33 --- /dev/null +++ b/tests/Records/PTRTest.php @@ -0,0 +1,74 @@ +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); + } +} diff --git a/tests/Support/FactoryTest.php b/tests/Support/FactoryTest.php index 55c6239..543d672 100644 --- a/tests/Support/FactoryTest.php +++ b/tests/Support/FactoryTest.php @@ -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; @@ -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"'], diff --git a/tests/Support/TypesTest.php b/tests/Support/TypesTest.php index 3e0c8b2..3bb1573 100644 --- a/tests/Support/TypesTest.php +++ b/tests/Support/TypesTest.php @@ -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)); @@ -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']));