PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/LiquidStandardFilters.class.php

http://github.com/harrydeluxe/php-liquid
PHP | 309 lines | 268 code | 8 blank | 33 comment | 3 complexity | 902827bf386982d16bbbd955d367f8e3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * A selection of standard filters
  4. *
  5. * @package Liquid
  6. * @copyright Copyright (c) 2011-2012 Harald Hanek,
  7. * fork of php-liquid (c) 2006 Mateo Murphy,
  8. * based on Liquid for Ruby (c) 2006 Tobias Luetke
  9. * @license http://harrydeluxe.mit-license.org
  10. */
  11. class LiquidStandardFilters
  12. {
  13. /**
  14. * Return the size of an array or of an string
  15. *
  16. * @param mixed $input
  17. * @return int
  18. */
  19. public static function size($input)
  20. {
  21. if (is_string($input) || is_numeric($input))
  22. {
  23. return strlen($input);
  24. }
  25. elseif (is_array($input))
  26. {
  27. return count($input);
  28. }
  29. elseif (is_object($input))
  30. {
  31. if (method_exists($input, 'size'))
  32. {
  33. return $input->size();
  34. }
  35. }
  36. return $input;
  37. }
  38. /**
  39. * Convert an input to lowercase
  40. *
  41. * @param string $input
  42. * @return string
  43. */
  44. public static function downcase($input)
  45. {
  46. return is_string($input) ? strtolower($input) : $input;
  47. }
  48. /**
  49. * Convert an input to uppercase
  50. *
  51. * @param string $input
  52. * @return string
  53. */
  54. public static function upcase($input)
  55. {
  56. return is_string($input) ? strtoupper($input) : $input;
  57. }
  58. /**
  59. * Capitalize words in the input sentence
  60. *
  61. * @param string $input
  62. * @return string
  63. */
  64. public static function capitalize($input)
  65. {
  66. return preg_replace("!(^|[^\p{L}'])([\p{Ll}])!ue", "'\\1'.ucfirst('\\2')", ucwords($input));
  67. }
  68. /**
  69. * Escape a string
  70. *
  71. * @param string $input
  72. * @return string
  73. */
  74. public static function escape($input)
  75. {
  76. return is_string($input) ? addslashes($input) : $input;
  77. }
  78. /**
  79. * Strip all newlines (\n, \r) from string
  80. *
  81. * @param string $input
  82. * @return string
  83. */
  84. public static function strip_newlines($input)
  85. {
  86. return is_string($input) ? str_replace(array(
  87. "\n", "\r"
  88. ), '', $input) : $input;
  89. }
  90. /**
  91. * Replace each newline (\n) with html break
  92. *
  93. * @param string $input
  94. * @return string
  95. */
  96. public static function newline_to_br($input)
  97. {
  98. return is_string($input) ? str_replace(array(
  99. "\n", "\r"
  100. ), '<br />', $input) : $input;
  101. }
  102. /**
  103. * Truncate a string down to x characters
  104. *
  105. * @param string $input
  106. * @param int $characters
  107. * @return string
  108. */
  109. public static function truncate($input, $characters = 100)
  110. {
  111. if (is_string($input) || is_numeric($input))
  112. {
  113. if (strlen($input) > $characters)
  114. {
  115. return substr($input, 0, $characters) . '&hellip;';
  116. }
  117. }
  118. return $input;
  119. }
  120. /**
  121. * Truncate string down to x words
  122. *
  123. * @param string $input
  124. * @param int $words
  125. * @return string
  126. */
  127. public static function truncatewords($input, $words = 3)
  128. {
  129. if (is_string($input))
  130. {
  131. $wordlist = explode(" ", $input);
  132. if (count($wordlist) > $words)
  133. {
  134. return implode(" ", array_slice($wordlist, 0, $words)) . '&hellip;';
  135. }
  136. }
  137. return $input;
  138. }
  139. /**
  140. * Removes html tags from text
  141. *
  142. * @param string $input
  143. * @return string
  144. */
  145. public static function strip_html($input)
  146. {
  147. return is_string($input) ? strip_tags($input) : $input;
  148. }
  149. /**
  150. * Joins elements of an array with a given character between them
  151. *
  152. * @param array $input
  153. * @param string $glue
  154. * @return string
  155. */
  156. public static function join($input, $glue = ' ')
  157. {
  158. return is_array($input) ? implode($glue, $input) : $input;
  159. }
  160. /**
  161. * Formats a date using strftime
  162. *
  163. * @param mixed $input
  164. * @param string $format
  165. * @return string
  166. */
  167. public static function date($input, $format)
  168. {
  169. if (!is_numeric($input))
  170. {
  171. $input = strtotime($input);
  172. }
  173. if ($format == 'r')
  174. return date($format, $input);
  175. return strftime($format, $input);
  176. }
  177. /**
  178. * Returns the first element of an array
  179. *
  180. * @param array $input
  181. * @return mixed
  182. */
  183. public static function first($input)
  184. {
  185. return is_array($input) ? reset($input) : $input;
  186. }
  187. /**
  188. * Returns the last element of an array
  189. *
  190. * @param array $input
  191. * @return mixed
  192. */
  193. public static function last($input)
  194. {
  195. return is_array($input) ? end($input) : $input;
  196. }
  197. /**
  198. * Split input string into an array of substrings separated by given pattern.
  199. *
  200. * @param string $input
  201. * @param string $pattern
  202. * @return array
  203. */
  204. public static function split($input, $pattern)
  205. {
  206. return explode($pattern, $input);
  207. }
  208. /**
  209. * addition
  210. *
  211. * @param int $input
  212. * @param int $operand
  213. * @return int
  214. */
  215. public static function plus($input, $operand)
  216. {
  217. return (int) $input + (int) $operand;
  218. }
  219. /**
  220. * subtraction
  221. *
  222. * @param int $input
  223. * @param int $operand
  224. * @return int
  225. */
  226. public static function minus($input, $operand)
  227. {
  228. return (int) $input - (int) $operand;
  229. }
  230. /**
  231. * multiplication
  232. *
  233. * @param int $input
  234. * @param int $operand
  235. * @return int
  236. */
  237. public static function times($input, $operand)
  238. {
  239. return (int) $input * (int) $operand;
  240. }
  241. /**
  242. * division
  243. *
  244. * @param int $input
  245. * @param int $operand
  246. * @return int
  247. */
  248. public static function divided_by($input, $operand)
  249. {
  250. return (int) $input / (int) $operand;
  251. }
  252. /**
  253. * modulo
  254. *
  255. * @param int $input
  256. * @param int $operand
  257. * @return int
  258. */
  259. public static function modulo($input, $operand)
  260. {
  261. return (int) $input % (int) $operand;
  262. }
  263. }