Icu.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Intl\Icu;
  11. /**
  12. * Provides fake static versions of the global functions in the intl extension.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. *
  16. * @internal
  17. */
  18. abstract class Icu
  19. {
  20. /**
  21. * Indicates that no error occurred.
  22. */
  23. public const U_ZERO_ERROR = 0;
  24. /**
  25. * Indicates that an invalid argument was passed.
  26. */
  27. public const U_ILLEGAL_ARGUMENT_ERROR = 1;
  28. /**
  29. * Indicates that the parse() operation failed.
  30. */
  31. public const U_PARSE_ERROR = 9;
  32. /**
  33. * All known error codes.
  34. */
  35. private static $errorCodes = [
  36. self::U_ZERO_ERROR => 'U_ZERO_ERROR',
  37. self::U_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR',
  38. self::U_PARSE_ERROR => 'U_PARSE_ERROR',
  39. ];
  40. /**
  41. * The error code of the last operation.
  42. */
  43. private static $errorCode = self::U_ZERO_ERROR;
  44. /**
  45. * The error code of the last operation.
  46. */
  47. private static $errorMessage = 'U_ZERO_ERROR';
  48. /**
  49. * Returns whether the error code indicates a failure.
  50. *
  51. * @param int $errorCode The error code returned by Icu::getErrorCode()
  52. */
  53. public static function isFailure(int $errorCode): bool
  54. {
  55. return isset(self::$errorCodes[$errorCode])
  56. && $errorCode > self::U_ZERO_ERROR;
  57. }
  58. /**
  59. * Returns the error code of the last operation.
  60. *
  61. * Returns Icu::U_ZERO_ERROR if no error occurred.
  62. *
  63. * @return int
  64. */
  65. public static function getErrorCode()
  66. {
  67. return self::$errorCode;
  68. }
  69. /**
  70. * Returns the error message of the last operation.
  71. *
  72. * Returns "U_ZERO_ERROR" if no error occurred.
  73. */
  74. public static function getErrorMessage(): string
  75. {
  76. return self::$errorMessage;
  77. }
  78. /**
  79. * Returns the symbolic name for a given error code.
  80. *
  81. * @param int $code The error code returned by Icu::getErrorCode()
  82. */
  83. public static function getErrorName(int $code): string
  84. {
  85. return self::$errorCodes[$code] ?? '[BOGUS UErrorCode]';
  86. }
  87. /**
  88. * Sets the current error.
  89. *
  90. * @param int $code One of the error constants in this class
  91. * @param string $message The ICU class error message
  92. *
  93. * @throws \InvalidArgumentException If the code is not one of the error constants in this class
  94. */
  95. public static function setError(int $code, string $message = '')
  96. {
  97. if (!isset(self::$errorCodes[$code])) {
  98. throw new \InvalidArgumentException(sprintf('No such error code: "%s".', $code));
  99. }
  100. self::$errorMessage = $message ? sprintf('%s: %s', $message, self::$errorCodes[$code]) : self::$errorCodes[$code];
  101. self::$errorCode = $code;
  102. }
  103. }