Completed
Push — master ( b51bc7...ffb45a )
by Marcus
01:53
created

DecimalMinutes::getLngPrefix()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Coordinate Formatter "DecimalMinutes"
4
 *
5
 * @category  Location
6
 * @package   Formatter
7
 * @author    Marcus Jaschen <[email protected]>
8
 * @license   https://opensource.org/licenses/GPL-3.0 GPL
9
 * @link      https://github.com/mjaschen/phpgeo
10
 */
11
12
namespace Location\Formatter\Coordinate;
13
14
use Location\Coordinate;
15
16
/**
17
 * Coordinate Formatter "DecimalMinutes"
18
 *
19
 * @category Location
20
 * @package  Formatter
21
 * @author   Marcus Jaschen <[email protected]>
22
 * @license  https://opensource.org/licenses/GPL-3.0 GPL
23
 * @link     https://github.com/mjaschen/phpgeo
24
 */
25
class DecimalMinutes implements FormatterInterface
26
{
27
    const UNITS_UTF8  = 'UTF-8';
28
    const UNITS_ASCII = 'ASCII';
29
30
    /**
31
     * @var string Separator string between latitude and longitude
32
     */
33
    protected $separator;
34
35
    /**
36
     * Use cardinal letters for N/S and W/E instead of minus sign
37
     *
38
     * @var bool
39
     */
40
    protected $useCardinalLetters;
41
42
    /**
43
     * @var string
44
     */
45
    protected $unitType;
46
47
    /**
48
     * @var int
49
     */
50
    protected $digits = 3;
51
52
    /**
53
     * @var string
54
     */
55
    protected $decimalPoint = '.';
56
57
    /**
58
     * @var array
59
     */
60
    protected $units = [
61
        'UTF-8' => [
62
            'deg' => '°',
63
            'min' => '′',
64
        ],
65
        'ASCII' => [
66
            'deg' => '°',
67
            'min' => '\'',
68
        ],
69
    ];
70
71
    /**
72
     * @param string $separator
73
     */
74
    public function __construct($separator = " ")
75
    {
76
        $this->setSeparator($separator);
77
        $this->useCardinalLetters(false);
78
        $this->setUnits(static::UNITS_UTF8);
79
    }
80
81
    /**
82
     * Sets the separator between latitude and longitude values
83
     *
84
     * @param $separator
85
     *
86
     * @return DecimalMinutes
87
     */
88
    public function setSeparator($separator)
89
    {
90
        $this->separator = $separator;
91
92
        return $this;
93
    }
94
95
    /**
96
     * @param bool $value
97
     *
98
     * @return DecimalMinutes
99
     */
100
    public function useCardinalLetters($value)
101
    {
102
        $this->useCardinalLetters = $value;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @param string $type
109
     *
110
     * @throws \InvalidArgumentException
111
     */
112
    public function setUnits($type)
113
    {
114
        if (! array_key_exists($type, $this->units)) {
115
            throw new \InvalidArgumentException("Invalid unit type");
116
        }
117
118
        $this->unitType = $type;
119
    }
120
121
    /**
122
     * @param int $digits
123
     *
124
     * @return DecimalMinutes
125
     */
126
    public function setDigits($digits)
127
    {
128
        $this->digits = $digits;
129
130
        return $this;
131
    }
132
133
    /**
134
     * @param string $decimalPoint
135
     *
136
     * @return DecimalMinutes
137
     */
138
    public function setDecimalPoint($decimalPoint)
139
    {
140
        $this->decimalPoint = $decimalPoint;
141
142
        return $this;
143
    }
144
145
    /**
146
     * @param Coordinate $coordinate
147
     *
148
     * @return string
149
     */
150
    public function format(Coordinate $coordinate)
151
    {
152
        $lat = $coordinate->getLat();
153
        $lng = $coordinate->getLng();
154
155
        $latValue   = abs($lat);
156
        $latDegrees = intval($latValue);
157
158
        $latMinutesDecimal = $latValue - $latDegrees;
159
        $latMinutes        = 60 * $latMinutesDecimal;
160
161
        $lngValue   = abs($lng);
162
        $lngDegrees = intval($lngValue);
163
164
        $lngMinutesDecimal = $lngValue - $lngDegrees;
165
        $lngMinutes        = 60 * $lngMinutesDecimal;
166
167
        return sprintf(
168
            "%s%02d%s %s%s%s%s%s%03d%s %s%s%s",
169
            $this->getLatPrefix($lat),
170
            abs($latDegrees),
171
            $this->units[$this->unitType]['deg'],
172
            number_format($latMinutes, $this->digits, $this->decimalPoint, $this->decimalPoint),
173
            $this->units[$this->unitType]['min'],
174
            $this->getLatSuffix($lat),
175
            $this->separator,
176
            $this->getLngPrefix($lng),
177
            abs($lngDegrees),
178
            $this->units[$this->unitType]['deg'],
179
            number_format($lngMinutes, $this->digits, $this->decimalPoint, $this->decimalPoint),
180
            $this->units[$this->unitType]['min'],
181
            $this->getLngSuffix($lng)
182
        );
183
    }
184
185
    /**
186
     * @param $lat
187
     *
188
     * @return string
189
     */
190
    protected function getLatPrefix($lat)
191
    {
192
        if ($this->useCardinalLetters || $lat >= 0) {
193
            return '';
194
        }
195
196
        return '-';
197
    }
198
199
    /**
200
     * @param $lng
201
     *
202
     * @return string
203
     */
204
    protected function getLngPrefix($lng)
205
    {
206
        if ($this->useCardinalLetters || $lng >= 0) {
207
            return '';
208
        }
209
210
        return '-';
211
    }
212
213
    /**
214
     * @param $lat
215
     *
216
     * @return string
217
     */
218
    protected function getLatSuffix($lat)
219
    {
220
        if (! $this->useCardinalLetters) {
221
            return '';
222
        }
223
224
        if ($lat >= 0) {
225
            return ' N';
226
        }
227
228
        return ' S';
229
    }
230
231
    /**
232
     * @param $lng
233
     *
234
     * @return string
235
     */
236
    protected function getLngSuffix($lng)
237
    {
238
        if (! $this->useCardinalLetters) {
239
            return '';
240
        }
241
242
        if ($lng >= 0) {
243
            return ' E';
244
        }
245
246
        return ' W';
247
    }
248
}
249