<?php
namespace App\Package\Openform\Twig;
use Twig\TwigFilter;
use Twig\Extension\AbstractExtension;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class CustomExtensions extends AbstractExtension
{
private $translator;
private $em;
private $domain;
public function __construct(EntityManagerInterface $em, TranslatorInterface $translator)
{
$this->translator = $translator;
$this->em = $em;
$this->domain = 'openform_front';
}
public function getFilters()
{
return array(
new TwigFilter('fileInfo', array($this, 'fileInfoFilter')),
new TwigFilter('varietyTransChanger', array($this, 'varietyTransChangerFilter')),
new TwigFilter('explodedDate', array($this, 'explodedDateFilter')),
new TwigFilter('composeDateEvent', array($this, 'composeDateEventFilter')),
new TwigFilter('dayName', array($this, 'dayNameFilter')),
new TwigFilter('parsePhone', array($this, 'parsePhoneFilter')),
new TwigFilter('getClassName', array($this, 'getClassNameFilter')),
new TwigFilter('tomd5', array($this, 'tomd5Func')),
new TwigFilter('extoev', array($this, 'ex2evFilter')),
new TwigFilter('emailtoat', array($this, 'email2atFilter')),
new TwigFilter('price', array($this, 'priceFilter')),
new TwigFilter('jsonDecode', array($this, 'jsonDecodeFilter')),
new TwigFilter('filterVisibles', array($this, 'filterVisiblesFilter')),
);
}
public function filterVisiblesFilter($entities, $locale)
{
$res = [];
foreach ($entities as $entity) {
if (!$entity->getStat()) {
continue;
}
$res [] = $entity;
}
return $res;
}
public function fileInfoFilter($filePath)
{
$filePath = __WEB_DIR__ . $filePath;
if (!file_exists($filePath)) {
return '';
}
$size = filesize($filePath);
$units = array('B', 'kB', 'MB', 'GB', 'TB');
$u = 0;
while ((round($size / 1024) > 0) && ($u < 4)) {
$size = $size / 1024;
$u++;
}
$type = strtoupper(pathinfo($filePath)['extension']);
return $type . ' ' . (number_format($size, 0) . " " . $units[$u]);
}
public function jsonDecodeFilter($str)
{
return \json_decode($str);
}
public function varietyTransChangerFilter (int $number, $transA, $transB, $transC)
{
if ($number == 0) {
return $transC;
}
$lastDigit = intval(substr($number, strlen($number) - 1, 1));
if ($lastDigit == 1) {
return $transA;
}
if ($lastDigit >= 2 && $lastDigit <= 4) {
return $transB;
}
return $transC;
}
public function explodedDateFilter (?\DateTime $date, string $locale='pl')
{
$localeStrArr = [
'pl' => 'pl_PL.UTF-8',
'en' => 'en_US', # na nazwie jakby en_EN nie działało, stąd en_US
];
setlocale(LC_TIME, $localeStrArr[$locale]);
$arr = [
'y' => $date->format('Y'), // 2020
'm' => $date->format('m'), // 01
'M' => (string) intval($date->format('m')), // 1
'd' => $date->format('d'), // 04
'D' => (string) intval($date->format('d')), // 4
'month' => strftime('%B', $date->format('U')), // stycznia
'Month' => strftime('%OB', $date->format('U')), // styczeń
'day' => strftime('%A', $date->format('U')), // środa
'H' => $date->format('H'), // 23
'i' => $date->format('i'), // 59
's' => $date->format('s'), // 59
];
return $arr;
}
public function dayNameFilter(\DateTime $date, string $locale): string
{
return $this->getDayName(date('w', strtotime($date->format('Y-m-d'))), $locale);
}
/**
* Zwraca tylko numer telefonu, w przypadku gdy mamy np 660-981-700 wewn. 303 ==> 660-981-700
*
* @param string $phone
* @return string
*/
public function parsePhoneFilter(?string $phone): string
{
if ($phone) {
$phone = explode(',', $phone, 2);
return str_replace([' ', '-', '.'], '', $phone[0]);
}
return '';
}
/**
* Zwraca tlumaczenie miesiaca
*
* @param integer $month
* @param string $locale
* @return string
*/
public function getMonthName(int $month, string $locale): string
{
return $this->translator->trans('T_MONTH_NAME_' . $month, [], $this->domain, $locale);
}
/**
* Zwraca tlumaczenie dnia
*
* @param integer $day
* @param string $locale
* @return string
*/
public function getDayName(int $day, string $locale): string
{
return $this->translator->trans('T_DAY_NAME_' . $day, [], $this->domain, $locale);
}
public function getClassNameFilter($class)
{
return pathinfo(str_replace('\\', '/', get_class($class)), PATHINFO_FILENAME);
}
public function tomd5Func($str)
{
if (trim($str) != '') return md5(trim($str));
return '';
}
public function email2atFilter($email)
{
return str_replace('@', '<span class="at-symbol"></span>', $email);
}
/**
* Return formatted price
* @param float $price
* @param string $locale
* @param integer $divide
* @return string
*/
public function priceFilter(float $price, string $locale, int $divide = 1): string
{
$sep = '.';
if ($locale === 'pl') {
$sep = ',';
}
return \number_format($price / $divide, 2, $sep, '');
}
}