• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

remotelyliving / php-dns / 7742771606

01 Feb 2024 02:25PM UTC coverage: 99.856% (-0.1%) from 100.0%
7742771606

Pull #56

github

web-flow
Merge fa8df85e4 into 711697608
Pull Request #56: Update to supported versions

694 of 695 relevant lines covered (99.86%)

17.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/src/Entities/DataAbstract.php
1
<?php
2

3
namespace RemotelyLiving\PHPDNS\Entities;
4

5
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable;
6
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;
7
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
8

9
use function count;
10
use function explode;
11
use function trim;
12

13
abstract class DataAbstract implements Arrayable, Serializable, \Stringable
14
{
15
    abstract public function __toString(): string;
16

17
    abstract public function toArray(): array;
18

19
    public function __serialize(): array
20
    {
21
        return $this->toArray();
10✔
22
    }
23

24
    public function equals(DataAbstract $dataAbstract): bool
25
    {
26
        return (string)$this === (string)$dataAbstract;
9✔
27
    }
28

29
    /**
30
     * @throws \RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException
31
     */
32
    public static function createFromTypeAndString(DNSRecordType $recordType, string $data): self
33
    {
34
        if ($recordType->isA(DNSRecordType::TYPE_TXT)) {
24✔
35
            return new TXTData(trim($data, '"'));
12✔
36
        }
37

38
        if ($recordType->isA(DNSRecordType::TYPE_NS)) {
18✔
39
            return new NSData(new Hostname($data));
5✔
40
        }
41

42
        if ($recordType->isA(DNSRecordType::TYPE_CNAME)) {
18✔
43
            return new CNAMEData(new Hostname($data));
3✔
44
        }
45

46
        $parsed = self::parseDataToArray($data);
17✔
47

48
        if ($recordType->isA(DNSRecordType::TYPE_MX)) {
17✔
49
            return new MXData(new Hostname($parsed[1]), (int)$parsed[0]);
6✔
50
        }
51

52
        if ($recordType->isA(DNSRecordType::TYPE_SOA)) {
16✔
53
            return new SOAData(
13✔
54
                new Hostname($parsed[0]),
13✔
55
                new Hostname($parsed[1]),
13✔
56
                (int)($parsed[2] ?? 0),
13✔
57
                (int)($parsed[3] ?? 0),
13✔
58
                (int)($parsed[4] ?? 0),
13✔
59
                (int)($parsed[5] ?? 0),
13✔
60
                (int)($parsed[6] ?? 0)
13✔
61
            );
13✔
62
        }
63

64
        if ($recordType->isA(DNSRecordType::TYPE_CAA) && count($parsed) === 3) {
8✔
65
            return new CAAData((int)$parsed[0], (string)$parsed[1], $parsed[2]);
2✔
66
        }
67

68
        if ($recordType->isA(DNSRecordType::TYPE_SRV)) {
8✔
69
            return new SRVData(
2✔
70
                (int)$parsed[0] ?: 0,
2✔
71
                (int) $parsed[1] ?: 0,
2✔
72
                (int) $parsed[2] ?: 0,
2✔
73
                new Hostname($parsed[3])
2✔
74
            );
2✔
75
        }
76

77
        if ($recordType->isA(DNSRecordType::TYPE_PTR)) {
7✔
78
            return new PTRData(new Hostname($data));
2✔
79
        }
80

81
        throw new InvalidArgumentException("{$data} could not be created with type {$recordType}");
5✔
82
    }
83

84
    public function jsonSerialize(): array
85
    {
86
        return $this->toArray();
8✔
87
    }
88

89
    protected function init(): void
90
    {
91
    }
×
92
    private static function parseDataToArray(string $data): array
93
    {
94
        return explode(' ', $data);
17✔
95
    }
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc