|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Geocoder package. |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Geocoder; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Exception\InvalidArgument; |
|
16
|
|
|
|
|
17
|
|
|
class Assert |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param float $value |
|
21
|
|
|
*/ |
|
22
|
17 |
|
public static function latitude($value, string $message = '') |
|
23
|
|
|
{ |
|
24
|
17 |
|
self::float($value, $message); |
|
25
|
17 |
|
if ($value < -90 || $value > 90) { |
|
26
|
|
|
throw new InvalidArgument(sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value)); |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param float $value |
|
32
|
|
|
*/ |
|
33
|
17 |
|
public static function longitude($value, string $message = '') |
|
34
|
|
|
{ |
|
35
|
17 |
|
self::float($value, $message); |
|
36
|
17 |
|
if ($value < -180 || $value > 180) { |
|
37
|
|
|
throw new InvalidArgument(sprintf($message ?: 'Longitude should be between -180 and 180. Got: %s', $value)); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
17 |
|
public static function notNull($value, string $message = '') |
|
42
|
|
|
{ |
|
43
|
17 |
|
if (null === $value) { |
|
44
|
|
|
throw new InvalidArgument(sprintf($message ?: 'Value cannot be null')); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
private static function typeToString($value): string |
|
49
|
|
|
{ |
|
50
|
|
|
return is_object($value) ? get_class($value) : gettype($value); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
17 |
|
private static function float($value, string $message) |
|
54
|
|
|
{ |
|
55
|
17 |
|
if (!is_float($value)) { |
|
56
|
|
|
throw new InvalidArgument(sprintf($message ?: 'Expected a float. Got: %s', self::typeToString($value))); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|