Completed
Branch v2 (ec0e33)
by Sam
02:09
created

LOC::outputFormatted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 9
cts 9
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
    public function setLatitude(float $latitude): void
70
    {
71
        $this->latitude = (float) $latitude;
72
    }
73
74
    /**
75
     * @param string $format
76
     *
77
     * @return float|string
78
     */
79
    public function getLatitude(string $format = self::FORMAT_DECIMAL)
80
    {
81
        if (self::FORMAT_DMS === $format) {
82
            return $this->toDms($this->latitude, self::LATITUDE);
83
        }
84
85
        return $this->latitude;
86
    }
87
88
    /**
89
     * @param float $longitude
90
     */
91
    public function setLongitude(float $longitude): void
92
    {
93
        $this->longitude = (float) $longitude;
94
    }
95
96
    /**
97
     * @param string $format
98
     *
99
     * @return float|string
100
     */
101
    public function getLongitude(string $format = self::FORMAT_DECIMAL)
102
    {
103
        if (self::FORMAT_DMS === $format) {
104
            return $this->toDms($this->longitude, self::LONGITUDE);
105
        }
106
107
        return $this->longitude;
108
    }
109
110
    /**
111
     * @param float $altitude
112
     *
113
     * @throws \OutOfRangeException
114
     */
115
    public function setAltitude(float $altitude): void
116
    {
117
        if ($altitude < -100000.00 || $altitude > 42849672.95) {
118
            throw new \OutOfRangeException('The altitude must be on [-100000.00, 42849672.95].');
119
        }
120
121
        $this->altitude = (float) $altitude;
122
    }
123
124
    /**
125
     * @return float
126
     */
127
    public function getAltitude(): float
128
    {
129
        return $this->altitude;
130
    }
131
132
    /**
133
     * @param float $horizontalPrecision
134
     *
135
     * @throws \OutOfRangeException
136
     */
137
    public function setHorizontalPrecision(float $horizontalPrecision): void
138
    {
139
        if ($horizontalPrecision < 0 || $horizontalPrecision > 90000000.0) {
140
            throw new \OutOfRangeException('The horizontal precision must be on [0, 90000000.0].');
141
        }
142
143
        $this->horizontalPrecision = (float) $horizontalPrecision;
144
    }
145
146
    /**
147
     * @return float
148
     */
149
    public function getHorizontalPrecision(): float
150
    {
151
        return $this->horizontalPrecision;
152
    }
153
154
    /**
155
     * @param float $size
156
     *
157
     * @throws \OutOfRangeException
158
     */
159
    public function setSize(float $size): void
160
    {
161
        if ($size < 0 || $size > 90000000.0) {
162
            throw new \OutOfRangeException('The size must be on [0, 90000000.0].');
163
        }
164
165
        $this->size = (float) $size;
166
    }
167
168
    /**
169
     * @return float
170
     */
171
    public function getSize(): float
172
    {
173
        return $this->size;
174
    }
175
176
    /**
177
     * @param float $verticalPrecision
178
     *
179
     * @throws \OutOfRangeException
180
     */
181
    public function setVerticalPrecision(float $verticalPrecision): void
182
    {
183
        if ($verticalPrecision < 0 || $verticalPrecision > 90000000.0) {
184
            throw new \OutOfRangeException('The vertical precision must be on [0, 90000000.0].');
185
        }
186
187
        $this->verticalPrecision = $verticalPrecision;
188
    }
189
190
    /**
191
     * @return float
192
     */
193
    public function getVerticalPrecision(): float
194
    {
195
        return $this->verticalPrecision;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function output(): string
202
    {
203
        return sprintf(
204
                '%s %s %.2fm %.2fm %.2fm %.2fm',
205
                $this->getLatitude(self::FORMAT_DMS),
206
                $this->getLongitude(self::FORMAT_DMS),
207
                $this->altitude,
208
                $this->size,
209
                $this->horizontalPrecision,
210
                $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
    private function toDms(float $decimal, string $axis = self::LATITUDE): string
223
    {
224
        $d = (int) floor(abs($decimal));
225
        $m = (int) floor((abs($decimal) - $d) * 60);
226
        $s = ((abs($decimal) - $d) * 60 - $m) * 60;
227
        if (self::LATITUDE === $axis) {
228
            $h = ($decimal < 0) ? 'S' : 'N';
229
        } else {
230
            $h = ($decimal < 0) ? 'W' : 'E';
231
        }
232
233
        return sprintf('%d %d %.3f %s', $d, $m, $s, $h);
234
    }
235
}
236