PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/AppBundle/Twig/TwigHelperExtension.php

https://gitlab.com/aredhel-bazaar/la-taverne-du-hasard
PHP | 211 lines | 138 code | 36 blank | 37 comment | 24 complexity | d21f004d7f7b8cf80643b0eebd513534 MD5 | raw file
  1. <?php
  2. namespace AppBundle\Twig;
  3. use AppBundle\Service\Slugger;
  4. class TwigHelperExtension extends \Twig_Extension
  5. {
  6. /**
  7. * @var
  8. */
  9. private $locale;
  10. /**
  11. * @var \IntlDateFormatter
  12. */
  13. private $dateFormatter;
  14. /**
  15. * @var Slugger
  16. */
  17. private $slugger;
  18. public function __construct($locale, Slugger $slugger)
  19. {
  20. $this->locale = $locale;
  21. $this->dateFormatter = \IntlDateFormatter::create(
  22. $this->locale,
  23. \IntlDateFormatter::LONG,
  24. \IntlDateFormatter::LONG
  25. );
  26. $this->slugger = $slugger;
  27. }
  28. /**
  29. * @return array
  30. */
  31. public function getFilters()
  32. {
  33. return array(
  34. new \Twig_SimpleFilter('aging', array($this, 'getAging')),
  35. new \Twig_SimpleFilter('strftime', array($this, 'strftime')),
  36. new \Twig_SimpleFilter('intval', array($this, 'intval')),
  37. new \Twig_SimpleFilter('slug', array($this, 'slugify'))
  38. );
  39. }
  40. /**
  41. * @param \DateTime $dateTimeOld
  42. * @param \DateTime|null $dateTimeNew
  43. *
  44. * @return string
  45. */
  46. public function getAging(\DateTime $dateTimeOld, \DateTime $dateTimeNew=null)
  47. {
  48. if( is_null($dateTimeNew) ) {
  49. $dateTimeNew = new \DateTime();
  50. }
  51. $DateInterval = $dateTimeOld->diff($dateTimeNew, true);
  52. $str = [];
  53. if( $DateInterval->y != 0 ) {
  54. if( !isset($show) ) {
  55. $show = [
  56. 'y' => TRUE,
  57. 'm' => FALSE,
  58. 'd' => FALSE,
  59. 'h' => FALSE,
  60. 'i' => FALSE,
  61. 's' => FALSE
  62. ];
  63. }
  64. $str[] = $DateInterval->y." an".($DateInterval->y > 1 ? "s":"");
  65. }
  66. if( $DateInterval->m != 0 ) {
  67. if( !isset($show) ) {
  68. $show = [
  69. 'y' => FALSE,
  70. 'm' => TRUE,
  71. 'd' => FALSE,
  72. 'h' => FALSE,
  73. 'i' => FALSE,
  74. 's' => FALSE
  75. ];
  76. }
  77. if( $show['m'] ) {
  78. $str[] = $DateInterval->m." mois";
  79. }
  80. }
  81. if( $DateInterval->d != 0 ) {
  82. if( !isset($show) ) {
  83. $show = [
  84. 'y' => FALSE,
  85. 'm' => FALSE,
  86. 'd' => TRUE,
  87. 'h' => FALSE,
  88. 'i' => FALSE,
  89. 's' => FALSE
  90. ];
  91. }
  92. if( $show['d'] ) {
  93. $str[] = $DateInterval->d."j";
  94. }
  95. }
  96. if( $DateInterval->h != 0 ) {
  97. if( !isset($show) ) {
  98. $show = [
  99. 'y' => FALSE,
  100. 'm' => FALSE,
  101. 'd' => FALSE,
  102. 'h' => TRUE,
  103. 'i' => FALSE,
  104. 's' => FALSE
  105. ];
  106. }
  107. if( $show['h'] ) {
  108. $str[] = $DateInterval->h."hr".($DateInterval->h > 1 ? "s":"");
  109. }
  110. }
  111. if( $DateInterval->i != 0 ) {
  112. if( !isset($show) ) {
  113. $show = [
  114. 'y' => FALSE,
  115. 'm' => FALSE,
  116. 'd' => FALSE,
  117. 'h' => FALSE,
  118. 'i' => TRUE,
  119. 's' => FALSE
  120. ];
  121. }
  122. if( $show['i'] ) {
  123. $str[] = $DateInterval->i."min";
  124. }
  125. }
  126. if( $DateInterval->s != 0 ) {
  127. if( !isset($show) ) {
  128. $show = [
  129. 'y' => FALSE,
  130. 'm' => FALSE,
  131. 'd' => FALSE,
  132. 'h' => FALSE,
  133. 'i' => FALSE,
  134. 's' => TRUE
  135. ];
  136. }
  137. if( $show['s'] ) {
  138. $str[] = $DateInterval->s."s";
  139. }
  140. }
  141. return implode(' ', $str);
  142. }
  143. /**
  144. * @param \DateTime $dateTime
  145. * @param string $format
  146. *
  147. * @return string
  148. */
  149. public function strftime(\DateTime $dateTime, string $format) {
  150. return $this->dateFormatter->formatObject($dateTime, $format);
  151. }
  152. /**
  153. * @param string $value
  154. *
  155. * @return int
  156. */
  157. public function intval(string $value) {
  158. return intval($value);
  159. }
  160. /**
  161. * @param string $value
  162. *
  163. * @return string
  164. */
  165. public function slugify(string $value) {
  166. return $this->slugger->slugify($value);
  167. }
  168. /**
  169. * @return string
  170. */
  171. public function getName()
  172. {
  173. return 'twig_helper_extension';
  174. }
  175. }