src/Package/Openform/Twig/CustomExtensions.php line 113

Open in your IDE?
  1. <?php
  2. namespace App\Package\Openform\Twig;
  3. use Twig\TwigFilter;
  4. use Twig\Extension\AbstractExtension;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. class CustomExtensions extends AbstractExtension
  8. {
  9.     private $translator;
  10.     private $em;
  11.     private $domain;
  12.     public function __construct(EntityManagerInterface $emTranslatorInterface $translator)
  13.     {
  14.         $this->translator $translator;
  15.         $this->em $em;
  16.         $this->domain 'openform_front';
  17.     }
  18.     public function getFilters()
  19.     {
  20.         return array(
  21.             new TwigFilter('fileInfo', array($this'fileInfoFilter')),
  22.             new TwigFilter('varietyTransChanger', array($this'varietyTransChangerFilter')),
  23.             new TwigFilter('explodedDate', array($this'explodedDateFilter')),
  24.             new TwigFilter('composeDateEvent', array($this'composeDateEventFilter')),
  25.             new TwigFilter('dayName', array($this'dayNameFilter')),
  26.             new TwigFilter('parsePhone', array($this'parsePhoneFilter')),
  27.             new TwigFilter('getClassName', array($this'getClassNameFilter')),
  28.             new TwigFilter('tomd5', array($this'tomd5Func')),
  29.             new TwigFilter('extoev', array($this'ex2evFilter')),
  30.             new TwigFilter('emailtoat', array($this'email2atFilter')),
  31.             new TwigFilter('price', array($this'priceFilter')),
  32.             new TwigFilter('jsonDecode', array($this'jsonDecodeFilter')),
  33.             new TwigFilter('filterVisibles', array($this'filterVisiblesFilter')),
  34.         );
  35.     }
  36.     
  37.     public function filterVisiblesFilter($entities$locale)
  38.     {
  39.         $res = [];
  40.         foreach ($entities as $entity) {
  41.             if (!$entity->getStat()) {
  42.                 continue;
  43.             }
  44.             $res [] = $entity;
  45.         }
  46.         return $res;
  47.     }
  48.     public function fileInfoFilter($filePath
  49.     {
  50.         $filePath __WEB_DIR__ $filePath;
  51.         if (!file_exists($filePath)) {
  52.             return '';
  53.         }
  54.         $size filesize($filePath);
  55.         $units = array('B''kB''MB''GB''TB');
  56.         $u 0;
  57.         while ((round($size 1024) > 0) && ($u 4)) {
  58.             $size $size 1024;
  59.             $u++;
  60.         }        
  61.         $type strtoupper(pathinfo($filePath)['extension']);
  62.         return $type ' ' . (number_format($size0) . " " $units[$u]);
  63.     }
  64.     public function jsonDecodeFilter($str)
  65.     {
  66.         return \json_decode($str);
  67.     }
  68.     public function varietyTransChangerFilter (int $number$transA$transB$transC
  69.     {
  70.         if ($number == 0) {
  71.             return $transC;
  72.         }
  73.         $lastDigit intval(substr($numberstrlen($number) - 11)); 
  74.         if ($lastDigit == 1) {
  75.             return $transA;
  76.         }
  77.         if ($lastDigit >= && $lastDigit <= 4) {
  78.             return $transB;
  79.         }
  80.         return $transC;
  81.     }
  82.     public function explodedDateFilter (?\DateTime $datestring $locale='pl'
  83.     {
  84.         $localeStrArr = [
  85.             'pl' => 'pl_PL.UTF-8',
  86.             'en' => 'en_US'# na nazwie jakby en_EN nie działało, stąd en_US
  87.         ];
  88.         
  89.         setlocale(LC_TIME$localeStrArr[$locale]);
  90.         $arr = [
  91.             'y' => $date->format('Y'),                       // 2020
  92.             'm' => $date->format('m'),                       // 01
  93.             'M' => (string) intval($date->format('m')),      // 1
  94.             'd' => $date->format('d'),                       // 04
  95.             'D' => (string) intval($date->format('d')),      // 4
  96.             'month' => strftime('%B'$date->format('U')),   // stycznia
  97.             'Month' => strftime('%OB'$date->format('U')),  // styczeń
  98.             'day' => strftime('%A'$date->format('U')),     // środa
  99.             'H' => $date->format('H'),                       // 23
  100.             'i' => $date->format('i'),                       // 59
  101.             's' => $date->format('s'),                       // 59
  102.         ];
  103.                 
  104.         return $arr;
  105.     }
  106.     public function dayNameFilter(\DateTime $datestring $locale): string
  107.     {
  108.         return $this->getDayName(date('w'strtotime($date->format('Y-m-d'))), $locale);
  109.     }
  110.     /**
  111.      * Zwraca tylko numer telefonu, w przypadku gdy mamy np 660-981-700 wewn. 303 ==> 660-981-700
  112.      *
  113.      * @param string $phone
  114.      * @return string
  115.      */
  116.     public function parsePhoneFilter(?string $phone): string
  117.     {
  118.         if ($phone) {
  119.             $phone explode(','$phone2);
  120.             return str_replace([' ''-''.'], ''$phone[0]);
  121.         }
  122.         return '';
  123.     }
  124.     /**
  125.      * Zwraca tlumaczenie miesiaca
  126.      *
  127.      * @param integer $month
  128.      * @param string $locale
  129.      * @return string
  130.      */
  131.     public function getMonthName(int $monthstring $locale): string
  132.     {
  133.         return $this->translator->trans('T_MONTH_NAME_' $month, [], $this->domain$locale);
  134.     }
  135.     /**
  136.      * Zwraca tlumaczenie dnia
  137.      *
  138.      * @param integer $day
  139.      * @param string $locale
  140.      * @return string
  141.      */
  142.     public function getDayName(int $daystring $locale): string
  143.     {
  144.         return $this->translator->trans('T_DAY_NAME_' $day, [], $this->domain$locale);
  145.     }
  146.     public function getClassNameFilter($class)
  147.     {
  148.         return pathinfo(str_replace('\\''/'get_class($class)), PATHINFO_FILENAME);
  149.     }
  150.     public function tomd5Func($str)
  151.     {
  152.         if (trim($str) != '') return md5(trim($str));
  153.         return '';
  154.     }
  155.     public function email2atFilter($email)
  156.     {
  157.         return str_replace('@''<span class="at-symbol"></span>'$email);
  158.     }
  159.     /**
  160.      * Return formatted price
  161.      * @param float $price
  162.      * @param string $locale
  163.      * @param integer $divide
  164.      * @return string
  165.      */
  166.     public function priceFilter(float $pricestring $localeint $divide 1): string
  167.     {
  168.         $sep '.';
  169.         if ($locale === 'pl') {
  170.             $sep ',';
  171.         }
  172.         return \number_format($price $divide2$sep'');
  173.     }
  174. }