Completed
Push — master ( f63b7d...5836e1 )
by Sam
02:24
created

APL::output()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4555
c 0
b 0
f 0
cc 5
nc 9
nop 0
crap 5
1
<?php
2
3
namespace Badcow\DNS\Rdata;
4
5
6
use Badcow\DNS\ResourceRecord;
7
8
class APL implements RdataInterface, FormattableInterface
9
{
10
    use RdataTrait;
11
    use FormattableTrait;
12
13
    const TYPE = 'APL';
14
15
    /**
16
     * @var \IPBlock[]
17
     */
18
    private $includedAddressRanges = [];
19
20
    /**
21
     * @var \IPBlock[]
22
     */
23
    private $excludedAddressRanges = [];
24
25
    /**
26
     * @param \IPBlock $ipBlock
27
     * @param bool $included True if the resource exists within the range, False if the resource
28
     * is not within the range. I.E. the negation.
29
     */
30 3
    public function addAddressRange(\IPBlock $ipBlock, $included = true)
31
    {
32 3
        if ($included) {
33 3
            $this->includedAddressRanges[] = $ipBlock;
34
        } else {
35 3
            $this->excludedAddressRanges[] = $ipBlock;
36
        }
37 3
    }
38
39
    /**
40
     * @return \IPBlock[]
41
     */
42
    public function getIncludedAddressRanges(): array
43
    {
44
        return $this->includedAddressRanges;
45
    }
46
47
    /**
48
     * @return \IPBlock[]
49
     */
50
    public function getExcludedAddressRanges(): array
51
    {
52
        return $this->excludedAddressRanges;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 3
    public function output()
59
    {
60 3
        $string = '';
61 3
        foreach ($this->includedAddressRanges as $ipBlock) {
62 3
            $string .= (4 === $ipBlock->getVersion())? '1:' : '2:';
63 3
            $string .= (string) $ipBlock.' ';
64
        }
65
66 3
        foreach ($this->excludedAddressRanges as $ipBlock) {
67 3
            $string .= (4 === $ipBlock->getVersion())? '!1:' : '!2:';
68 3
            $string .= (string) $ipBlock.' ';
69
        }
70
71 3
        return rtrim($string, ' ');
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function outputFormatted()
78
    {
79
        $string = ResourceRecord::MULTILINE_BEGIN.PHP_EOL;
80
        foreach (explode(' ', $this->output()) as $block) {
81
            $string .= $this->makeLine($block);
82
        }
83
84
        return $string.str_repeat(' ', $this->padding).ResourceRecord::MULTILINE_END;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function longestVarLength()
91
    {
92
        $l = 0;
93
        foreach (explode(' ', $this->output()) as $block) {
94
            $l = ($l < strlen($block)) ? strlen($block) : $l;
95
        }
96
97
        return $l;
98
    }
99
}