PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/nette-dev/Templates/Filters/TemplateHelpers.php

https://github.com/bazo/Mokuji
PHP | 279 lines | 108 code | 58 blank | 113 comment | 15 complexity | 2eaed737679677d57e8b2e639ce37b49 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Templates
  10. */
  11. /**
  12. * Standard template run-time helpers shipped with Nette Framework.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette\Templates
  16. */
  17. final class TemplateHelpers
  18. {
  19. /**
  20. * Static class - cannot be instantiated.
  21. */
  22. final public function __construct()
  23. {
  24. throw new LogicException("Cannot instantiate static class " . get_class($this));
  25. }
  26. /**
  27. * Try to load the requested helper.
  28. * @param string helper name
  29. * @return callback
  30. */
  31. public static function loader($helper)
  32. {
  33. $callback = callback('TemplateHelpers', $helper);
  34. if ($callback->isCallable()) {
  35. return $callback;
  36. }
  37. $callback = callback('String', $helper);
  38. if ($callback->isCallable()) {
  39. return $callback;
  40. }
  41. }
  42. /**
  43. * Escapes string for use inside HTML template.
  44. * @param mixed UTF-8 encoding or 8-bit
  45. * @return string
  46. */
  47. public static function escapeHtml($s)
  48. {
  49. if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
  50. return $s->__toString(TRUE);
  51. }
  52. return htmlSpecialChars($s, ENT_QUOTES);
  53. }
  54. /**
  55. * Escapes string for use inside HTML comments.
  56. * @param mixed UTF-8 encoding or 8-bit
  57. * @return string
  58. */
  59. public static function escapeHtmlComment($s)
  60. {
  61. // -- has special meaning in different browsers
  62. return str_replace('--', '--><!--', $s); // HTML tags have no meaning inside comments
  63. }
  64. /**
  65. * Escapes string for use inside XML 1.0 template.
  66. * @param string UTF-8 encoding or 8-bit
  67. * @return string
  68. */
  69. public static function escapeXML($s)
  70. {
  71. // XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
  72. // XML 1.1: \x00 forbidden directly and as a character reference, \x09 \x0A \x0D \x85 allowed directly, C0, C1 and \x7F allowed as character references
  73. return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
  74. }
  75. /**
  76. * Escapes string for use inside CSS template.
  77. * @param string UTF-8 encoding or 8-bit
  78. * @return string
  79. */
  80. public static function escapeCss($s)
  81. {
  82. // http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
  83. return addcslashes($s, "\x00..\x2C./:;<=>?@[\\]^`{|}~");
  84. }
  85. /**
  86. * Escapes string for use inside HTML style attribute.
  87. * @param string UTF-8 encoding or 8-bit
  88. * @return string
  89. */
  90. public static function escapeHtmlCss($s)
  91. {
  92. return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
  93. }
  94. /**
  95. * Escapes string for use inside JavaScript template.
  96. * @param mixed UTF-8 encoding
  97. * @return string
  98. */
  99. public static function escapeJs($s)
  100. {
  101. if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
  102. $s = $s->__toString(TRUE);
  103. }
  104. return str_replace(']]>', ']]\x3E', json_encode($s));
  105. }
  106. /**
  107. * Escapes string for use inside HTML JavaScript attribute.
  108. * @param mixed UTF-8 encoding
  109. * @return string
  110. */
  111. public static function escapeHtmlJs($s)
  112. {
  113. return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
  114. }
  115. /**
  116. * Replaces all repeated white spaces with a single space.
  117. * @param string UTF-8 encoding or 8-bit
  118. * @return string
  119. */
  120. public static function strip($s)
  121. {
  122. $s = preg_replace_callback('#<(textarea|pre|script).*?</\\1#si', array(__CLASS__, 'indentCb'), $s);
  123. $s = trim(preg_replace('#[ \t\r\n]+#', ' ', $s));
  124. return strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
  125. }
  126. /**
  127. * Indents the HTML content from the left.
  128. * @param string UTF-8 encoding or 8-bit
  129. * @param int
  130. * @param string
  131. * @return string
  132. */
  133. public static function indent($s, $level = 1, $chars = "\t")
  134. {
  135. if ($level >= 1) {
  136. $s = preg_replace_callback('#<(textarea|pre).*?</\\1#si', array(__CLASS__, 'indentCb'), $s);
  137. $s = String::indent($s, $level, $chars);
  138. $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
  139. }
  140. return $s;
  141. }
  142. /**
  143. * Callback for self::indent
  144. */
  145. private static function indentCb($m)
  146. {
  147. return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");
  148. }
  149. /**
  150. * Date/time formatting.
  151. * @param string|int|DateTime
  152. * @param string
  153. * @return string
  154. */
  155. public static function date($time, $format = "%x")
  156. {
  157. if ($time == NULL) { // intentionally ==
  158. return NULL;
  159. }
  160. $time = Tools::createDateTime($time);
  161. return strpos($format, '%') === FALSE
  162. ? $time->format($format) // formats using date()
  163. : strftime($format, $time->format('U')); // formats according to locales
  164. }
  165. /**
  166. * Converts to human readable file size.
  167. * @param int
  168. * @param int
  169. * @return string
  170. */
  171. public static function bytes($bytes, $precision = 2)
  172. {
  173. $bytes = round($bytes);
  174. $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
  175. foreach ($units as $unit) {
  176. if (abs($bytes) < 1024 || $unit === end($units)) break;
  177. $bytes = $bytes / 1024;
  178. }
  179. return round($bytes, $precision) . ' ' . $unit;
  180. }
  181. /**
  182. * Returns array of string length.
  183. * @param mixed
  184. * @return int
  185. */
  186. public static function length($var)
  187. {
  188. return is_string($var) ? iconv_strlen($var, 'UTF-8') : count($var);
  189. }
  190. /**
  191. * Performs a search and replace.
  192. * @param string
  193. * @param string
  194. * @param string
  195. * @return string
  196. */
  197. public static function replace($subject, $search, $replacement = '')
  198. {
  199. return str_replace($search, $replacement, $subject);
  200. }
  201. /**
  202. * Performs a regular expression search and replace.
  203. * @param string
  204. * @param string
  205. * @param string
  206. * @return string
  207. */
  208. public static function replaceRe($subject, $pattern, $replacement = '')
  209. {
  210. return preg_replace($pattern, $replacement, $subject);
  211. }
  212. /**
  213. * /dev/null.
  214. * @param mixed
  215. * @return string
  216. */
  217. public static function null($value)
  218. {
  219. return '';
  220. }
  221. }