1 | <?php |
||
16 | class FloatCoordinateParser extends LatLongParserBase { |
||
17 | |||
18 | public const FORMAT_NAME = 'float-coordinate'; |
||
19 | |||
20 | /** |
||
21 | * @see LatLongParserBase::getParsedCoordinate |
||
22 | * |
||
23 | * @param string $coordinateSegment |
||
24 | * |
||
25 | * @return float |
||
26 | */ |
||
27 | 18 | protected function getParsedCoordinate( string $coordinateSegment ): float { |
|
30 | |||
31 | /** |
||
32 | * @see LatLongParserBase::areValidCoordinates |
||
33 | * |
||
34 | * @param string[] $normalizedCoordinateSegments |
||
35 | * |
||
36 | * @return bool |
||
37 | */ |
||
38 | 18 | protected function areValidCoordinates( array $normalizedCoordinateSegments ): bool { |
|
39 | // TODO: Implement localized decimal separator. |
||
40 | 18 | $baseRegExp = '\d{1,3}(\.\d{1,20})?'; |
|
41 | |||
42 | // Cache whether the coordinates are specified in directional format (a mixture of |
||
43 | // directional and non-directional is regarded invalid). |
||
44 | 18 | $directional = false; |
|
45 | |||
46 | 18 | $match = false; |
|
47 | |||
48 | 18 | foreach ( $normalizedCoordinateSegments as $i => $segment ) { |
|
49 | 18 | $segment = str_replace( ' ', '', $segment ); |
|
50 | |||
51 | $direction = '(' |
||
52 | 18 | . $this->getOption( self::OPT_NORTH_SYMBOL ) . '|' |
|
53 | 18 | . $this->getOption( self::OPT_SOUTH_SYMBOL ) . ')'; |
|
54 | |||
55 | 18 | if ( $i === 1 ) { |
|
56 | $direction = '(' |
||
57 | 18 | . $this->getOption( self::OPT_EAST_SYMBOL ) . '|' |
|
58 | 18 | . $this->getOption( self::OPT_WEST_SYMBOL ) . ')'; |
|
59 | } |
||
60 | |||
61 | 18 | $match = preg_match( |
|
62 | 18 | '/^(' . $baseRegExp . $direction . '|' . $direction . $baseRegExp . ')$/i', |
|
63 | 18 | $segment |
|
64 | ); |
||
65 | |||
66 | 18 | if ( $directional && !$match ) { |
|
67 | // Latitude is directional, longitude not. |
||
68 | break; |
||
69 | 18 | } elseif ( $match ) { |
|
70 | 10 | continue; |
|
71 | } |
||
72 | |||
73 | 8 | $match = preg_match( '/^(-)?' . $baseRegExp . '$/i', $segment ); |
|
74 | |||
75 | 8 | if ( !$match ) { |
|
76 | // Does neither match directional nor non-directional. |
||
77 | break; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | 18 | return ( 1 === $match ); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @see LatLongParserBase::splitString |
||
86 | * |
||
87 | * @param string $normalizedCoordinateString |
||
88 | * |
||
89 | * @throws ParseException if unable to split input string into two segments |
||
90 | * @return string[] |
||
91 | */ |
||
92 | 20 | protected function splitString( string $normalizedCoordinateString ): array { |
|
135 | |||
136 | } |
||
137 |