src/Twig/AppExtension.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * @Author: stephan <m6ahenina@gmail.com>
  4.  * @Date:   2019-11-21 12:57:41
  5.  * @Last Modified by:   stephan <m6ahenina@gmail.com>
  6.  * @Last Modified time: 2020-02-07 14:09:26
  7.  */
  8. namespace App\Twig;
  9. use App\Entity\{Company,User};
  10. use App\Utils\Resolver;
  11. use IntlDateFormatter;
  12. use Symfony\Component\Asset\Packages;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Security\Core\Security;
  15. use Twig\Extension\AbstractExtension;
  16. use Twig\TwigFilter;
  17. use Twig\TwigFunction;
  18. use Vich\UploaderBundle\Storage\StorageInterface;
  19. class AppExtension extends AbstractExtension
  20. {
  21.     public function __construct(StorageInterface $storageSecurity $securityPackages $packagesRequestStack $requestStack$defaultLocale)
  22.     {
  23.         $this->storage $storage;
  24.         $this->security $security;
  25.         $this->packages $packages;
  26.         $this->requestStack $requestStack;
  27.         $this->defaultLocale $defaultLocale;
  28.     }
  29.     public function getFilters(): array
  30.     {
  31.         return [
  32.             new TwigFilter('file_size', [$this'fileSize']),
  33.             new TwigFilter('intl_date', [$this'intlDate']),
  34.         ];
  35.     }
  36.     public function getFunctions(): array
  37.     {
  38.         return [
  39.             new TwigFunction('user_avatar', [$this'userAvatar']),
  40.             new TwigFunction('company_logo', [$this'companyLogo']),
  41.             new TwigFunction('user_cover', [$this'userCover']),
  42.             new TwigFunction('resolve', [$this'resolve']),
  43.             new TwigFunction('intl_date', [$this'intlDate']),
  44.             new TwigFunction('assoc_array', [$this'assocArray']),
  45.         ];
  46.     }
  47.     public function resolve($path$defaultValue null)
  48.     {
  49.         return Resolver::resolve($path$defaultValue);
  50.     }
  51.     public function fileSize($size)
  52.     {
  53.         if (!$size || !is_numeric($size)) return $size;
  54.         $size = (int) $size;
  55.         $exts = ['B''KB''MB''GB'];
  56.         $dept 0;
  57.         for ($i 0$i count($exts); $i++) {
  58.             $dept $i;
  59.             if ($size >= 1024*1.5) {
  60.                 $size $size 1024;
  61.             } else {
  62.                 break;
  63.             }
  64.         }
  65.         return number_format($size$dept 0',''.') . ' ' $exts[$dept];
  66.     }
  67.     /**
  68.      * Return the given user photo(avatar|cover)'s uri 
  69.      * If no user is provided, return the current user's photo'url
  70.      *
  71.      * @param User $user
  72.      * @return string User photo url
  73.      */
  74.     public function getUserPhoto(?User $user null$prop=''$uploadableField=''$defaultValue=''$packageName 'images')
  75.     {
  76.         if ($user) {
  77.             if (Resolver::resolve([$user$prop], null)) {
  78.                 return $this->storage->resolveUri($user$uploadableFieldUser::class);
  79.             }
  80.         } else if ($user == $this->security->getUser()) {
  81.             if (Resolver::resolve([$user$prop], null)) {
  82.                 return $this->storage->resolveUri($user$uploadableFieldUser::class);
  83.             }
  84.         }
  85.         return $this->packages->getUrl($defaultValue$packageName);
  86.     }
  87.     public function getCompanyLogo(?Company $company null$prop null$uploadableField null$defaultValue null$packageName 'images'): string
  88.     {
  89.         if ($company) {
  90.             if (Resolver::resolve([$company$prop], null)) {
  91.                 return $this->storage->resolveUri($company$uploadableFieldCompany::class);
  92.             }
  93.         } /*else if ($company = $this->security->getUser()->getCompany()) {
  94.             if (Resolver::resolve([$company, $prop], null)) {
  95.                 return $this->storage->resolveUri($company, $uploadableField, Company::class);
  96.             }
  97.         }*/
  98.         return '';
  99.     }
  100.     /**
  101.      * Return the given user avatar's url
  102.      * If no user is provided, return the current user's avatar'url
  103.      *
  104.      * @param User $user
  105.      * @return string User avatar url
  106.      */
  107.     public function userAvatar(?User $user null): string
  108.     {   
  109.         $userPhoto $this->getUserPhoto($user'avatar''avatarFile''user.png''images');
  110.         if(!empty($user)){
  111.             if(!empty($user->getAvatar())){
  112.                 return "/images/avatar/".$user->getAvatar();
  113.             }
  114.         }
  115.         if(empty($userPhoto)){
  116.             return "/images/user.png";
  117.         }
  118.         return $this->getUserPhoto($user'avatar''avatarFile''user.png''images');
  119.     }
  120.       /**
  121.      * Return the given company logo's url
  122.      * If no company is provided, return the current company's logo'url
  123.      *
  124.      * @param Company $company
  125.      * @return string Company logo url
  126.      */
  127.     public function companyLogo(?Company $company null): string
  128.     {
  129.        return $this->getCompanyLogo($company'logo''logoFile''user.png''images');
  130.     }
  131.     public function userCover(?User $user null): string
  132.     {
  133.         return $this->getUserPhoto($user'cover''coverFile''user_cover.png''images');
  134.     }
  135.     public function intlDate(\DateTime $date null$pattern "d/m/Y"$locale null)
  136.     {
  137.         if (is_null($date)) {
  138.             return '';
  139.         }
  140.         if (is_null($locale)) {
  141.             $request $this->requestStack->getCurrentRequest();
  142.             $locale Resolver::resolve([$request'locale'], $this->defaultLocale);
  143.         }
  144.         $formatter = new IntlDateFormatter($localeIntlDateFormatter::FULLIntlDateFormatter::FULL);
  145.         $formatter->setPattern($pattern);
  146.         
  147.         return ucwords($formatter->format($date));
  148.     }
  149.     public function assocArray(string $key$value, array $source = []): ?array
  150.     {
  151.         $dest $source;
  152.         $dest[$key] = $value;
  153.         return $dest;
  154.     }
  155. }