Completed
Push — master ( 5836e1...99cc50 )
by Sam
15s queued 10s
created

LOC::setSize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of Badcow DNS Library.
5
 *
6
 * (c) Samuel Williams <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Badcow\DNS\Rdata;
13
14
/**
15
 * Class LocRdata.
16
 *
17
 * Mechanism to allow the DNS to carry location
18
 * information about hosts, networks, and subnets.
19
 *
20
 * @see http://tools.ietf.org/html/rfc1876
21
 */
22
class LOC implements RdataInterface
23
{
24
    use RdataTrait;
25
26
    const TYPE = 'LOC';
27
28
    const LATITUDE = 'LATITUDE';
29
30
    const LONGITUDE = 'LONGITUDE';
31
32
    const FORMAT_DECIMAL = 'DECIMAL';
33
34
    const FORMAT_DMS = 'DMS';
35
36
    /**
37
     * @var float
38
     */
39
    private $latitude;
40
41
    /**
42
     * @var float
43
     */
44
    private $longitude;
45
46
    /**
47
     * @var float
48
     */
49
    private $altitude = 0.0;
50
51
    /**
52
     * @var float
53
     */
54
    private $size = 1.0;
55
56
    /**
57
     * @var float
58
     */
59
    private $horizontalPrecision = 10000.0;
60
61
    /**
62
     * @var float
63
     */
64
    private $verticalPrecision = 10.0;
65
66
    /**
67
     * @param float $latitude
68
     */
69 8
    public function setLatitude(float $latitude): void
70
    {
71 8
        $this->latitude = (float) $latitude;
72 8
    }
73
74
    /**
75
     * @param string $format
76
     *
77
     * @return float|string
78
     */
79 4
    public function getLatitude(string $format = self::FORMAT_DECIMAL)
80
    {
81 4
        if (self::FORMAT_DMS === $format) {
82 4
            return $this->toDms($this->latitude, self::LATITUDE);
83
        }
84
85 1
        return $this->latitude;
86
    }
87
88
    /**
89
     * @param float $longitude
90
     */
91 8
    public function setLongitude(float $longitude): void
92
    {
93 8
        $this->longitude = (float) $longitude;
94 8
    }
95
96
    /**
97
     * @param string $format
98
     *
99
     * @return float|string
100
     */
101 4
    public function getLongitude(string $format = self::FORMAT_DECIMAL)
102
    {
103 4
        if (self::FORMAT_DMS === $format) {
104 4
            return $this->toDms($this->longitude, self::LONGITUDE);
105
        }
106
107 1
        return $this->longitude;
108
    }
109
110
    /**
111
     * @param float $altitude
112
     *
113
     * @throws \OutOfRangeException
114
     */
115 10
    public function setAltitude(float $altitude): void
116
    {
117 10
        if ($altitude < -100000.00 || $altitude > 42849672.95) {
118 2
            throw new \OutOfRangeException('The altitude must be on [-100000.00, 42849672.95].');
119
        }
120
121 8
        $this->altitude = (float) $altitude;
122 8
    }
123
124
    /**
125
     * @return float
126
     */
127 2
    public function getAltitude(): float
128
    {
129 2
        return $this->altitude;
130
    }
131
132
    /**
133
     * @param float $horizontalPrecision
134
     *
135
     * @throws \OutOfRangeException
136
     */
137 10
    public function setHorizontalPrecision(float $horizontalPrecision): void
138
    {
139 10
        if ($horizontalPrecision < 0 || $horizontalPrecision > 90000000.0) {
140 2
            throw new \OutOfRangeException('The horizontal precision must be on [0, 90000000.0].');
141
        }
142
143 8
        $this->horizontalPrecision = (float) $horizontalPrecision;
144 8
    }
145
146
    /**
147
     * @return float
148
     */
149 2
    public function getHorizontalPrecision(): float
150
    {
151 2
        return $this->horizontalPrecision;
152
    }
153
154
    /**
155
     * @param float $size
156
     *
157
     * @throws \OutOfRangeException
158
     */
159 10
    public function setSize(float $size): void
160
    {
161 10
        if ($size < 0 || $size > 90000000.0) {
162 2
            throw new \OutOfRangeException('The size must be on [0, 90000000.0].');
163
        }
164
165 8
        $this->size = (float) $size;
166 8
    }
167
168
    /**
169
     * @return float
170
     */
171 2
    public function getSize(): float
172
    {
173 2
        return $this->size;
174
    }
175
176
    /**
177
     * @param float $verticalPrecision
178
     *
179
     * @throws \OutOfRangeException
180
     */
181 10
    public function setVerticalPrecision(float $verticalPrecision): void
182
    {
183 10
        if ($verticalPrecision < 0 || $verticalPrecision > 90000000.0) {
184 2
            throw new \OutOfRangeException('The vertical precision must be on [0, 90000000.0].');
185
        }
186
187 8
        $this->verticalPrecision = $verticalPrecision;
188 8
    }
189
190
    /**
191
     * @return float
192
     */
193 2
    public function getVerticalPrecision(): float
194
    {
195 2
        return $this->verticalPrecision;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201 2
    public function output(): string
202
    {
203 2
        return sprintf(
204 2
                '%s %s %.2fm %.2fm %.2fm %.2fm',
205 2
                $this->getLatitude(self::FORMAT_DMS),
206 2
                $this->getLongitude(self::FORMAT_DMS),
207 2
                $this->altitude,
208 2
                $this->size,
209 2
                $this->horizontalPrecision,
210 2
                $this->verticalPrecision
211
        );
212
    }
213
214
    /**
215
     * Determine the degree minute seconds value from decimal.
216
     *
217
     * @param float  $decimal
218
     * @param string $axis
219
     *
220
     * @return string
221
     */
222 5
    private function toDms(float $decimal, string $axis = self::LATITUDE): string
223
    {
224 5
        $d = (int) floor(abs($decimal));
225 5
        $m = (int) floor((abs($decimal) - $d) * 60);
226 5
        $s = ((abs($decimal) - $d) * 60 - $m) * 60;
227 5
        if (self::LATITUDE === $axis) {
228 4
            $h = ($decimal < 0) ? 'S' : 'N';
229
        } else {
230 4
            $h = ($decimal < 0) ? 'W' : 'E';
231
        }
232
233 5
        return sprintf('%d %d %.3f %s', $d, $m, $s, $h);
234
    }
235
}
236