PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Templating/DefaultHelpers.php

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