PageRenderTime 198ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Centurion/Inflector.php

http://github.com/centurion-project/Centurion
PHP | 543 lines | 383 code | 59 blank | 101 comment | 38 complexity | 6b597cecfac313de74d006c79138f0eb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Centurion
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@centurion-project.org so we can send you a copy immediately.
  12. *
  13. * @category Centurion
  14. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  15. * @license http://centurion-project.org/license/new-bsd New BSD License
  16. * @version $Id$
  17. */
  18. /**
  19. * Inflector for Zend_Db, inspired by Doctrine_Inflector.
  20. *
  21. * This classe uses static methods for inflecting text.
  22. *
  23. * @category Centurion
  24. * @copyright Copyright (c) 2008-2011 Octave & Octave (http://www.octaveoctave.com)
  25. * @license http://centurion-project.org/license/new-bsd New BSD License
  26. * @author Florent Messa <florent.messa@gmail.com>
  27. */
  28. class Centurion_Inflector
  29. {
  30. protected static $_characterMap = array(
  31. array(
  32. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' =>
  33. 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
  34. 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' =>
  35. 'O', 'Ő' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
  36. 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' =>'a', 'á' =>'a', 'â' => 'a', 'ã' => 'a', 'ä' =>
  37. 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
  38. 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
  39. 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u',
  40. 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y'
  41. ),
  42. array(
  43. '©' =>'(c)'
  44. ),
  45. array(
  46. 'α' =>'a', 'β' =>'b', 'γ' =>'g', 'δ' =>'d', 'ε' =>'e', 'ζ' =>'z', 'η' =>'h', 'θ' =>'8',
  47. 'ι' =>'i', 'κ' =>'k', 'λ' =>'l', 'μ' =>'m', 'ν' =>'n', 'ξ' =>'3', 'ο' =>'o', 'π' =>'p',
  48. 'ρ' =>'r', 'σ' =>'s', 'τ' =>'t', 'υ' =>'y', 'φ' =>'f', 'χ' =>'x', 'ψ' =>'ps', 'ω' =>'w',
  49. 'ά' =>'a', 'έ' =>'e', 'ί' =>'i', 'ό' =>'o', 'ύ' =>'y', 'ή' =>'h', 'ώ' =>'w', 'ς' =>'s',
  50. 'ϊ' =>'i', 'ΰ' =>'y', 'ϋ' =>'y', 'ΐ' =>'i',
  51. 'Α' =>'A', 'Β' =>'B', 'Γ' =>'G', 'Δ' =>'D', 'Ε' =>'E', 'Ζ' =>'Z', 'Η' =>'H', 'Θ' =>'8',
  52. 'Ι' =>'I', 'Κ' =>'K', 'Λ' =>'L', 'Μ' =>'M', 'Ν' =>'N', 'Ξ' =>'3', 'Ο' =>'O', 'Π' =>'P',
  53. 'Ρ' =>'R', 'Σ' =>'S', 'Τ' =>'T', 'Υ' =>'Y', 'Φ' =>'F', 'Χ' =>'X', 'Ψ' =>'PS', 'Ω' =>'W',
  54. 'Ά' =>'A', 'Έ' =>'E', 'Ί' =>'I', 'Ό' =>'O', 'Ύ' =>'Y', 'Ή' =>'H', 'Ώ' =>'W', 'Ϊ' =>'I',
  55. 'Ϋ' =>'Y'
  56. ),
  57. array(
  58. 'ş' =>'s', 'Ş' =>'S', 'ı' =>'i', 'İ' =>'I', 'ç' =>'c', 'Ç' =>'C', 'ü' =>'u', 'Ü' =>'U',
  59. 'ö' =>'o', 'Ö' =>'O', 'ğ' =>'g', 'Ğ' =>'G'
  60. ),
  61. array(
  62. 'а' =>'a', 'б' =>'b', 'в' =>'v', 'г' =>'g', 'д' =>'d', 'е' =>'e', 'ё' =>'yo', 'ж' =>'zh',
  63. 'з' =>'z', 'и' =>'i', 'й' =>'j', 'к' =>'k', 'л' =>'l', 'м' =>'m', 'н' =>'n', 'о' =>'o',
  64. 'п' =>'p', 'р' =>'r', 'с' =>'s', 'т' =>'t', 'у' =>'u', 'ф' =>'f', 'х' =>'h', 'ц' =>'c',
  65. 'ч' =>'ch', 'ш' =>'sh', 'щ' =>'sh', 'ъ' =>'', 'ы' =>'y', 'ь' =>'', 'э' =>'e', 'ю' =>'yu',
  66. 'я' =>'ya',
  67. 'А' =>'A', 'Б' =>'B', 'В' =>'V', 'Г' =>'G', 'Д' =>'D', 'Е' =>'E', 'Ё' =>'Yo', 'Ж' =>'Zh',
  68. 'З' =>'Z', 'И' =>'I', 'Й' =>'J', 'К' =>'K', 'Л' =>'L', 'М' =>'M', 'Н' =>'N', 'О' =>'O',
  69. 'П' =>'P', 'Р' =>'R', 'С' =>'S', 'Т' =>'T', 'У' =>'U', 'Ф' =>'F', 'Х' =>'H', 'Ц' =>'C',
  70. 'Ч' =>'Ch', 'Ш' =>'Sh', 'Щ' =>'Sh', 'Ъ' =>'', 'Ы' =>'Y', 'Ь' =>'', 'Э' =>'E', 'Ю' =>'Yu',
  71. 'Я' =>'Ya'
  72. ),
  73. array(
  74. 'Є' =>'Ye', 'І' =>'I', 'Ї' =>'Yi', 'Ґ' =>'G', 'є' =>'ye', 'і' =>'i', 'ї' =>'yi', 'ґ' =>'g'
  75. ),
  76. array(
  77. 'č' =>'c', 'ď' =>'d', 'ě' =>'e', 'ň' => 'n', 'ř' =>'r', 'š' =>'s', 'ť' =>'t', 'ů' =>'u',
  78. 'ž' =>'z', 'Č' =>'C', 'Ď' =>'D', 'Ě' =>'E', 'Ň' => 'N', 'Ř' =>'R', 'Š' =>'S', 'Ť' =>'T',
  79. 'Ů' =>'U', 'Ž' =>'Z'
  80. ),
  81. array(
  82. 'ą' =>'a', 'ć' =>'c', 'ę' =>'e', 'ł' =>'l', 'ń' =>'n', 'ó' =>'o', 'ś' =>'s', 'ź' =>'z',
  83. 'ż' =>'z', 'Ą' =>'A', 'Ć' =>'C', 'Ę' =>'e', 'Ł' =>'L', 'Ń' =>'N', 'Ó' =>'o', 'Ś' =>'S',
  84. 'Ź' =>'Z', 'Ż' =>'Z'
  85. ),
  86. array(
  87. 'ā' =>'a', 'č' =>'c', 'ē' =>'e', 'ģ' =>'g', 'ī' =>'i', 'ķ' =>'k', 'ļ' =>'l', 'ņ' =>'n',
  88. 'š' =>'s', 'ū' =>'u', 'ž' =>'z', 'Ā' =>'A', 'Č' =>'C', 'Ē' =>'E', 'Ģ' =>'G', 'Ī' =>'i',
  89. 'Ķ' =>'k', 'Ļ' =>'L', 'Ņ' =>'N', 'Š' =>'S', 'Ū' =>'u', 'Ž' =>'Z'
  90. )
  91. );
  92. /**
  93. * Convert word in to the format for a Zend_Db table name. Converts 'ModelName' to 'model_name'
  94. *
  95. * @param string $word Word to tableize
  96. * @return string $word Tableized word
  97. */
  98. public static function tableize($word, $separator = '_')
  99. {
  100. return strtolower(preg_replace('~(?<=\\w)([A-Z])~', $separator . '$1', $word));
  101. }
  102. /**
  103. * Convert a word in to the format for a Zend_Db class name. Converts 'table_name' to 'TableName'
  104. *
  105. * @param string $word Word to classify
  106. * @return string $word Classified word
  107. */
  108. public static function classify($word)
  109. {
  110. $inflector = new Zend_Filter_Word_UnderscoreToCamelCase();
  111. return $inflector->filter($word);
  112. }
  113. public static function modelize($word, $separator = '_')
  114. {
  115. return self::classify(substr($word, strpos($word, $separator) + 1));
  116. }
  117. public static function nonLatinCharacter($text)
  118. {
  119. foreach (self::$_characterMap as $characters) {
  120. $text = str_replace(array_keys($characters), array_values($characters), $text);
  121. }
  122. return $text;
  123. }
  124. public static function slugify($text)
  125. {
  126. $original = $text;
  127. $text = self::nonLatinCharacter($text);
  128. $text = self::urlize($text);
  129. if ($text == null) {
  130. return md5($original);
  131. }
  132. return $text;
  133. }
  134. /**
  135. * Check if a string has utf7 characters in it
  136. *
  137. * By bmorel at ssi dot fr
  138. *
  139. * @param string $string
  140. * @return boolean $bool
  141. */
  142. public static function seemsUtf8($string)
  143. {
  144. for ($i = 0; $i < strlen($string); $i++) {
  145. if (ord($string[$i]) < 0x80) continue; # 0bbbbbbb
  146. elseif ((ord($string[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
  147. elseif ((ord($string[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
  148. elseif ((ord($string[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
  149. elseif ((ord($string[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
  150. elseif ((ord($string[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
  151. else return false; # Does not match any model
  152. for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
  153. if ((++$i == strlen($string)) || ((ord($string[$i]) & 0xC0) != 0x80))
  154. return false;
  155. }
  156. }
  157. return true;
  158. }
  159. /**
  160. * Remove any illegal characters, accents, etc.
  161. *
  162. * @param string $string String to unaccent
  163. * @return string $string Unaccented string
  164. */
  165. public static function unaccent($string)
  166. {
  167. if (!preg_match('/[\x80-\xff]/', $string)) {
  168. return $string;
  169. }
  170. if (self::seemsUtf8($string)) {
  171. $chars = array(
  172. // Decompositions for Latin-1 Supplement
  173. chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  174. chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  175. chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  176. chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  177. chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  178. chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  179. chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  180. chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  181. chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  182. chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  183. chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  184. chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  185. chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  186. chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  187. chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  188. chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  189. chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  190. chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  191. chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  192. chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  193. chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  194. chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  195. chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  196. chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  197. chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  198. chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  199. chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  200. chr(195).chr(191) => 'y',
  201. // Decompositions for Latin Extended-A
  202. chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  203. chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  204. chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  205. chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  206. chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  207. chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  208. chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  209. chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  210. chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  211. chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  212. chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  213. chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  214. chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  215. chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  216. chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  217. chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  218. chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  219. chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  220. chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  221. chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  222. chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  223. chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  224. chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  225. chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  226. chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  227. chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  228. chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  229. chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  230. chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  231. chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  232. chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  233. chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  234. chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
  235. chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
  236. chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
  237. chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
  238. chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
  239. chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
  240. chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
  241. chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
  242. chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
  243. chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
  244. chr(197).chr(148) => 'R', chr(197).chr(149) => 'r',
  245. chr(197).chr(150) => 'R', chr(197).chr(151) => 'r',
  246. chr(197).chr(152) => 'R', chr(197).chr(153) => 'r',
  247. chr(197).chr(154) => 'S', chr(197).chr(155) => 's',
  248. chr(197).chr(156) => 'S', chr(197).chr(157) => 's',
  249. chr(197).chr(158) => 'S', chr(197).chr(159) => 's',
  250. chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
  251. chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
  252. chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
  253. chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
  254. chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
  255. chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
  256. chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
  257. chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
  258. chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
  259. chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
  260. chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
  261. chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
  262. chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
  263. chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
  264. chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
  265. chr(197).chr(190) => 'z', chr(197).chr(191) => 's',
  266. // Euro Sign
  267. chr(226).chr(130).chr(172) => 'E',
  268. // GBP (Pound) Sign
  269. chr(194).chr(163) => '',
  270. 'Ä' => 'Ae', 'ä' => 'ae', 'Ü' => 'Ue', 'ü' => 'ue',
  271. 'Ö' => 'Oe', 'ö' => 'oe', 'ß' => 'ss');
  272. $string = strtr($string, $chars);
  273. } else {
  274. // Assume ISO-8859-1 if not UTF-8
  275. $chars['in'] = chr(128).chr(131).chr(138).chr(142).chr(154).chr(158)
  276. .chr(159).chr(162).chr(165).chr(181).chr(192).chr(193).chr(194)
  277. .chr(195).chr(196).chr(197).chr(199).chr(200).chr(201).chr(202)
  278. .chr(203).chr(204).chr(205).chr(206).chr(207).chr(209).chr(210)
  279. .chr(211).chr(212).chr(213).chr(214).chr(216).chr(217).chr(218)
  280. .chr(219).chr(220).chr(221).chr(224).chr(225).chr(226).chr(227)
  281. .chr(228).chr(229).chr(231).chr(232).chr(233).chr(234).chr(235)
  282. .chr(236).chr(237).chr(238).chr(239).chr(241).chr(242).chr(243)
  283. .chr(244).chr(245).chr(246).chr(248).chr(249).chr(250).chr(251)
  284. .chr(252).chr(253).chr(255);
  285. $chars['out'] = "EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy";
  286. $string = strtr($string, $chars['in'], $chars['out']);
  287. $doubleChars['in'] = array(chr(140), chr(156), chr(198), chr(208), chr(222), chr(223), chr(230), chr(240), chr(254));
  288. $doubleChars['out'] = array('OE', 'oe', 'AE', 'DH', 'TH', 'ss', 'ae', 'dh', 'th');
  289. $string = str_replace($doubleChars['in'], $doubleChars['out'], $string);
  290. }
  291. return $string;
  292. }
  293. /**
  294. * Convert any passed string to a url friendly string. Converts 'My first blog post' to 'my-first-blog-post'
  295. *
  296. * @param string $text Text to urlize
  297. * @return string $text Urlized text
  298. */
  299. public static function urlize($text)
  300. {
  301. // Remove all non url friendly characters with the unaccent function
  302. $text = self::unaccent($text);
  303. if (function_exists('mb_strtolower')) {
  304. $text = mb_strtolower($text);
  305. } else {
  306. $text = strtolower($text);
  307. }
  308. // Remove all none word characters
  309. $text = preg_replace('/\W/', ' ', $text);
  310. // More stripping. Replace spaces with dashes
  311. $text = preg_replace('/[^A-Z^a-z^0-9^\/]+/', '-',
  312. preg_replace('/([a-z\d])([A-Z])/', '\1_\2',
  313. preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2',
  314. preg_replace('/::/', '/', $text))));
  315. return trim($text, '-');
  316. }
  317. /**
  318. * Pluralize a word, for example "category" will be "categories", etc.
  319. *
  320. * @param string $word
  321. * @return string The pluralized word
  322. */
  323. public static function pluralize($word)
  324. {
  325. if (substr($word, -1) == 'y') {
  326. return sprintf('%s%s', substr($word, 0, -1), 'ies');
  327. }
  328. return sprintf('%s%s', $word, 's');
  329. }
  330. /**
  331. * Retrieve the extension of a filename.
  332. *
  333. * @param string $filename The filename
  334. * @param string $withDot If true, the return value will be with dot
  335. * @return string The extension of the specified filename
  336. */
  337. public static function extension($filename, $withDot = true)
  338. {
  339. if (preg_match('`.*(\.[a-z]*)(\?.*)?(#.*)?`i', $filename, $matches)) {
  340. $ext = $matches[1];
  341. if ($withDot === false) {
  342. return substr($ext, 1);
  343. }
  344. return $ext;
  345. }
  346. return false;
  347. }
  348. /**
  349. * Camelize a text.
  350. *
  351. * @param string $text
  352. * @return string The camelized text
  353. * @TODO: this function is never used, and seems not be usefull
  354. */
  355. public static function camelize($text)
  356. {
  357. return self::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
  358. }
  359. public static function sortArrayByArray(array $toSort, array $sortByValuesAsKeys)
  360. {
  361. $commonKeysInOrder = array_intersect_key(array_flip($sortByValuesAsKeys), $toSort);
  362. $commonKeysWithValue = array_intersect_key($toSort, $commonKeysInOrder);
  363. $sorted = array_merge($commonKeysInOrder, $commonKeysWithValue);
  364. return $sorted;
  365. }
  366. /**
  367. * @see http://www.php.net/manual/fr/function.substr.php#80247
  368. */
  369. public static function cuttext($value, $length, $separator = '...')
  370. {
  371. if (strlen($value) < 1)
  372. return $value;
  373. if (is_array($value))
  374. list($string, $matchTo) = $value;
  375. else {
  376. $string = $value;
  377. $matchTo = $value{0};
  378. }
  379. $matchStart = stristr($string, $matchTo);
  380. $matchCompute = strlen($string) - strlen($matchStart);
  381. if (strlen($string) > $length) {
  382. if ($matchCompute < ($length - strlen($matchTo))) {
  383. $preString = substr($string, 0, $length);
  384. $posEnd = strrpos($preString, ' ');
  385. if (false === $posEnd)
  386. $string = $preString . $separator;
  387. else
  388. $string = substr($preString, 0, $posEnd) . $separator;
  389. } else if ($matchCompute > (strlen($string) - ($length - strlen($matchTo)))) {
  390. $preString = substr($string, (strlen($string) - ($length - strlen($matchTo))));
  391. $posStart = strpos($preString, ' ');
  392. $string = $separator.substr($preString, $posStart);
  393. if (false === $posStart)
  394. $string = $separator . $preString;
  395. else
  396. $string = $separator . substr($preString, $posStart);
  397. } else {
  398. $preString = substr($string, ($matchCompute - round(($length / 3))), $length);
  399. $posStart = strpos($preString, ' ');
  400. $posEnd = strrpos($preString, ' ');
  401. $string = $separator . substr($preString, $posStart, $posEnd) . $separator;
  402. if (false === $posStart && false === $posEnd)
  403. $string = $separator . $preString . $separator;
  404. else
  405. $string = $separator . substr($preString, $posStart, $posEnd) . $separator;
  406. }
  407. $matchStart = stristr($string, $matchTo);
  408. $matchCompute = strlen($string) - strlen($matchStart);
  409. }
  410. return $string;
  411. }
  412. /**
  413. * Returns subject replaced with regular expression matchs
  414. *
  415. * @param mixed $search subject to search
  416. * @param array $replacePairs array of search => replace pairs
  417. */
  418. public static function pregtr($search, $replacePairs)
  419. {
  420. return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
  421. }
  422. public static function uniq($name)
  423. {
  424. return sha1($name) . time() . md5(mt_rand(0, 999999));
  425. }
  426. /**
  427. * Retrieve the id of an object.
  428. *
  429. * @param mixed $target
  430. * @return string
  431. */
  432. public static function id($target)
  433. {
  434. if (is_string($target))
  435. return $target;
  436. return sprintf("%s_%s", get_class($target), spl_object_hash($target));
  437. }
  438. public static function entityDecode($str)
  439. {
  440. if (null === $str)
  441. return '';
  442. $str = strip_tags($str);
  443. $str = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $str);
  444. $str = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $str);
  445. $str = htmlspecialchars_decode($str);
  446. $str = html_entity_decode($str, ENT_COMPAT, 'UTF-8');
  447. return $str;
  448. }
  449. public static function urlEncode($str)
  450. {
  451. $str = base64_encode($str);
  452. return strtr($str, array('+' => '-', '/' => '_', '=' => ''));
  453. }
  454. public static function urlDecode($str)
  455. {
  456. $str = strtr($str, array('-' => '+', '_' => '/'));
  457. return base64_decode($str);
  458. }
  459. public static function md5UrlEncode($str)
  460. {
  461. return self::UrlEncode(md5($str));
  462. }
  463. public static function sha1UrlEncode($str)
  464. {
  465. return self::UrlEncode(sha1($str));
  466. }
  467. public static function crypt($str)
  468. {
  469. return base64_encode($str);
  470. }
  471. public static function uncrypt($str)
  472. {
  473. return base64_decode($str);
  474. }
  475. public static function roundUpTo($number, $increments) {
  476. return (ceil($number / $increments) * $increments);
  477. }
  478. public static function roundTo($number, $increments) {
  479. return (round($number / $increments) * $increments);
  480. }
  481. public static function roundDowTo($number, $increments) {
  482. return (floor($number / $increments) * $increments);
  483. }
  484. }