Completed
Pull Request — master (#35)
by
unknown
14:26
created

CoordinateFactory::mergeSecondsToMinutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Coordinate Factory
6
 *
7
 * @author   Marcus Jaschen <[email protected]>
8
 * @license  https://opensource.org/licenses/MIT
9
 * @link     https://github.com/mjaschen/phpgeo
10
 */
11
12
namespace Location\Factory;
13
14
use Location\Coordinate;
15
use Location\Ellipsoid;
16
17
/**
18
 * Coordinate Factory
19
 *
20
 * @author   Marcus Jaschen <[email protected]>
21
 * @license  https://opensource.org/licenses/MIT
22
 * @link     https://github.com/mjaschen/phpgeo
23
 */
24
class CoordinateFactory implements GeometryFactoryInterface
25
{
26
    /**
27
     * Creates a Coordinate instance from the given string.
28
     *
29
     * The string is parsed by a regular expression for a known
30
     * format of geographical coordinates.
31
     *
32
     * @param string $string formatted geographical coordinate
33
     * @param \Location\Ellipsoid $ellipsoid
34
     *
35
     * @return Coordinate
36
     * @throws \InvalidArgumentException
37
     */
38
    public static function fromString(string $string, Ellipsoid $ellipsoid = null): Coordinate
39
    {
40
        $string = self::mergeSecondsToMinutes($string);
41
42
        $result = self::parseDecimalMinutesWithoutCardinalLetters($string, $ellipsoid);
0 ignored issues
show
Bug introduced by
It seems like $string defined by self::mergeSecondsToMinutes($string) on line 40 can also be of type array<integer,string> or null; however, Location\Factory\Coordin...ithoutCardinalLetters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
43
44
        if ($result instanceof Coordinate) {
45
            return $result;
46
        }
47
48
        $result = self::parseDecimalMinutesWithCardinalLetters($string, $ellipsoid);
0 ignored issues
show
Bug introduced by
It seems like $string defined by self::mergeSecondsToMinutes($string) on line 40 can also be of type array<integer,string> or null; however, Location\Factory\Coordin...esWithCardinalLetters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
49
50
        if ($result instanceof Coordinate) {
51
            return $result;
52
        }
53
54
        $result = self::parseDecimalDegreesWithoutCardinalLetters($string, $ellipsoid);
0 ignored issues
show
Bug introduced by
It seems like $string defined by self::mergeSecondsToMinutes($string) on line 40 can also be of type array<integer,string> or null; however, Location\Factory\Coordin...ithoutCardinalLetters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
56
        if ($result instanceof Coordinate) {
57
            return $result;
58
        }
59
60
        $result = self::parseDecimalDegreesWithCardinalLetters($string, $ellipsoid);
0 ignored issues
show
Bug introduced by
It seems like $string defined by self::mergeSecondsToMinutes($string) on line 40 can also be of type array<integer,string> or null; however, Location\Factory\Coordin...esWithCardinalLetters() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
62
        if ($result instanceof Coordinate) {
63
            return $result;
64
        }
65
66
        throw new \InvalidArgumentException('Format of coordinates was not recognized');
67
    }
68
69
    /**
70
     * @param $string
71
     * @param $ellipsoid
72
     *
73
     * @return Coordinate|null
74
     * @throws \InvalidArgumentException
75
     */
76
    private static function parseDecimalMinutesWithoutCardinalLetters(string $string, Ellipsoid $ellipsoid = null)
77
    {
78
        // Decimal minutes without cardinal letters, e. g. "52 12.345, 13 23.456",
79
        // "52° 12.345, 13° 23.456", "52° 12.345′, 13° 23.456′", "52 12.345 N, 13 23.456 E",
80
        // "N52° 12.345′ E13° 23.456′"
81
        if (preg_match('/(-?\d{1,2})°?\s+(\d{1,2}\.?\d*)[\'′]?[, ]\s*(-?\d{1,3})°?\s+(\d{1,2}\.?\d*)[\'′]?/ui', $string, $match)) {
82
            $latitude = $match[1] >= 0 ? $match[1] + $match[2] / 60 : $match[1] - $match[2] / 60;
83
            $longitude = $match[3] >= 0 ? $match[3] + $match[4] / 60 : $match[3] - $match[4] / 60;
84
85
            return new Coordinate((float)$latitude, (float)$longitude, $ellipsoid);
86
        }
87
88
        return null;
89
    }
90
91
    /**
92
     * @param $string
93
     * @param $ellipsoid
94
     *
95
     * @return Coordinate|null
96
     * @throws \InvalidArgumentException
97
     */
98
    private static function parseDecimalMinutesWithCardinalLetters(string $string, Ellipsoid $ellipsoid = null)
99
    {
100
        // Decimal minutes with cardinal letters, e. g. "52 12.345, 13 23.456",
101
        // "52° 12.345, 13° 23.456", "52° 12.345′, 13° 23.456′", "52 12.345 N, 13 23.456 E",
102
        // "N52° 12.345′ E13° 23.456′"
103
        if (preg_match('/([NS]?\s*)(\d{1,2})°?\s+(\d{1,2}\.?\d*)[\'′]?(\s*[NS]?)[, ]\s*([EW]?\s*)(\d{1,3})°?\s+(\d{1,2}\.?\d*)[\'′]?(\s*[EW]?)/ui', $string, $match)) {
104
            $latitude = $match[2] + $match[3] / 60;
105 View Code Duplication
            if (strtoupper(trim($match[1])) === 'S' || strtoupper(trim($match[4])) === 'S') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
                $latitude = - $latitude;
107
            }
108
            $longitude = $match[6] + $match[7] / 60;
109 View Code Duplication
            if (strtoupper(trim($match[5])) === 'W' || strtoupper(trim($match[8])) === 'W') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
                $longitude = - $longitude;
111
            }
112
113
            return new Coordinate((float)$latitude, (float)$longitude, $ellipsoid);
114
        }
115
116
        return null;
117
    }
118
119
    /**
120
     * @param $string
121
     * @param $ellipsoid
122
     *
123
     * @return Coordinate|null
124
     * @throws \InvalidArgumentException
125
     */
126
    private static function parseDecimalDegreesWithoutCardinalLetters(string $string, Ellipsoid $ellipsoid = null)
127
    {
128
        // The most simple format: decimal degrees without cardinal letters,
129
        // e. g. "52.5, 13.5" or "53.25732 14.24984"
130
        if (preg_match('/(-?\d{1,2}\.?\d*)°?[, ]\s*(-?\d{1,3}\.?\d*)°?/u', $string, $match)) {
131
            return new Coordinate((float)$match[1], (float)$match[2], $ellipsoid);
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     * @param $string
139
     * @param $ellipsoid
140
     *
141
     * @return Coordinate|null
142
     * @throws \InvalidArgumentException
143
     */
144
    private static function parseDecimalDegreesWithCardinalLetters(string $string, Ellipsoid $ellipsoid = null)
145
    {
146
        // Decimal degrees with cardinal letters, e. g. "N52.5, E13.5",
147
        // "40.2S, 135.3485W", or "56.234°N, 157.245°W"
148
        if (preg_match('/([NS]?\s*)(\d{1,2}\.?\d*)°?(\s*[NS]?)[, ]\s*([EW]?\s*)(\d{1,3}\.?\d*)°?(\s*[EW]?)/ui', $string, $match)) {
149
            $latitude = $match[2];
150 View Code Duplication
            if (strtoupper(trim($match[1])) === 'S' || strtoupper(trim($match[3])) === 'S') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
                $latitude = - $latitude;
152
            }
153
            $longitude = $match[5];
154 View Code Duplication
            if (strtoupper(trim($match[4])) === 'W' || strtoupper(trim($match[6])) === 'W') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
                $longitude = - $longitude;
156
            }
157
158
            return new Coordinate((float)$latitude, (float)$longitude, $ellipsoid);
159
        }
160
161
        return null;
162
    }
163
164
    /**
165
     * @param string $string
166
     * @return null|string|string[]
167
     */
168
    private static function mergeSecondsToMinutes(string $string)
169
    {
170
        return preg_replace_callback('/(\d+)(°|\s)\s*(\d+)(\'|′|\s)(\s*([0-9\.]*))("|\'\'|′′)?/', function (array $matches) {
171
            return sprintf('%d %d.%s', $matches[1], $matches[3], substr(sprintf('%f', floatval($matches[6]) / 60), 2));
172
        }, $string);
173
    }
174
}
175