Skip to content

Commit

Permalink
Merge pull request #70 from okaufmann/make-records-arrayable
Browse files Browse the repository at this point in the history
Make records arrayable
  • Loading branch information
freekmurze authored Aug 1, 2021
2 parents f9ca33b + c097768 commit cfc8660
Show file tree
Hide file tree
Showing 19 changed files with 311 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Records/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ public function __toString(): string
{
return "{$this->host}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t{$this->ip}";
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'ip' => $this->ip,
];
}
}
11 changes: 11 additions & 0 deletions src/Records/AAAA.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@ public function __toString(): string
{
return "{$this->host}.\t\t{$this->ttl}\t{$this->class}\t{$this->type}\t{$this->ipv6}";
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'ipv6' => $this->ipv6,
];
}
}
13 changes: 13 additions & 0 deletions src/Records/CAA.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,17 @@ protected function castValue(string $value): string
{
return $this->prepareText($value);
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'flags' => $this->flags,
'tag' => $this->tag,
'value' => $this->value,
];
}
}
11 changes: 11 additions & 0 deletions src/Records/CNAME.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ protected function castTarget(string $value): string
{
return $this->prepareDomain($value);
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'target' => $this->target,
];
}
}
12 changes: 12 additions & 0 deletions src/Records/MX.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,16 @@ protected function castTarget(string $value): string
{
return $this->prepareDomain($value);
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'pri' => $this->pri,
'target' => $this->target,
];
}
}
11 changes: 11 additions & 0 deletions src/Records/NS.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ protected function castTarget(string $value): string
{
return $this->prepareDomain($value);
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'target' => $this->target,
];
}
}
5 changes: 5 additions & 0 deletions src/Records/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ abstract public static function parse(string $line): self;

abstract public function __toString(): string;

/**
* @return array
*/
abstract public function toArray();

public function __call(string $name, array $arguments)
{
if (property_exists($this, $name)) {
Expand Down
17 changes: 17 additions & 0 deletions src/Records/SOA.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,21 @@ protected function castMinimumTtl($value): int
{
return $this->prepareInt($value);
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'mname' => $this->mname,
'rname' => $this->rname,
'serial' => $this->serial,
'refresh' => $this->refresh,
'retry' => $this->retry,
'expire' => $this->expire,
'minimum_ttl' => $this->minimum_ttl,
];
}
}
14 changes: 14 additions & 0 deletions src/Records/SRV.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ protected function castTarget(string $value): string
{
return $this->prepareDomain($value);
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'pri' => $this->pri,
'weight' => $this->weight,
'port' => $this->port,
'target' => $this->target,
];
}
}
11 changes: 11 additions & 0 deletions src/Records/TXT.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,15 @@ protected function castTxt(string $value): string
{
return $this->prepareText($value);
}

public function toArray()
{
return [
'host' => $this->host,
'ttl' => $this->ttl,
'class' => $this->class,
'type' => $this->type,
'txt' => $this->txt,
];
}
}
19 changes: 19 additions & 0 deletions tests/Records/AAAATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,23 @@ public function it_can_transform_to_string()

$this->assertSame("google.com.\t\t900\tIN\tAAAA\t2a00:1450:400e:800::200e", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = AAAA::make([
'host' => 'google.com',
'class' => 'IN',
'ttl' => 900,
'type' => 'AAAA',
'ipv6' => '2a00:1450:400e:800::200e',
]);

$data = $record->toArray();
$this->assertSame('google.com', $data['host']);
$this->assertSame(900, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('AAAA', $data['type']);
$this->assertSame('2a00:1450:400e:800::200e', $data['ipv6']);
}
}
19 changes: 19 additions & 0 deletions tests/Records/ATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,23 @@ public function it_can_transform_to_string()

$this->assertSame("spatie.be.\t\t900\tIN\tA\t138.197.187.74", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = A::make([
'host' => 'spatie.be',
'class' => 'IN',
'ttl' => 900,
'type' => 'A',
'ip' => '138.197.187.74',
]);

$data = $record->toArray();
$this->assertSame('spatie.be', $data['host']);
$this->assertSame(900, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('A', $data['type']);
$this->assertSame('138.197.187.74', $data['ip']);
}
}
23 changes: 23 additions & 0 deletions tests/Records/CAATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,27 @@ public function it_can_transform_to_string()

$this->assertSame("google.com.\t\t86400\tIN\tCAA\t0\tissue\t\"pki.goog\"", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = CAA::make([
'host' => 'google.com',
'class' => 'IN',
'ttl' => 86400,
'type' => 'CAA',
'flags' => 0,
'tag' => 'issue',
'value' => 'pki.goog',
]);

$data = $record->toArray();
$this->assertSame('google.com', $data['host']);
$this->assertSame(86400, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('CAA', $data['type']);
$this->assertSame(0, $data['flags']);
$this->assertSame('issue', $data['tag']);
$this->assertSame('pki.goog', $data['value']);
}
}
19 changes: 19 additions & 0 deletions tests/Records/CNAMETest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,23 @@ public function it_can_transform_to_string()

$this->assertSame("www.spatie.be.\t\t300\tIN\tCNAME\tspatie.be.", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = CNAME::make([
'host' => 'www.spatie.be',
'class' => 'IN',
'ttl' => 300,
'type' => 'CNAME',
'target' => 'spatie.be',
]);

$data = $record->toArray();
$this->assertSame('www.spatie.be', $data['host']);
$this->assertSame(300, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('CNAME', $data['type']);
$this->assertSame('spatie.be', $data['target']);
}
}
21 changes: 21 additions & 0 deletions tests/Records/MXTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,25 @@ public function it_can_transform_to_string()

$this->assertSame("spatie.be.\t\t1665\tIN\tMX\t10\taspmx.l.google.com.", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = MX::make([
'host' => 'spatie.be',
'class' => 'IN',
'ttl' => 1665,
'type' => 'MX',
'pri' => 10,
'target' => 'ASPMX.L.GOOGLE.COM',
]);

$data = $record->toArray();
$this->assertSame('spatie.be', $data['host']);
$this->assertSame(1665, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('MX', $data['type']);
$this->assertSame(10, $data['pri']);
$this->assertSame('aspmx.l.google.com', $data['target']);
}
}
19 changes: 19 additions & 0 deletions tests/Records/NSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,23 @@ public function it_can_transform_to_string()

$this->assertSame("spatie.be.\t\t82516\tIN\tNS\tns1.openprovider.nl.", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = NS::make([
'host' => 'spatie.be',
'class' => 'IN',
'ttl' => 82516,
'type' => 'NS',
'target' => 'ns1.openprovider.nl',
]);

$data = $record->toArray();
$this->assertSame('spatie.be', $data['host']);
$this->assertSame(82516, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('NS', $data['type']);
$this->assertSame('ns1.openprovider.nl', $data['target']);
}
}
31 changes: 31 additions & 0 deletions tests/Records/SOATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,35 @@ public function it_can_transform_to_string()

$this->assertSame("spatie.be.\t\t82393\tIN\tSOA\tns1.openprovider.nl.\tdns.openprovider.eu.\t2020100801\t10800\t3600\t604800\t3600", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = SOA::make([
'host' => 'spatie.be',
'class' => 'IN',
'ttl' => 82393,
'type' => 'SOA',
'mname' => 'ns1.openprovider.nl',
'rname' => 'dns.openprovider.eu',
'serial' => 2020100801,
'refresh' => 10800,
'retry' => 3600,
'expire' => 604800,
'minimum_ttl' => 3600,
]);

$data = $record->toArray();
$this->assertSame('spatie.be', $data['host']);
$this->assertSame(82393, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('SOA', $data['type']);
$this->assertSame('ns1.openprovider.nl', $data['mname']);
$this->assertSame('dns.openprovider.eu', $data['rname']);
$this->assertSame(2020100801, $data['serial']);
$this->assertSame(10800, $data['refresh']);
$this->assertSame(3600, $data['retry']);
$this->assertSame(604800, $data['expire']);
$this->assertSame(3600, $data['minimum_ttl']);
}
}
25 changes: 25 additions & 0 deletions tests/Records/SRVTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,29 @@ public function it_can_transform_to_string()

$this->assertSame("_http._tcp.mxtoolbox.com.\t\t3600\tIN\tSRV\t10\t100\t80\tmxtoolbox.com.", strval($record));
}

/** @test */
public function it_can_be_converted_to_an_array()
{
$record = SRV::make([
'host' => '_http._tcp.mxtoolbox.com',
'class' => 'IN',
'ttl' => 3600,
'type' => 'SRV',
'pri' => 10,
'weight' => 100,
'port' => 80,
'target' => 'mxtoolbox.com',
]);

$data = $record->toArray();
$this->assertSame('_http._tcp.mxtoolbox.com', $data['host']);
$this->assertSame(3600, $data['ttl']);
$this->assertSame('IN', $data['class']);
$this->assertSame('SRV', $data['type']);
$this->assertSame(10, $data['pri']);
$this->assertSame(100, $data['weight']);
$this->assertSame(80, $data['port']);
$this->assertSame('mxtoolbox.com', $data['target']);
}
}
Loading

0 comments on commit cfc8660

Please sign in to comment.