Intl.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\g11n\multibyte\adapter;
  9. /**
  10. * The `Intl` class is an adapter which uses certain string functions from
  11. * `ext/intl`. You will need to have the extension installed to use this adapter.
  12. *
  13. * Internally works with a fixed encoding of UTF-8. This means you can't use
  14. * this adapter for anything different than UTF-8 encoded strings. Silently
  15. * returns `null` or `false` when input string contains badly formed UTF-8
  16. * sequences.
  17. *
  18. * @link http://php.net/manual/en/book.intl.php
  19. */
  20. class Intl extends \lithium\core\Object {
  21. /**
  22. * Determines if this adapter is enabled by checking if the `intl` extension is loaded.
  23. *
  24. * @return boolean Returns `true` if enabled, otherwise `false`.
  25. */
  26. public static function enabled() {
  27. return extension_loaded('intl');
  28. }
  29. /**
  30. * Here used as a multibyte enabled equivalent of `strlen()`.
  31. *
  32. * @link http://php.net/manual/en/function.grapheme-strlen.php
  33. * @param string $string
  34. * @return integer|void
  35. */
  36. public function strlen($string) {
  37. return grapheme_strlen($string);
  38. }
  39. /**
  40. * Here used as a multibyte enabled equivalent of `strpos()`.
  41. *
  42. * @link http://php.net/manual/en/function.grapheme-strpos.php
  43. * @param string $haystack
  44. * @param string $needle
  45. * @param integer $offset
  46. * @return integer|boolean
  47. */
  48. public function strpos($haystack, $needle, $offset) {
  49. return grapheme_strpos($haystack, $needle, $offset);
  50. }
  51. /**
  52. * Here used as a multibyte enabled equivalent of `strrpos()`.
  53. *
  54. * @link http://php.net/manual/en/function.grapheme-strpos.php
  55. * @param string $haystack
  56. * @param string $needle
  57. * @return integer|boolean
  58. */
  59. public function strrpos($haystack, $needle) {
  60. return grapheme_strrpos($haystack, $needle);
  61. }
  62. /**
  63. * Here used as a multibyte enabled equivalent of `substr()`.
  64. *
  65. * @link http://php.net/manual/en/function.grapheme-substr.php
  66. * @param string $string
  67. * @param integer $start
  68. * @param integer $length
  69. * @return string|boolean
  70. */
  71. public function substr($string, $start, $length) {
  72. return grapheme_substr($string, $start, $length);
  73. }
  74. }
  75. ?>