1 | <?php |
||
15 | class DecimalMinutes implements FormatterInterface |
||
16 | { |
||
17 | public const UNITS_UTF8 = 'UTF-8'; |
||
18 | public const UNITS_ASCII = 'ASCII'; |
||
19 | |||
20 | /** |
||
21 | * @var string Separator string between latitude and longitude |
||
22 | */ |
||
23 | protected $separator; |
||
24 | |||
25 | /** |
||
26 | * Use cardinal letters for N/S and W/E instead of minus sign |
||
27 | * |
||
28 | * @var bool |
||
29 | */ |
||
30 | protected $useCardinalLetters; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | * |
||
35 | * @psalm-suppress PropertyNotSetInConstructor |
||
36 | */ |
||
37 | protected $unitType; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $digits = 3; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $decimalPoint = '.'; |
||
48 | |||
49 | /** |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $units = [ |
||
53 | 'UTF-8' => [ |
||
54 | 'deg' => '°', |
||
55 | 'min' => '′', |
||
56 | ], |
||
57 | 'ASCII' => [ |
||
58 | 'deg' => '°', |
||
59 | 'min' => '\'', |
||
60 | ], |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * @param string $separator |
||
65 | */ |
||
66 | public function __construct(string $separator = ' ') |
||
67 | { |
||
68 | $this->separator = $separator; |
||
69 | $this->useCardinalLetters = false; |
||
70 | |||
71 | $this->setUnits(static::UNITS_UTF8); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Sets the separator between latitude and longitude values |
||
76 | * |
||
77 | * @param string $separator |
||
78 | * |
||
79 | * @return DecimalMinutes |
||
80 | */ |
||
81 | public function setSeparator(string $separator): DecimalMinutes |
||
82 | { |
||
83 | $this->separator = $separator; |
||
84 | |||
85 | return $this; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param bool $value |
||
90 | * |
||
91 | * @return DecimalMinutes |
||
92 | */ |
||
93 | public function useCardinalLetters(bool $value): DecimalMinutes |
||
94 | { |
||
95 | $this->useCardinalLetters = $value; |
||
96 | |||
97 | return $this; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param string $type |
||
102 | * |
||
103 | * @return DecimalMinutes |
||
104 | * @throws \InvalidArgumentException |
||
105 | */ |
||
106 | public function setUnits(string $type): DecimalMinutes |
||
107 | { |
||
108 | if (! array_key_exists($type, $this->units)) { |
||
109 | throw new InvalidArgumentException('Invalid unit type'); |
||
110 | } |
||
111 | |||
112 | $this->unitType = $type; |
||
113 | |||
114 | return $this; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | public function getUnitType(): string |
||
121 | { |
||
122 | return $this->unitType; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param int $digits |
||
127 | * |
||
128 | * @return DecimalMinutes |
||
129 | */ |
||
130 | public function setDigits(int $digits): DecimalMinutes |
||
136 | |||
137 | /** |
||
138 | * @param string $decimalPoint |
||
139 | * |
||
140 | * @return DecimalMinutes |
||
141 | */ |
||
142 | public function setDecimalPoint(string $decimalPoint): DecimalMinutes |
||
148 | |||
149 | /** |
||
150 | * @param Coordinate $coordinate |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | public function format(Coordinate $coordinate): string |
||
188 | |||
189 | /** |
||
190 | * @param float $lat |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | protected function getLatPrefix(float $lat): string |
||
202 | |||
203 | /** |
||
204 | * @param float $lng |
||
205 | * |
||
206 | * @return string |
||
207 | */ |
||
208 | protected function getLngPrefix(float $lng): string |
||
216 | |||
217 | /** |
||
218 | * @param float $lat |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | protected function getLatSuffix(float $lat): string |
||
234 | |||
235 | /** |
||
236 | * @param float $lng |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | protected function getLngSuffix(float $lng): string |
||
252 | } |
||
253 |