Passed
Pull Request — master (#11)
by Christian
04:46
created

DataAbstract::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace RemotelyLiving\PHPDNS\Entities;
3
4
use RemotelyLiving\PHPDNS\Entities\Interfaces\Arrayable;
5
use RemotelyLiving\PHPDNS\Entities\Interfaces\Serializable;
6
use RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException;
7
8
abstract class DataAbstract implements Arrayable, Serializable
9
{
10
    abstract public function __toString(): string;
11
12
    abstract public function toArray() : array;
13
14
    public function equals(DataAbstract $dataAbstract): bool
15
    {
16
        return (string)$this === (string)$dataAbstract;
17
    }
18
19
    /**
20
     * @throws \RemotelyLiving\PHPDNS\Exceptions\InvalidArgumentException
21
     */
22
    public static function createFromTypeAndString(DNSRecordType $recordType, string $data): self
23
    {
24
        if ($recordType->isA(DNSRecordType::TYPE_TXT)) {
25
            return new TXTData($data);
26
        }
27
28
        if ($recordType->isA(DNSRecordType::TYPE_NS)) {
29
            return new NSData(new Hostname($data));
30
        }
31
32
        if ($recordType->isA(DNSRecordType::TYPE_CNAME)) {
33
            return new CNAMEData(new Hostname($data));
34
        }
35
36
        $parsed = self::parseDataToArray($data);
37
38
        if ($recordType->isA(DNSRecordType::TYPE_MX)) {
39
            return new MXData(new Hostname($parsed[1]), (int)$parsed[0]);
40
        }
41
42
        if ($recordType->isA(DNSRecordType::TYPE_SOA)) {
43
            return new SOAData(
44
                new Hostname($parsed[0]),
45
                new Hostname($parsed[1]),
46
                (int)$parsed[2] ?? 0,
47
                (int)$parsed[3] ?? 0,
48
                (int)$parsed[4] ?? 0,
49
                (int)$parsed[5] ?? 0,
50
                (int)$parsed[6] ?? 0
51
            );
52
        }
53
54
55
56
        if ($recordType->isA(DNSRecordType::TYPE_CAA)) {
57
            return new CAAData($parsed[0], $parsed[1], $parsed[2]);
0 ignored issues
show
Bug introduced by
$parsed[0] of type string is incompatible with the type integer expected by parameter $flags of RemotelyLiving\PHPDNS\En...\CAAData::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
            return new CAAData(/** @scrutinizer ignore-type */ $parsed[0], $parsed[1], $parsed[2]);
Loading history...
58
        }
59
60
        throw new InvalidArgumentException("{$data} could not be created with type {$recordType}");
61
    }
62
63
    public function jsonSerialize(): array
64
    {
65
        return $this->toArray();
66
    }
67
68
    private static function parseDataToArray(string $data): array
69
    {
70
        return explode(' ', $data);
71
    }
72
}
73