Intl.php 2.0 KB

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