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