1 | <?php |
||
15 | class DMS 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 array |
||
41 | */ |
||
42 | protected $units = [ |
||
43 | 'UTF-8' => [ |
||
44 | 'deg' => '°', |
||
45 | 'min' => '′', |
||
46 | 'sec' => '″', |
||
47 | ], |
||
48 | 'ASCII' => [ |
||
49 | 'deg' => '°', |
||
50 | 'min' => '\'', |
||
51 | 'sec' => '"', |
||
52 | ], |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @param string $separator |
||
57 | * |
||
58 | * @throws InvalidArgumentException |
||
59 | */ |
||
60 | public function __construct(string $separator = ' ') |
||
61 | { |
||
62 | $this->separator = $separator; |
||
63 | $this->useCardinalLetters = false; |
||
64 | $this->setUnits(static::UNITS_UTF8); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Sets the separator between latitude and longitude values |
||
69 | * |
||
70 | * @param string $separator |
||
71 | * |
||
72 | * @return DMS |
||
73 | */ |
||
74 | public function setSeparator(string $separator): DMS |
||
75 | { |
||
76 | $this->separator = $separator; |
||
77 | |||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param bool $value |
||
83 | * |
||
84 | * @return DMS |
||
85 | */ |
||
86 | public function useCardinalLetters(bool $value): DMS |
||
87 | { |
||
88 | $this->useCardinalLetters = $value; |
||
89 | |||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param string $type |
||
95 | * |
||
96 | * @return DMS |
||
97 | * @throws InvalidArgumentException |
||
98 | */ |
||
99 | public function setUnits(string $type): DMS |
||
100 | { |
||
101 | if (! array_key_exists($type, $this->units)) { |
||
102 | throw new InvalidArgumentException('Invalid unit type'); |
||
103 | } |
||
104 | |||
105 | $this->unitType = $type; |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return string |
||
112 | */ |
||
113 | public function getUnitType(): string |
||
114 | { |
||
115 | return $this->unitType; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param Coordinate $coordinate |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function format(Coordinate $coordinate): string |
||
165 | |||
166 | /** |
||
167 | * @param float $lat |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function getLatPrefix(float $lat): string |
||
179 | |||
180 | /** |
||
181 | * @param float $lng |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | protected function getLngPrefix(float $lng): string |
||
193 | |||
194 | /** |
||
195 | * @param float $lat |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function getLatSuffix(float $lat): string |
||
211 | |||
212 | /** |
||
213 | * @param float $lng |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | protected function getLngSuffix(float $lng): string |
||
229 | } |
||
230 |