remotelyliving /
php-dns
| 1 | <?php |
||
| 2 | |||
| 3 | namespace RemotelyLiving\PHPDNS\Entities; |
||
| 4 | |||
| 5 | use RemotelyLiving\PHPDNS\Exceptions; |
||
| 6 | |||
| 7 | use function preg_match; |
||
| 8 | use function serialize; |
||
| 9 | use function str_ireplace; |
||
| 10 | use function trim; |
||
| 11 | use function unserialize; |
||
| 12 | |||
| 13 | final class CAAData extends DataAbstract implements \Stringable |
||
| 14 | { |
||
| 15 | private ?string $value; |
||
| 16 | |||
| 17 | public function __construct(private int $flags, private string $tag, string $value = null) |
||
| 18 | { |
||
| 19 | $this->value = ($value) |
||
| 20 | ? $this->normalizeValue($value) |
||
| 21 | : null; |
||
| 22 | } |
||
| 23 | |||
| 24 | public function __toString(): string |
||
| 25 | { |
||
| 26 | return "{$this->flags} {$this->tag} \"{$this->value}\""; |
||
| 27 | } |
||
| 28 | |||
| 29 | public function __unserialize(array $unserialized): void |
||
| 30 | { |
||
| 31 | $this->flags = $unserialized['flags']; |
||
| 32 | $this->tag = $unserialized['tag']; |
||
| 33 | $this->value = $unserialized['value']; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function getFlags(): int |
||
| 37 | { |
||
| 38 | return $this->flags; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function getTag(): string |
||
| 42 | { |
||
| 43 | return $this->tag; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function getValue(): ?string |
||
| 47 | { |
||
| 48 | return $this->value; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function toArray(): array |
||
| 52 | { |
||
| 53 | return [ |
||
| 54 | 'flags' => $this->flags, |
||
| 55 | 'tag' => $this->tag, |
||
| 56 | 'value' => $this->value, |
||
| 57 | ]; |
||
| 58 | } |
||
| 59 | |||
| 60 | private function normalizeValue(string $value): string |
||
| 61 | { |
||
| 62 | $normalized = trim(str_ireplace('"', '', $value)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 63 | |||
| 64 | if (preg_match('/\s/m', $normalized)) { |
||
| 65 | throw new Exceptions\InvalidArgumentException("$value is not a valid CAA value"); |
||
| 66 | } |
||
| 67 | |||
| 68 | return $normalized; |
||
| 69 | } |
||
| 70 | } |
||
| 71 |