src/Twig/HelperExtension.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use Twig\Extension\AbstractExtension;
  4. use Twig\TwigFilter;
  5. use Twig\TwigFunction;
  6. use Twig\TwigTest;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. class HelperExtension extends AbstractExtension
  9. {
  10. public function __construct(EntityManagerInterface $em)
  11. {
  12. $this->em = $em;
  13. }
  14. public function getFilters(): array
  15. {
  16. return array(
  17. new TwigFilter('time_ago', array($this, 'time_ago')),
  18. new TwigFilter('time_togo', array($this, 'time_togo')),
  19. new TwigFilter('filter', array($this, 'filter')),
  20. new TwigFilter('bbcode', array($this, 'bbcode')),
  21. new TwigFilter('shorten', array($this, 'shorten')),
  22. new TwigFilter('remove_empty', array($this, 'remove_empty')),
  23. new TwigFilter('basename', array($this, 'basename')),
  24. new TwigFilter('remove_element', array($this, 'remove_element')),
  25. new TwigFilter('format_bytes', array($this, 'format_bytes')),
  26. new TwigFilter('join_objects', array($this, 'join_objects')),
  27. new TwigFilter('pluralize', array($this, 'pluralize')),
  28. new TwigFilter('cash', array($this, 'cash')),
  29. new TwigFilter('not_less_than', array($this, 'not_less_than')),
  30. new TwigFilter('filesize_convert', array($this, 'filesize_convert')),
  31. new TwigFilter('resolve_tags', array($this, 'resolve_tags')),
  32. new TwigFilter('json_decode', array($this, 'json_decode'))
  33. );
  34. }
  35. public function getFunctions(): array
  36. {
  37. return array(
  38. new TwigFunction('getRepository', array($this, 'getRepository')),
  39. );
  40. }
  41. public function getTests(): array
  42. {
  43. return array(
  44. new TwigTest('instanceof', array($this, 'isInstanceOf')),
  45. );
  46. }
  47. public function getRepository($repositoryName)
  48. {
  49. return $this->em->getRepository($repositoryName);
  50. }
  51. public function isInstanceOf($var, $instance)
  52. {
  53. if(is_null($var))
  54. return false;
  55. $reflexionClass = new \ReflectionClass($instance);
  56. return $reflexionClass->isInstance($var);
  57. }
  58. public function json_decode($text)
  59. {
  60. return json_decode($text);
  61. }
  62. public function filesize_convert($amount, $from, $to)
  63. {
  64. if($from == "byte" && $to == "gigabyte")
  65. {
  66. return ((($amount / 1024) / 1024) / 1024);
  67. }
  68. }
  69. public function not_less_than($number, $notLessThan)
  70. {
  71. if($number < $notLessThan)
  72. return $notLessThan;
  73. else
  74. return $number;
  75. }
  76. function cash(?float $amount, $currency, $symbol = false)
  77. {
  78. // Null?
  79. if($amount === null)
  80. {
  81. if($currency == "GBP")
  82. return ($symbol ? '&pound;' : '') . number_format(0, 2);
  83. else
  84. throw new \Exception("Unknown currency: " . $currency);
  85. }
  86. else
  87. {
  88. if($currency == "GBP")
  89. return ($symbol ? '&pound;' : '') . number_format($amount, 2);
  90. else
  91. throw new \Exception("Unknown currency: " . $currency);
  92. }
  93. }
  94. function pluralize($count, $single, $multiple)
  95. {
  96. if($count == 1)
  97. return $single;
  98. else
  99. return $multiple;
  100. }
  101. public function join_objects($arr, $method, $glue = ', ')
  102. {
  103. $bits = array();
  104. foreach($arr AS $someObject)
  105. {
  106. $methodName = "get" . ucfirst($method);
  107. $bits[] = $someObject->$methodName();
  108. }
  109. return implode($glue, $bits);
  110. }
  111. public function remove_element($arr, $elem)
  112. {
  113. foreach($arr AS $k => $v)
  114. {
  115. if($v === $elem)
  116. unset($arr[$k]);
  117. }
  118. return $arr;
  119. }
  120. public function basename($val)
  121. {
  122. return basename($val);
  123. }
  124. function shorten($content, $length)
  125. {
  126. return trim(substr($content, 0, $length));
  127. }
  128. function remove_empty($arr)
  129. {
  130. return array_filter($arr);
  131. }
  132. function bbcode($content)
  133. {
  134. preg_match_all('~\[PAGELINK\](.*?)\[/PAGELINK\]~s', $content, $matches);
  135. // Got any?
  136. if(count($matches))
  137. {
  138. // Loop
  139. foreach($matches[0] AS $k => $someString)
  140. {
  141. // Grab the perma ref
  142. $permaRef = $matches[1][$k];
  143. // Valid
  144. if(strlen($permaRef) == 5)
  145. {
  146. // Find the page
  147. $thePage = $this->em->getRepository('App:Pages\Page')->findOneFiltered(array(
  148. "permaRef" => $permaRef
  149. ));
  150. // Found it?
  151. if($thePage)
  152. {
  153. // Replace it
  154. $content = str_replace($someString, $thePage->getFriendlyPath(), $content);
  155. }
  156. else
  157. {
  158. // Replace it
  159. $content = str_replace($someString, "" . $permaRef, $content);
  160. }
  161. }
  162. }
  163. }
  164. return $content;
  165. }
  166. function filter($arr, $filters = array())
  167. {
  168. $keepValues = array();
  169. // Loop each
  170. foreach($arr AS $someItem)
  171. {
  172. $match = true;
  173. // Loop the filters
  174. foreach($filters AS $k => $v)
  175. {
  176. // Get the method
  177. $getterMethod = "get" . ucfirst($k);
  178. // Matched?
  179. if($someItem->$getterMethod() != $v)
  180. $match = false;
  181. }
  182. // Matched?
  183. if($match)
  184. {
  185. // Into the array
  186. $keepValues[] = $someItem;
  187. }
  188. }
  189. return $keepValues;
  190. }
  191. function time_ago(\DateTime $ptime, $word = 'ago')
  192. {
  193. $timestamp = $ptime->getTimestamp();
  194. $estimate_time = time() - $timestamp;
  195. if($timestamp < 0)
  196. {
  197. return '---';
  198. }
  199. elseif( $estimate_time < 5 )
  200. {
  201. return 'just now';
  202. }
  203. $condition = array(
  204. 12 * 30 * 24 * 60 * 60 => 'year',
  205. 30 * 24 * 60 * 60 => 'month',
  206. 24 * 60 * 60 => 'day',
  207. 60 * 60 => 'hour',
  208. 60 => 'minute',
  209. 1 => 'second'
  210. );
  211. foreach( $condition as $secs => $str )
  212. {
  213. $d = $estimate_time / $secs;
  214. if( $d >= 1 )
  215. {
  216. $r = round( $d );
  217. return $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ' . $word;
  218. }
  219. }
  220. }
  221. function time_togo(\DateTime $ptime)
  222. {
  223. $timestamp = $ptime->getTimestamp();
  224. $estimate_time = $timestamp - time();
  225. if($timestamp < 0)
  226. {
  227. return '---';
  228. }
  229. elseif( $estimate_time < 5 )
  230. {
  231. return 'just now';
  232. }
  233. $condition = array(
  234. 12 * 30 * 24 * 60 * 60 => 'year',
  235. 30 * 24 * 60 * 60 => 'month',
  236. 24 * 60 * 60 => 'day',
  237. 60 * 60 => 'hour',
  238. 60 => 'minute',
  239. 1 => 'second'
  240. );
  241. foreach( $condition as $secs => $str )
  242. {
  243. $d = $estimate_time / $secs;
  244. if( $d >= 1 )
  245. {
  246. $r = round( $d );
  247. return $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . '';
  248. }
  249. }
  250. }
  251. }