StubIntl.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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\Component\Locale\Stub;
  11. /**
  12. * Provides fake static versions of the global functions in the intl extension
  13. *
  14. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  15. */
  16. abstract class StubIntl
  17. {
  18. /**
  19. * Indicates that no error occurred
  20. *
  21. * @var integer
  22. */
  23. const U_ZERO_ERROR = 0;
  24. /**
  25. * Indicates that an invalid argument was passed
  26. *
  27. * @var integer
  28. */
  29. const U_ILLEGAL_ARGUMENT_ERROR = 1;
  30. /**
  31. * Indicates that the parse() operation failed
  32. *
  33. * @var integer
  34. */
  35. const U_PARSE_ERROR = 9;
  36. /**
  37. * All known error codes
  38. *
  39. * @var array
  40. */
  41. private static $errorCodes = array(
  42. self::U_ZERO_ERROR => 'U_ZERO_ERROR',
  43. self::U_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR',
  44. self::U_PARSE_ERROR => 'U_PARSE_ERROR',
  45. );
  46. /**
  47. * The error code of the last operation
  48. *
  49. * @var integer
  50. */
  51. private static $errorCode = self::U_ZERO_ERROR;
  52. /**
  53. * The error code of the last operation
  54. *
  55. * @var integer
  56. */
  57. private static $errorMessage = 'U_ZERO_ERROR';
  58. /**
  59. * Returns whether the error code indicates a failure
  60. *
  61. * @param integer $errorCode The error code returned by StubIntl::getErrorCode()
  62. *
  63. * @return Boolean
  64. */
  65. static public function isFailure($errorCode)
  66. {
  67. return array_key_exists($errorCode, self::$errorCodes)
  68. && $errorCode > self::U_ZERO_ERROR;
  69. }
  70. /**
  71. * Returns the error code of the last operation
  72. *
  73. * Returns StubIntl::U_ZERO_ERROR if no error occurred.
  74. *
  75. * @return integer
  76. */
  77. static public function getErrorCode()
  78. {
  79. return self::$errorCode;
  80. }
  81. /**
  82. * Returns the error message of the last operation
  83. *
  84. * Returns "U_ZERO_ERROR" if no error occurred.
  85. *
  86. * @return string
  87. */
  88. static public function getErrorMessage()
  89. {
  90. return self::$errorMessage;
  91. }
  92. /**
  93. * Sets the current error
  94. *
  95. * @param integer $code One of the error constants in this class
  96. * @param string $message The ICU class error message
  97. *
  98. * @throws \InvalidArgumentException If the code is not one of the error constants in this class
  99. */
  100. static public function setError($code, $message = '')
  101. {
  102. if (!isset(self::$errorCodes[$code])) {
  103. throw new \InvalidArgumentException(sprintf('No such error code: "%s"', $code));
  104. }
  105. self::$errorMessage = $message ? sprintf('%s: %s', $message, self::$errorCodes[$code]) : self::$errorCodes[$code];
  106. self::$errorCode = $code;
  107. }
  108. }