NumberFormatException.php 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. declare(strict_types=1);
  3. namespace Brick\Math\Exception;
  4. /**
  5. * Exception thrown when attempting to create a number from a string with an invalid format.
  6. */
  7. class NumberFormatException extends MathException
  8. {
  9. public static function invalidFormat(string $value) : self
  10. {
  11. return new self(\sprintf(
  12. 'The given value "%s" does not represent a valid number.',
  13. $value,
  14. ));
  15. }
  16. /**
  17. * @param string $char The failing character.
  18. *
  19. * @psalm-pure
  20. */
  21. public static function charNotInAlphabet(string $char) : self
  22. {
  23. $ord = \ord($char);
  24. if ($ord < 32 || $ord > 126) {
  25. $char = \strtoupper(\dechex($ord));
  26. if ($ord < 10) {
  27. $char = '0' . $char;
  28. }
  29. } else {
  30. $char = '"' . $char . '"';
  31. }
  32. return new self(\sprintf('Char %s is not a valid character in the given alphabet.', $char));
  33. }
  34. }