From c0977683137e9b65230a7f1a474b555dc7a9ef21 Mon Sep 17 00:00:00 2001 From: Oliver Kaufmann Date: Sat, 31 Jul 2021 23:39:24 +0200 Subject: [PATCH] Make records arrayable Fix styling --- src/Records/A.php | 11 +++++++++++ src/Records/AAAA.php | 11 +++++++++++ src/Records/CAA.php | 13 +++++++++++++ src/Records/CNAME.php | 11 +++++++++++ src/Records/MX.php | 12 ++++++++++++ src/Records/NS.php | 11 +++++++++++ src/Records/Record.php | 5 +++++ src/Records/SOA.php | 17 +++++++++++++++++ src/Records/SRV.php | 14 ++++++++++++++ src/Records/TXT.php | 11 +++++++++++ tests/Records/AAAATest.php | 19 +++++++++++++++++++ tests/Records/ATest.php | 19 +++++++++++++++++++ tests/Records/CAATest.php | 23 +++++++++++++++++++++++ tests/Records/CNAMETest.php | 19 +++++++++++++++++++ tests/Records/MXTest.php | 21 +++++++++++++++++++++ tests/Records/NSTest.php | 19 +++++++++++++++++++ tests/Records/SOATest.php | 31 +++++++++++++++++++++++++++++++ tests/Records/SRVTest.php | 25 +++++++++++++++++++++++++ tests/Records/TXTTest.php | 19 +++++++++++++++++++ 19 files changed, 311 insertions(+) diff --git a/src/Records/A.php b/src/Records/A.php index f359399..0b83015 100644 --- a/src/Records/A.php +++ b/src/Records/A.php @@ -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, + ]; + } } diff --git a/src/Records/AAAA.php b/src/Records/AAAA.php index e551dab..ec94f87 100644 --- a/src/Records/AAAA.php +++ b/src/Records/AAAA.php @@ -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, + ]; + } } diff --git a/src/Records/CAA.php b/src/Records/CAA.php index 759f41f..d46ea22 100644 --- a/src/Records/CAA.php +++ b/src/Records/CAA.php @@ -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, + ]; + } } diff --git a/src/Records/CNAME.php b/src/Records/CNAME.php index 9a43311..ab09bdf 100644 --- a/src/Records/CNAME.php +++ b/src/Records/CNAME.php @@ -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, + ]; + } } diff --git a/src/Records/MX.php b/src/Records/MX.php index 3ff75fe..448fc4b 100644 --- a/src/Records/MX.php +++ b/src/Records/MX.php @@ -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, + ]; + } } diff --git a/src/Records/NS.php b/src/Records/NS.php index 6980b4e..f6454ce 100644 --- a/src/Records/NS.php +++ b/src/Records/NS.php @@ -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, + ]; + } } diff --git a/src/Records/Record.php b/src/Records/Record.php index 04d734e..e3dda80 100644 --- a/src/Records/Record.php +++ b/src/Records/Record.php @@ -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)) { diff --git a/src/Records/SOA.php b/src/Records/SOA.php index f585ab8..3317e11 100644 --- a/src/Records/SOA.php +++ b/src/Records/SOA.php @@ -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, + ]; + } } diff --git a/src/Records/SRV.php b/src/Records/SRV.php index ac1fe33..bd2b09a 100644 --- a/src/Records/SRV.php +++ b/src/Records/SRV.php @@ -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, + ]; + } } diff --git a/src/Records/TXT.php b/src/Records/TXT.php index 400ab0d..cdade45 100644 --- a/src/Records/TXT.php +++ b/src/Records/TXT.php @@ -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, + ]; + } } diff --git a/tests/Records/AAAATest.php b/tests/Records/AAAATest.php index 610441b..8b0946e 100644 --- a/tests/Records/AAAATest.php +++ b/tests/Records/AAAATest.php @@ -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']); + } } diff --git a/tests/Records/ATest.php b/tests/Records/ATest.php index d07cea9..3d07cbf 100644 --- a/tests/Records/ATest.php +++ b/tests/Records/ATest.php @@ -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']); + } } diff --git a/tests/Records/CAATest.php b/tests/Records/CAATest.php index de1a436..4cabd3d 100644 --- a/tests/Records/CAATest.php +++ b/tests/Records/CAATest.php @@ -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']); + } } diff --git a/tests/Records/CNAMETest.php b/tests/Records/CNAMETest.php index e8e15a7..51ea73d 100644 --- a/tests/Records/CNAMETest.php +++ b/tests/Records/CNAMETest.php @@ -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']); + } } diff --git a/tests/Records/MXTest.php b/tests/Records/MXTest.php index 6974f9c..a5e7e3a 100644 --- a/tests/Records/MXTest.php +++ b/tests/Records/MXTest.php @@ -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']); + } } diff --git a/tests/Records/NSTest.php b/tests/Records/NSTest.php index 625ed47..7525d2a 100644 --- a/tests/Records/NSTest.php +++ b/tests/Records/NSTest.php @@ -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']); + } } diff --git a/tests/Records/SOATest.php b/tests/Records/SOATest.php index 6a076b6..9c9ee07 100644 --- a/tests/Records/SOATest.php +++ b/tests/Records/SOATest.php @@ -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']); + } } diff --git a/tests/Records/SRVTest.php b/tests/Records/SRVTest.php index ec7c83c..0061a98 100644 --- a/tests/Records/SRVTest.php +++ b/tests/Records/SRVTest.php @@ -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']); + } } diff --git a/tests/Records/TXTTest.php b/tests/Records/TXTTest.php index c835e84..a714cb8 100644 --- a/tests/Records/TXTTest.php +++ b/tests/Records/TXTTest.php @@ -44,4 +44,23 @@ public function it_can_transform_to_string() $this->assertSame("spatie.be.\t\t594\tIN\tTXT\t\"v=spf1 include:eu.mailgun.org include:spf.factuursturen.be include:sendgrid.net a mx ~all\"", strval($record)); } + + /** @test */ + public function it_can_be_converted_to_an_array() + { + $record = TXT::make([ + 'host' => 'spatie.be', + 'class' => 'IN', + 'ttl' => 594, + 'type' => 'TXT', + 'txt' => 'v=spf1 include:eu.mailgun.org include:spf.factuursturen.be include:sendgrid.net a mx ~all', + ]); + + $data = $record->toArray(); + $this->assertSame('spatie.be', $data['host']); + $this->assertSame(594, $data['ttl']); + $this->assertSame('IN', $data['class']); + $this->assertSame('TXT', $data['type']); + $this->assertSame('v=spf1 include:eu.mailgun.org include:spf.factuursturen.be include:sendgrid.net a mx ~all', $data['txt']); + } }