Url.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ CXDHlInlrvmyqFCA1iYWVGyijIcOwxeEZp6enK5w9rc=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.5
  11. */
  12. /**
  13. * url view helper
  14. */
  15. namespace Cube\View\Helper;
  16. use Cube\Controller\Front;
  17. class Url extends AbstractHelper
  18. {
  19. /**
  20. *
  21. * character map
  22. *
  23. * @var array
  24. */
  25. protected static $_charMap = array(
  26. // Latin
  27. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',
  28. 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
  29. 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',
  30. 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',
  31. 'ß' => 'ss',
  32. 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',
  33. 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
  34. 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o',
  35. 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
  36. 'ÿ' => 'y',
  37. // Latin symbols
  38. '©' => '(c)',
  39. // Greek
  40. 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
  41. 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
  42. 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
  43. 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
  44. 'Ϋ' => 'Y',
  45. 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
  46. 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
  47. 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
  48. 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
  49. 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
  50. // Turkish
  51. 'Ş' => 'S', 'İ' => 'I', 'Ğ' => 'G',
  52. 'ş' => 's', 'ı' => 'i', 'ğ' => 'g',
  53. // Romanian
  54. 'Ă' => 'A', 'ă' => 'a',
  55. 'Ș' => 's', 'ș' => 's', 'Ț' => 'T', 'ț' => 't',
  56. // Russian
  57. 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
  58. 'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
  59. 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
  60. 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
  61. 'Я' => 'Ya',
  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. // Ukrainian
  68. 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G',
  69. 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g',
  70. // Czech
  71. 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U',
  72. 'Ž' => 'Z',
  73. 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
  74. 'ž' => 'z',
  75. // Polish
  76. 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ś' => 'S', 'Ź' => 'Z',
  77. 'Ż' => 'Z',
  78. 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ś' => 's', 'ź' => 'z',
  79. 'ż' => 'z',
  80. // Latvian
  81. 'Ā' => 'A', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N',
  82. 'Ū' => 'u',
  83. 'ā' => 'a', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
  84. 'ū' => 'u',
  85. // Arabic
  86. 'أ' => 'a', 'ب' => 'b', 'ت' => 't', 'ث' => 'th', 'ج' => 'g', 'ح' => 'h', 'خ' => 'kh', 'د' => 'd',
  87. 'ذ' => 'th', 'ر' => 'r', 'ز' => 'z', 'س' => 's', 'ش' => 'sh', 'ص' => 's', 'ض' => 'd', 'ط' => 't',
  88. 'ظ' => 'th', 'ع' => 'aa', 'غ' => 'gh', 'ف' => 'f', 'ق' => 'k', 'ك' => 'k', 'ل' => 'l', 'م' => 'm',
  89. 'ن' => 'n', 'ه' => 'h', 'و' => 'o', 'ي' => 'y'
  90. );
  91. /**
  92. *
  93. * create an url based on a set of params and the router object
  94. *
  95. * @param string|array $params a string or an array of params
  96. * @param string $name the name of the specific route to use
  97. * @param bool $addGetParams whether to attach params resulted from a previous get operation to the url
  98. * @param array $skipParams an array of params to be omitted when constructing the url
  99. * @param bool $addBaseUrl flag to add the base url param to the assembled route
  100. * @param bool $cleanString if true, will clean values of get variables (only if params is an array)
  101. *
  102. * @return string the url of the link href attribute
  103. */
  104. public function url($params, $name = null, $addGetParams = false, array $skipParams = null, $addBaseUrl = true, $cleanString = true)
  105. {
  106. $router = Front::getInstance()->getRouter();
  107. if (is_array($params) &&
  108. $cleanString === true
  109. ) {
  110. foreach ($params as $key => $value) {
  111. $params[$key] = self::cleanString($value);
  112. }
  113. }
  114. return $router->assemble($params, $name, $addBaseUrl, $addGetParams, $skipParams);
  115. }
  116. /**
  117. *
  118. * formats a string for url display purposes
  119. *
  120. * @param string $string
  121. * @param array $replace
  122. * @param string $delimiter
  123. *
  124. * @return string|null the formatted string or null if the input is not a string
  125. */
  126. public static function cleanString($string, $replace = array(), $delimiter = '-')
  127. {
  128. if (!is_string($string)) {
  129. return $string;
  130. }
  131. if (!empty($replace)) {
  132. $string = str_replace((array)$replace, ' ', $string);
  133. }
  134. $string = str_ireplace(
  135. array('&amp;', '&#039;', '&quot;', '&lt;', '&gt;', '&nbsp;'), array('&', "'", '"', '<', '>', ' '), $string);
  136. $string = str_replace(array_keys(self::$_charMap), self::$_charMap, $string);
  137. //modify by yubin
  138. //$clean = @iconv('UTF-8', 'ASCII//TRANSLIT', $string);
  139. $clean = $string;
  140. $clean = preg_replace("/[^a-zA-Z0-9\/\_|+ -]/", '', $clean);
  141. $clean = strtolower(trim($clean));
  142. $clean = preg_replace("/[\/|+ -]+/", $delimiter, $clean);
  143. return $clean;
  144. }
  145. }