Intl.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib\StringWrapper;
  10. use Zend\Stdlib\Exception;
  11. class Intl extends AbstractStringWrapper
  12. {
  13. /**
  14. * List of supported character sets (upper case)
  15. *
  16. * @var string[]
  17. */
  18. protected static $encodings = array('UTF-8');
  19. /**
  20. * Get a list of supported character encodings
  21. *
  22. * @return string[]
  23. */
  24. public static function getSupportedEncodings()
  25. {
  26. return static::$encodings;
  27. }
  28. /**
  29. * Constructor
  30. *
  31. * @throws Exception\ExtensionNotLoadedException
  32. */
  33. public function __construct()
  34. {
  35. if (!extension_loaded('intl')) {
  36. throw new Exception\ExtensionNotLoadedException(
  37. 'PHP extension "intl" is required for this wrapper'
  38. );
  39. }
  40. }
  41. /**
  42. * Returns the length of the given string
  43. *
  44. * @param string $str
  45. * @return int|false
  46. */
  47. public function strlen($str)
  48. {
  49. return grapheme_strlen($str);
  50. }
  51. /**
  52. * Returns the portion of string specified by the start and length parameters
  53. *
  54. * @param string $str
  55. * @param int $offset
  56. * @param int|null $length
  57. * @return string|false
  58. */
  59. public function substr($str, $offset = 0, $length = null)
  60. {
  61. // Due fix of PHP #62759 The third argument returns an empty string if is 0 or null.
  62. if ($length !== null) {
  63. return grapheme_substr($str, $offset, $length);
  64. }
  65. return grapheme_substr($str, $offset);
  66. }
  67. /**
  68. * Find the position of the first occurrence of a substring in a string
  69. *
  70. * @param string $haystack
  71. * @param string $needle
  72. * @param int $offset
  73. * @return int|false
  74. */
  75. public function strpos($haystack, $needle, $offset = 0)
  76. {
  77. return grapheme_strpos($haystack, $needle, $offset);
  78. }
  79. }