Completed
Push — master ( 1d13ba...f9bf31 )
by Sam
49:13
created

LOC   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 2
dl 0
loc 252
rs 9.92
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setLatitude() 0 4 1
A getLatitude() 0 8 2
A setLongitude() 0 4 1
A getLongitude() 0 8 2
A setAltitude() 0 8 3
A getAltitude() 0 4 1
A setHorizontalPrecision() 0 8 3
A getHorizontalPrecision() 0 4 1
A setSize() 0 8 3
A getSize() 0 4 1
A setVerticalPrecision() 0 8 3
A getVerticalPrecision() 0 4 1
A output() 0 12 1
A outputFormatted() 0 11 1
A longestVarLength() 0 17 3
A toDms() 0 13 4
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
use Badcow\DNS\ResourceRecord;
15
16
/**
17
 * Class LocRdata.
18
 *
19
 * Mechanism to allow the DNS to carry location
20
 * information about hosts, networks, and subnets.
21
 *
22
 * @link http://tools.ietf.org/html/rfc1876
23
 */
24
class LOC implements RdataInterface, FormattableInterface
25
{
26
    use RdataTrait, FormattableTrait;
27
28
    const TYPE = 'LOC';
29
30
    const LATITUDE = 'LATITUDE';
31
32
    const LONGITUDE = 'LONGITUDE';
33
34
    const FORMAT_DECIMAL = 'DECIMAL';
35
36
    const FORMAT_DMS = 'DMS';
37
38
    /**
39
     * @var float
40
     */
41
    private $latitude;
42
43
    /**
44
     * @var float
45
     */
46
    private $longitude;
47
48
    /**
49
     * @var float
50
     */
51
    private $altitude = 0.0;
52
53
    /**
54
     * @var float
55
     */
56
    private $size = 1.0;
57
58
    /**
59
     * @var float
60
     */
61
    private $horizontalPrecision = 10000.0;
62
63
    /**
64
     * @var float
65
     */
66
    private $verticalPrecision = 10.0;
67
68
    /**
69
     * @param float $latitude
70
     */
71
    public function setLatitude($latitude)
72
    {
73
        $this->latitude = (double) $latitude;
74
    }
75
76
    /**
77
     * @param string $format
78
     *
79
     * @return float|string
80
     */
81
    public function getLatitude($format = self::FORMAT_DECIMAL)
82
    {
83
        if ($format === self::FORMAT_DMS) {
84
            return $this->toDms($this->latitude, self::LATITUDE);
85
        }
86
87
        return $this->latitude;
88
    }
89
90
    /**
91
     * @param float $longitude
92
     */
93
    public function setLongitude($longitude)
94
    {
95
        $this->longitude = (double) $longitude;
96
    }
97
98
    /**
99
     * @param string $format
100
     *
101
     * @return float|string
102
     */
103
    public function getLongitude($format = self::FORMAT_DECIMAL)
104
    {
105
        if ($format === self::FORMAT_DMS) {
106
            return $this->toDms($this->longitude, self::LONGITUDE);
107
        }
108
109
        return $this->longitude;
110
    }
111
112
    /**
113
     * @param float $altitude
114
     *
115
     * @throws \OutOfRangeException
116
     */
117
    public function setAltitude($altitude)
118
    {
119
        if ($altitude < -100000.00 || $altitude > 42849672.95) {
120
            throw new \OutOfRangeException('The altitude must be on [-100000.00, 42849672.95].');
121
        }
122
123
        $this->altitude = (double) $altitude;
124
    }
125
126
    /**
127
     * @return float
128
     */
129
    public function getAltitude()
130
    {
131
        return $this->altitude;
132
    }
133
134
    /**
135
     * @param float $horizontalPrecision
136
     *
137
     * @throws \OutOfRangeException
138
     */
139
    public function setHorizontalPrecision($horizontalPrecision)
140
    {
141
        if ($horizontalPrecision < 0 || $horizontalPrecision > 90000000.0) {
142
            throw new \OutOfRangeException('The horizontal precision must be on [0, 90000000.0].');
143
        }
144
145
        $this->horizontalPrecision = (double) $horizontalPrecision;
146
    }
147
148
    /**
149
     * @return float
150
     */
151
    public function getHorizontalPrecision()
152
    {
153
        return $this->horizontalPrecision;
154
    }
155
156
    /**
157
     * @param float $size
158
     *
159
     * @throws \OutOfRangeException
160
     */
161
    public function setSize($size)
162
    {
163
        if ($size < 0 || $size > 90000000.0) {
164
            throw new \OutOfRangeException('The size must be on [0, 90000000.0].');
165
        }
166
167
        $this->size = (double) $size;
168
    }
169
170
    /**
171
     * @return float
172
     */
173
    public function getSize()
174
    {
175
        return $this->size;
176
    }
177
178
    /**
179
     * @param float $verticalPrecision
180
     *
181
     * @throws \OutOfRangeException
182
     */
183
    public function setVerticalPrecision($verticalPrecision)
184
    {
185
        if ($verticalPrecision < 0 || $verticalPrecision > 90000000.0) {
186
            throw new \OutOfRangeException('The vertical precision must be on [0, 90000000.0].');
187
        }
188
189
        $this->verticalPrecision = $verticalPrecision;
190
    }
191
192
    /**
193
     * @return float
194
     */
195
    public function getVerticalPrecision()
196
    {
197
        return $this->verticalPrecision;
198
    }
199
200
    /**
201
     * {@inheritdoc}
202
     */
203
    public function output()
204
    {
205
        return sprintf(
206
                '%s %s %.2fm %.2fm %.2fm %.2fm',
207
                $this->getLatitude(self::FORMAT_DMS),
208
                $this->getLongitude(self::FORMAT_DMS),
209
                $this->altitude,
210
                $this->size,
211
                $this->horizontalPrecision,
212
                $this->verticalPrecision
213
        );
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function outputFormatted()
220
    {
221
        return ResourceRecord::MULTILINE_BEGIN . PHP_EOL .
222
            $this->makeLine($this->getLatitude(self::FORMAT_DMS), 'LATITUDE') .
223
            $this->makeLine($this->getLongitude(self::FORMAT_DMS), 'LONGITUDE') .
224
            $this->makeLine(sprintf('%.2fm', $this->altitude), 'ALTITUDE') .
225
            $this->makeLine(sprintf('%.2fm', $this->size), 'SIZE') .
226
            $this->makeLine(sprintf('%.2fm', $this->horizontalPrecision), 'HORIZONTAL PRECISION') .
227
            $this->makeLine(sprintf('%.2fm', $this->verticalPrecision), 'VERTICAL PRECISION') .
228
            str_repeat(' ', $this->padding) . ResourceRecord::MULTILINE_END;
229
    }
230
231
    /**
232
     * Determines the longest variable.
233
     *
234
     * @return int
235
     */
236
    public function longestVarLength()
237
    {
238
        $l = 0;
239
240
        foreach ([
241
                        $this->getLatitude(self::FORMAT_DMS),
242
                        $this->getLongitude(self::FORMAT_DMS),
243
                        sprintf('%.2fm', $this->altitude),
244
                        sprintf('%.2fm', $this->size),
245
                        sprintf('%.2fm', $this->horizontalPrecision),
246
                        sprintf('%.2fm', $this->verticalPrecision),
247
                ] as $var) {
248
            $l = ($l < strlen($var)) ? strlen($var) : $l;
249
        }
250
251
        return $l;
252
    }
253
254
    /**
255
     * Determine the degree minute seconds value from decimal.
256
     *
257
     * @param $decimal
258
     * @param string $axis
259
     *
260
     * @return string
261
     */
262
    private function toDms($decimal, $axis = self::LATITUDE)
263
    {
264
        $d = (int) floor(abs($decimal));
265
        $m = (int) floor((abs($decimal) - $d) * 60);
266
        $s = ((abs($decimal) - $d) * 60 - $m) * 60;
267
        if ($axis === self::LATITUDE) {
268
            $h = ($decimal < 0) ? 'S' : 'N';
269
        } else {
270
            $h = ($decimal < 0) ? 'W' : 'E';
271
        }
272
273
        return sprintf('%d %d %.3f %s', $d, $m, $s, $h);
274
    }
275
}
276