PageRenderTime 20ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezi18n/classes/ezmbstringmapper.php

https://github.com/zerustech/ezpublish
PHP | 190 lines | 118 code | 20 blank | 52 comment | 10 complexity | 28033d55bc41d3836450b0f93106f048 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZMBStringMapper class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZMBStringMapper ezmbstringmapper.php
  12. \ingroup eZI18N
  13. \brief The class eZMBStringMapper does
  14. The mbstring extension supports the following charset:
  15. UCS-4, UCS-4BE, UCS-4LE, UCS-2, UCS-2BE, UCS-2LE, UTF-32, UTF-32BE, UTF-32LE, UCS-2LE, UTF-16,
  16. UTF-16BE, UTF-16LE, UTF-8, UTF-7, ASCII, EUC-JP, SJIS, eucJP-win, SJIS-win, ISO-2022-JP, JIS,
  17. ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8,
  18. ISO-8859-9, ISO-8859-10, ISO-8859-13, ISO-8859-14, ISO-8859-15, byte2be, byte2le, byte4be,
  19. byte4le, BASE64, 7bit, 8bit and UTF7-IMAP.
  20. */
  21. class eZMBStringMapper
  22. {
  23. /**
  24. * Constructor
  25. *
  26. * @param string $input_charset_code
  27. * @param string $output_charset_code
  28. */
  29. public function __construct( $input_charset_code, $output_charset_code )
  30. {
  31. $this->RequestedInputCharsetCode = $input_charset_code;
  32. $this->InputCharsetCode = eZCharsetInfo::realCharsetCode( $input_charset_code );
  33. $this->RequestedOutputCharsetCode = $output_charset_code;
  34. $this->OutputCharsetCode = eZCharsetInfo::realCharsetCode( $output_charset_code );
  35. $this->Valid = false;
  36. if ( !$this->isCharsetSupported( $input_charset_code ) )
  37. {
  38. eZDebug::writeError( "Input charset $input_charset_code not supported", "eZMBStringMapper" );
  39. }
  40. else if ( !$this->isCharsetSupported( $output_charset_code ) )
  41. {
  42. eZDebug::writeError( "Output charset $output_charset_code not supported", "eZMBStringMapper" );
  43. }
  44. else if ( $this->hasMBStringExtension() )
  45. $this->Valid = true;
  46. else
  47. eZDebug::writeError( "No mbstring functions available", "eZMBStringMapper" );
  48. }
  49. /*!
  50. \static
  51. \note This function is duplicated in eZTextCodec::eZTextCodec(), remember to update both places.
  52. */
  53. static function &charsetList()
  54. {
  55. $charsets =& $GLOBALS["eZMBCharsetList"];
  56. if ( !is_array( $charsets ) )
  57. {
  58. $charsetList = array( "ucs-4", "ucs-4be", "ucs-4le", "ucs-2", "ucs-2be", "ucs-2le", "utf-32", "utf-32be", "utf-32le", "utf-16",
  59. "utf-16be", "utf-16le", "utf-8", "utf-7", "ascii", "euc-jp", "sjis", "eucjp-win", "sjis-win", "iso-2022-jp", "jis",
  60. "iso-8859-1", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8",
  61. "iso-8859-9", "iso-8859-10", "iso-8859-13", "iso-8859-14", "iso-8859-15", "byte2be", "byte2le", "byte4be",
  62. "byte4le", "base64", "7bit", "8bit", "utf7-imap" );
  63. $charsets = array();
  64. foreach ( $charsetList as $charset )
  65. {
  66. $charsets[$charset] = $charset;
  67. }
  68. }
  69. return $charsets;
  70. }
  71. /*!
  72. \static
  73. \return \c true if the mbstring can be used.
  74. \note The following function must be present for the function to return \c true.
  75. mb_convert_encoding
  76. mb_substitute_character
  77. mb_strcut
  78. mb_strlen
  79. mb_strpos
  80. mb_strrpos
  81. mb_strwidth
  82. mb_substr
  83. \note This function is duplicated in eZTextCodec::eZTextCodec(), remember to update both places.
  84. */
  85. static function hasMBStringExtension()
  86. {
  87. return ( function_exists( "mb_convert_encoding" ) and
  88. function_exists( "mb_substitute_character" ) and
  89. function_exists( "mb_strcut" ) and
  90. function_exists( "mb_strlen" ) and
  91. function_exists( "mb_strpos" ) and
  92. function_exists( "mb_strrpos" ) and
  93. function_exists( "mb_strwidth" ) and
  94. function_exists( "mb_substr" ) );
  95. }
  96. function inputCharsetCode()
  97. {
  98. return $this->InputCharsetCode;
  99. }
  100. function outputCharsetCode()
  101. {
  102. return $this->OutputCharsetCode;
  103. }
  104. function requestedInputCharsetCode()
  105. {
  106. return $this->RequestedInputCharsetCode;
  107. }
  108. function requestedOutputCharsetCode()
  109. {
  110. return $this->RequestedOutputCharsetCode;
  111. }
  112. function isCharsetSupported( $charset_code )
  113. {
  114. $charset_code = eZCharsetInfo::realCharsetCode( $charset_code );
  115. return in_array( $charset_code, eZMBStringMapper::charsetList() );
  116. }
  117. function substituteCharacter()
  118. {
  119. if ( !$this->Valid )
  120. return null;
  121. return mb_substitute_character();
  122. }
  123. function setSubstituteCharacter( $char )
  124. {
  125. if ( $this->Valid )
  126. mb_substitute_character( $char );
  127. }
  128. function convertString( $str )
  129. {
  130. if ( !$this->Valid )
  131. return $str;
  132. return mb_convert_encoding( $str, $this->OutputCharsetCode, $this->InputCharsetCode );
  133. }
  134. function strlen( $str )
  135. {
  136. return mb_strlen( $str, $this->InputCharsetCode );
  137. }
  138. function strpos( $haystack, $needle, $offset = 0 )
  139. {
  140. return mb_strpos( $haystack, $needle, $offset, $this->InputCharsetCode );
  141. }
  142. function strrpos( $haystack, $needle )
  143. {
  144. return mb_strrpos( $haystack, $needle, $this->InputCharsetCode );
  145. }
  146. function substr( $str, $start, $length )
  147. {
  148. return mb_substr( $str, $start, $length, $this->InputCharsetCode );
  149. }
  150. /**
  151. * Returns a shared instance of the eZMBStringMapper pr the $input_charset_code
  152. * and $output_charset_code params.
  153. *
  154. * @param string $input_charset_code
  155. * @param string $output_charset_code
  156. * @return eZMBStringMapper
  157. */
  158. static function instance( $input_charset_code, $output_charset_code )
  159. {
  160. $globalsKey = "eZMBStringMapper-$input_charset_code-$output_charset_code";
  161. if ( !isset( $GLOBALS[$globalsKey] ) ||
  162. !( $GLOBALS[$globalsKey] instanceof eZMBStringMapper ) )
  163. {
  164. $GLOBALS[$globalsKey] = new eZMBStringMapper( $input_charset_code, $output_charset_code );
  165. }
  166. return $GLOBALS[$globalsKey];
  167. }
  168. }
  169. ?>