/lib/Fenom/Modifier.php

https://gitlab.com/matadorov/gravity_pph · PHP · 296 lines · 163 code · 18 blank · 115 comment · 32 complexity · 990e796f367940ec9fb62efbe5042b0d MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Fenom.
  4. *
  5. * (c) 2013 Ivan Shalganov
  6. *
  7. * For the full copyright and license information, please view the license.md
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fenom;
  11. /**
  12. * Collection of modifiers
  13. * @author Ivan Shalganov <a.cobest@gmail.com>
  14. */
  15. class Modifier
  16. {
  17. /**
  18. * Date format
  19. *
  20. * @param string|int $date
  21. * @param string $format
  22. * @return string
  23. */
  24. public static function dateFormat($date, $format = "%b %e, %Y")
  25. {
  26. if (!is_numeric($date)) {
  27. $date = strtotime($date);
  28. if (!$date) {
  29. $date = time();
  30. }
  31. }
  32. return strftime($format, $date);
  33. }
  34. /**
  35. * @param string $date
  36. * @param string $format
  37. * @return string
  38. */
  39. public static function date($date, $format = "Y m d")
  40. {
  41. if (!is_numeric($date)) {
  42. $date = strtotime($date);
  43. if (!$date) {
  44. $date = time();
  45. }
  46. }
  47. return date($format, $date);
  48. }
  49. /**
  50. * Escape string
  51. *
  52. * @param string $text
  53. * @param string $type
  54. * @param string $charset
  55. * @return string
  56. */
  57. public static function escape($text, $type = 'html', $charset = null)
  58. {
  59. switch (strtolower($type)) {
  60. case "url":
  61. return urlencode($text);
  62. case "html";
  63. return htmlspecialchars($text, ENT_COMPAT, $charset ? $charset : \Fenom::$charset);
  64. case "js":
  65. return json_encode($text, 64 | 256); // JSON_UNESCAPED_SLASHES = 64, JSON_UNESCAPED_UNICODE = 256
  66. default:
  67. return $text;
  68. }
  69. }
  70. /**
  71. * Unescape escaped string
  72. *
  73. * @param string $text
  74. * @param string $type
  75. * @return string
  76. */
  77. public static function unescape($text, $type = 'html')
  78. {
  79. switch (strtolower($type)) {
  80. case "url":
  81. return urldecode($text);
  82. case "html";
  83. return htmlspecialchars_decode($text);
  84. default:
  85. return $text;
  86. }
  87. }
  88. /**
  89. * Crop string to specific length (support unicode)
  90. *
  91. * @param string $string text witch will be truncate
  92. * @param int $length maximum symbols of result string
  93. * @param string $etc place holder truncated symbols
  94. * @param bool $by_words
  95. * @param bool $middle
  96. * @return string
  97. */
  98. public static function truncate($string, $length = 80, $etc = '...', $by_words = false, $middle = false)
  99. {
  100. if ($middle) {
  101. if (preg_match('#^(.{' . $length . '}).*?(.{' . $length . '})?$#usS', $string, $match)) {
  102. if (count($match) == 3) {
  103. if ($by_words) {
  104. return preg_replace('#\s\S*$#usS', "", $match[1]) .
  105. $etc .
  106. preg_replace('#\S*\s#usS', "", $match[2]);
  107. } else {
  108. return $match[1] . $etc . $match[2];
  109. }
  110. }
  111. }
  112. } else {
  113. if (preg_match('#^(.{' . $length . '})#usS', $string, $match)) {
  114. if ($by_words) {
  115. return preg_replace('#\s\S*$#usS', "", $match[1]) . $etc;
  116. } else {
  117. return $match[1] . $etc;
  118. }
  119. }
  120. }
  121. return $string;
  122. }
  123. /**
  124. * Strip spaces symbols on edge of string end multiple spaces in the string
  125. *
  126. * @param string $str
  127. * @param bool $to_line strip line ends
  128. * @return string
  129. */
  130. public static function strip($str, $to_line = false)
  131. {
  132. $str = trim($str);
  133. if ($to_line) {
  134. return preg_replace('#\s+#ms', ' ', $str);
  135. } else {
  136. return preg_replace('#[ \t]{2,}#', ' ', $str);
  137. }
  138. }
  139. /**
  140. * Return length of UTF8 string, array, countable object
  141. * @param mixed $item
  142. * @return int
  143. */
  144. public static function length($item)
  145. {
  146. if (is_string($item)) {
  147. return strlen(preg_replace('#[\x00-\x7F]|[\x80-\xDF][\x00-\xBF]|[\xE0-\xEF][\x00-\xBF]{2}#s', ' ', $item));
  148. } elseif (is_array($item)) {
  149. return count($item);
  150. } elseif ($item instanceof \Countable) {
  151. return $item->count();
  152. } else {
  153. return 0;
  154. }
  155. }
  156. /**
  157. *
  158. * @param mixed $value
  159. * @param mixed $haystack
  160. * @return bool
  161. */
  162. public static function in($value, $haystack)
  163. {
  164. if(is_scalar($value)) {
  165. if (is_array($haystack)) {
  166. return in_array($value, $haystack) || array_key_exists($value, $haystack);
  167. } elseif (is_string($haystack)) {
  168. return strpos($haystack, $value) !== false;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. * @param mixed $value
  175. * @return bool
  176. */
  177. public static function isIterable($value)
  178. {
  179. return is_array($value) || ($value instanceof \Iterator);
  180. }
  181. /**
  182. * Replace all occurrences of the search string with the replacement string
  183. * @param string $value The string being searched and replaced on, otherwise known as the haystack.
  184. * @param string $search The value being searched for, otherwise known as the needle.
  185. * @param string $replace The replacement value that replaces found search
  186. * @return mixed
  187. */
  188. public static function replace($value, $search, $replace)
  189. {
  190. return str_replace($search, $replace, $value);
  191. }
  192. /**
  193. * @param string $value
  194. * @param string $pattern
  195. * @param string $replacement
  196. * @return mixed
  197. */
  198. public static function ereplace($value, $pattern, $replacement)
  199. {
  200. return preg_replace($pattern, $replacement, $value);
  201. }
  202. /**
  203. * @param string $string
  204. * @param string $pattern
  205. * @return bool
  206. */
  207. public static function match($string, $pattern)
  208. {
  209. return fnmatch($pattern, $string);
  210. }
  211. /**
  212. * @param string $string
  213. * @param string $pattern
  214. * @return int
  215. */
  216. public static function ematch($string, $pattern)
  217. {
  218. return preg_match($pattern, $string);
  219. }
  220. /**
  221. * @param string $value
  222. * @param string $delimiter
  223. * @return array
  224. */
  225. public static function split($value, $delimiter = ",")
  226. {
  227. if(is_string($value)) {
  228. return explode($delimiter, $value);
  229. } elseif(is_array($value)) {
  230. return $value;
  231. } else {
  232. return array();
  233. }
  234. }
  235. /**
  236. * @param $value
  237. * @param string $pattern
  238. * @return array
  239. */
  240. public static function esplit($value, $pattern = '/,\s*/S')
  241. {
  242. if(is_string($value)) {
  243. return preg_split($pattern, $value);
  244. } elseif(is_array($value)) {
  245. return $value;
  246. } else {
  247. return array();
  248. }
  249. }
  250. /**
  251. * @param $value
  252. * @param string $glue
  253. * @return string
  254. */
  255. public static function join($value, $glue = ",")
  256. {
  257. if(is_array($value)) {
  258. return implode($glue, $value);
  259. } elseif(is_string($value)) {
  260. return $value;
  261. } else {
  262. return "";
  263. }
  264. }
  265. /**
  266. * @param string|int $from
  267. * @param string|int $to
  268. * @param int $step
  269. * @return RangeIterator
  270. */
  271. public static function range($from, $to, $step = 1) {
  272. if($from instanceof RangeIterator) {
  273. return $from->setStep($to);
  274. } else {
  275. return new RangeIterator($from, $to, $step);
  276. }
  277. }
  278. }