Assert.php 1.7 KB

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