-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from lwohn/add-ptr-record-type
Add PTR to supported record types.
- Loading branch information
Showing
7 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters