PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/phpunit/behat/behat/src/Behat/Behat/Util/Transliterator.php

https://github.com/m4rkw/env
PHP | 427 lines | 274 code | 31 blank | 122 comment | 66 complexity | a9abeea2af322062f4a9d7cc1c6e4b35 MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause
  1. <?php
  2. namespace Behat\Behat\Util;
  3. /**
  4. * This is the part taken from Doctrine 1.2.3
  5. * Doctrine inflector has static methods for inflecting text
  6. *
  7. * The methods in these classes are from several different sources collected
  8. * across several different php projects and several different authors. The
  9. * original author names and emails are not known
  10. *
  11. * Uses 3rd party libraries and functions:
  12. * http://sourceforge.net/projects/phputf8
  13. *
  14. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  15. * @link www.doctrine-project.org
  16. * @since 1.0
  17. * @version $Revision: 3189 $
  18. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  19. * @author Jonathan H. Wage <jonwage@gmail.com>
  20. * @author <hsivonen@iki.fi>
  21. */
  22. class Transliterator
  23. {
  24. /**
  25. * Check if a string has utf7 characters in it
  26. *
  27. * By bmorel at ssi dot fr
  28. *
  29. * @param string $string
  30. * @return boolean $bool
  31. */
  32. public static function seemsUtf8($string)
  33. {
  34. for ($i = 0; $i < strlen($string); $i++) {
  35. if (ord($string[$i]) < 0x80) continue; # 0bbbbbbb
  36. elseif ((ord($string[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  37. elseif ((ord($string[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  38. elseif ((ord($string[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
  39. elseif ((ord($string[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
  40. elseif ((ord($string[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
  41. else return false; # Does not match any model
  42. for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
  43. if ((++$i == strlen($string)) || ((ord($string[$i]) & 0xC0) != 0x80))
  44. return false;
  45. }
  46. }
  47. return true;
  48. }
  49. /**
  50. * Remove any illegal characters, accents, etc.
  51. *
  52. * @param string $string String to unaccent
  53. * @return string $string Unaccented string
  54. */
  55. public static function unaccent($string)
  56. {
  57. if (!preg_match('/[\x80-\xff]/', $string)) {
  58. return $string;
  59. }
  60. if (self::seemsUtf8($string)) {
  61. $chars = array(
  62. // Decompositions for Latin-1 Supplement
  63. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  64. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  65. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  66. chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  67. chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  68. chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  69. chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  70. chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  71. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  72. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  73. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  74. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  75. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  76. chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  77. chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  78. chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  79. chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  80. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  81. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  82. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  83. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  84. chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  85. chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  86. chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  87. chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  88. chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  89. chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  90. chr(195).chr(191) => 'y',
  91. // Decompositions for Latin Extended-A
  92. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  93. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  94. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  95. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  96. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  97. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  98. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  99. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  100. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  101. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  102. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  103. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  104. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  105. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  106. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  107. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  108. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  109. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  110. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  111. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  112. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  113. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  114. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  115. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  116. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  117. chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  118. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  119. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  120. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  121. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  122. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  123. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  124. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  125. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  126. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  127. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  128. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  129. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  130. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  131. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  132. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  133. chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
  134. chr(197).chr(148) => 'R', chr(197).chr(149) => 'r',
  135. chr(197).chr(150) => 'R', chr(197).chr(151) => 'r',
  136. chr(197).chr(152) => 'R', chr(197).chr(153) => 'r',
  137. chr(197).chr(154) => 'S', chr(197).chr(155) => 's',
  138. chr(197).chr(156) => 'S', chr(197).chr(157) => 's',
  139. chr(197).chr(158) => 'S', chr(197).chr(159) => 's',
  140. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  141. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  142. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  143. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  144. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  145. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  146. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  147. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  148. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  149. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  150. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  151. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  152. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  153. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  154. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  155. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  156. // Euro Sign
  157. chr(226).chr(130).chr(172) => 'E',
  158. // GBP (Pound) Sign
  159. chr(194).chr(163) => '',
  160. 'Ä' => 'Ae', 'ä' => 'ae', 'Ü' => 'Ue', 'ü' => 'ue',
  161. 'Ö' => 'Oe', 'ö' => 'oe', 'ß' => 'ss',
  162. // Norwegian characters
  163. 'Å'=>'Aa','Æ'=>'Ae','Ø'=>'O','æ'=>'a','ø'=>'o','å'=>'aa'
  164. );
  165. $string = strtr($string, $chars);
  166. } else {
  167. // Assume ISO-8859-1 if not UTF-8
  168. $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
  169. .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
  170. .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
  171. .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
  172. .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
  173. .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
  174. .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
  175. .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
  176. .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
  177. .chr(252).chr(253).chr(255);
  178. $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
  179. $string = strtr($string, $chars['in'], $chars['out']);
  180. $doubleChars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
  181. $doubleChars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
  182. $string = str_replace($doubleChars['in'], $doubleChars['out'], $string);
  183. }
  184. return $string;
  185. }
  186. /**
  187. * US-ASCII transliterations of Unicode text
  188. * Ported Sean M. Burke's Text::Unidecode Perl module (He did all the hard work!)
  189. * Warning: you should only pass this well formed UTF-8!
  190. * Be aware it works by making a copy of the input string which it appends transliterated
  191. * characters to - it uses a PHP output buffer to do this - it means, memory use will increase,
  192. * requiring up to the same amount again as the input string
  193. *
  194. * @see http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm
  195. * @param string UTF-8 string to convert
  196. * @author <hsivonen@iki.fi>
  197. * @param string (default = ?) Character use if character unknown
  198. * @return string US-ASCII string
  199. */
  200. public static function utf8ToAscii($str, $unknown = '?')
  201. {
  202. static $UTF8_TO_ASCII;
  203. if (strlen($str) == 0) {
  204. return;
  205. }
  206. preg_match_all('/.{1}|[^\x00]{1,1}$/us', $str, $ar);
  207. $chars = $ar[0];
  208. foreach ($chars as $i => $c) {
  209. $ud = 0;
  210. if (ord($c{0})>=0 && ord($c{0})<=127) { continue; } // ASCII - next please
  211. if (ord($c{0})>=192 && ord($c{0})<=223) { $ord = (ord($c{0})-192)*64 + (ord($c{1})-128); }
  212. if (ord($c{0})>=224 && ord($c{0})<=239) { $ord = (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128); }
  213. if (ord($c{0})>=240 && ord($c{0})<=247) { $ord = (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128); }
  214. if (ord($c{0})>=248 && ord($c{0})<=251) { $ord = (ord($c{0})-248)*16777216 + (ord($c{1})-128)*262144 + (ord($c{2})-128)*4096 + (ord($c{3})-128)*64 + (ord($c{4})-128); }
  215. if (ord($c{0})>=252 && ord($c{0})<=253) { $ord = (ord($c{0})-252)*1073741824 + (ord($c{1})-128)*16777216 + (ord($c{2})-128)*262144 + (ord($c{3})-128)*4096 + (ord($c{4})-128)*64 + (ord($c{5})-128); }
  216. if (ord($c{0})>=254 && ord($c{0})<=255) { $chars{$i} = $unknown; continue; } //error
  217. $bank = $ord >> 8;
  218. if (!array_key_exists($bank, (array) $UTF8_TO_ASCII)) {
  219. $bankfile = __DIR__. '/data/'. sprintf("x%02x",$bank).'.php';
  220. if (file_exists($bankfile)) {
  221. include $bankfile;
  222. } else {
  223. $UTF8_TO_ASCII[$bank] = array();
  224. }
  225. }
  226. $newchar = $ord & 255;
  227. if (array_key_exists($newchar, $UTF8_TO_ASCII[$bank])) {
  228. $chars{$i} = $UTF8_TO_ASCII[$bank][$newchar];
  229. } else {
  230. $chars{$i} = $unknown;
  231. }
  232. }
  233. return implode('', $chars);
  234. }
  235. /**
  236. * Does not transliterate correctly eastern languages
  237. *
  238. * @param string $text
  239. * @param string $separator
  240. * @return string
  241. */
  242. public static function urlize($text, $separator = '-')
  243. {
  244. $text = self::unaccent($text);
  245. return self::postProcessText($text, $separator);
  246. }
  247. /**
  248. * Uses transliteration tables to convert any kind of utf8 character
  249. *
  250. * @param string $text
  251. * @param string $separator
  252. * @return string $text
  253. */
  254. public static function transliterate($text, $separator = '-')
  255. {
  256. if (preg_match('/[\x80-\xff]/', $text) && self::validUtf8($text)) {
  257. $text = self::utf8ToAscii($text);
  258. }
  259. return self::postProcessText($text, $separator);
  260. }
  261. /**
  262. * Tests a string as to whether it's valid UTF-8 and supported by the
  263. * Unicode standard
  264. * Note: this function has been modified to simple return true or false
  265. * @author <hsivonen@iki.fi>
  266. * @param string UTF-8 encoded string
  267. * @return boolean true if valid
  268. * @see http://hsivonen.iki.fi/php-utf8/
  269. */
  270. public static function validUtf8($str)
  271. {
  272. $mState = 0; // cached expected number of octets after the current octet
  273. // until the beginning of the next UTF8 character sequence
  274. $mUcs4 = 0; // cached Unicode character
  275. $mBytes = 1; // cached expected number of octets in the current sequence
  276. $len = strlen($str);
  277. for ($i = 0; $i < $len; $i++) {
  278. $in = ord($str{$i});
  279. if ($mState == 0) {
  280. // When mState is zero we expect either a US-ASCII character or a
  281. // multi-octet sequence.
  282. if (0 == (0x80 & ($in))) {
  283. // US-ASCII, pass straight through.
  284. $mBytes = 1;
  285. } elseif (0xC0 == (0xE0 & ($in))) {
  286. // First octet of 2 octet sequence
  287. $mUcs4 = ($in);
  288. $mUcs4 = ($mUcs4 & 0x1F) << 6;
  289. $mState = 1;
  290. $mBytes = 2;
  291. } elseif (0xE0 == (0xF0 & ($in))) {
  292. // First octet of 3 octet sequence
  293. $mUcs4 = ($in);
  294. $mUcs4 = ($mUcs4 & 0x0F) << 12;
  295. $mState = 2;
  296. $mBytes = 3;
  297. } elseif (0xF0 == (0xF8 & ($in))) {
  298. // First octet of 4 octet sequence
  299. $mUcs4 = ($in);
  300. $mUcs4 = ($mUcs4 & 0x07) << 18;
  301. $mState = 3;
  302. $mBytes = 4;
  303. } elseif (0xF8 == (0xFC & ($in))) {
  304. /* First octet of 5 octet sequence.
  305. *
  306. * This is illegal because the encoded codepoint must be either
  307. * (a) not the shortest form or
  308. * (b) outside the Unicode range of 0-0x10FFFF.
  309. * Rather than trying to resynchronize, we will carry on until the end
  310. * of the sequence and let the later error handling code catch it.
  311. */
  312. $mUcs4 = ($in);
  313. $mUcs4 = ($mUcs4 & 0x03) << 24;
  314. $mState = 4;
  315. $mBytes = 5;
  316. } elseif (0xFC == (0xFE & ($in))) {
  317. // First octet of 6 octet sequence, see comments for 5 octet sequence.
  318. $mUcs4 = ($in);
  319. $mUcs4 = ($mUcs4 & 1) << 30;
  320. $mState = 5;
  321. $mBytes = 6;
  322. } else {
  323. /* Current octet is neither in the US-ASCII range nor a legal first
  324. * octet of a multi-octet sequence.
  325. */
  326. return false;
  327. }
  328. } else {
  329. // When mState is non-zero, we expect a continuation of the multi-octet
  330. // sequence
  331. if (0x80 == (0xC0 & ($in))) {
  332. // Legal continuation.
  333. $shift = ($mState - 1) * 6;
  334. $tmp = $in;
  335. $tmp = ($tmp & 0x0000003F) << $shift;
  336. $mUcs4 |= $tmp;
  337. /**
  338. * End of the multi-octet sequence. mUcs4 now contains the final
  339. * Unicode codepoint to be output
  340. */
  341. if (0 == --$mState) {
  342. /*
  343. * Check for illegal sequences and codepoints.
  344. */
  345. // From Unicode 3.1, non-shortest form is illegal
  346. if (((2 == $mBytes) && ($mUcs4 < 0x0080)) ||
  347. ((3 == $mBytes) && ($mUcs4 < 0x0800)) ||
  348. ((4 == $mBytes) && ($mUcs4 < 0x10000)) ||
  349. (4 < $mBytes) ||
  350. // From Unicode 3.2, surrogate characters are illegal
  351. (($mUcs4 & 0xFFFFF800) == 0xD800) ||
  352. // Codepoints outside the Unicode range are illegal
  353. ($mUcs4 > 0x10FFFF)
  354. ) {
  355. return false;
  356. }
  357. //initialize UTF8 cache
  358. $mState = 0;
  359. $mUcs4 = 0;
  360. $mBytes = 1;
  361. }
  362. } else {
  363. /**
  364. *((0xC0 & (*in) != 0x80) && (mState != 0))
  365. * Incomplete multi-octet sequence.
  366. */
  367. return false;
  368. }
  369. }
  370. }
  371. return true;
  372. }
  373. /**
  374. * Cleans up the text and adds separator
  375. *
  376. * @param string $text
  377. * @param string $separator
  378. * @return string
  379. */
  380. private static function postProcessText($text, $separator)
  381. {
  382. if (function_exists('mb_strtolower')) {
  383. $text = mb_strtolower($text);
  384. } else {
  385. $text = strtolower($text);
  386. }
  387. // Remove all none word characters
  388. $text = preg_replace('/\W/', ' ', $text);
  389. // More stripping. Replace spaces with dashes
  390. $text = strtolower(preg_replace('/[^A-Z^a-z^0-9^\/]+/', $separator,
  391. preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
  392. preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
  393. preg_replace('/::/', '/', $text)))));
  394. return trim($text, $separator);
  395. }
  396. }