lib/boab/cms-bundle/src/Twig/Extension/BoabCmsExtension.php line 16

Open in your IDE?
  1. <?php
  2. namespace Boab\CmsBundle\Twig\Extension;
  3. use Boab\CmsBundle\Model\ContentTypeManagerInterface;
  4. use Boab\CmsBundle\Model\TypeManagerInterface;
  5. use Symfony\Component\Intl\Intl;
  6. use Boab\CmsBundle\Util\UtilCommon;
  7. use Twig\Extension\AbstractExtension;
  8. use Twig\TwigFunction;
  9. class BoabCmsExtension extends AbstractExtension
  10. {
  11.     use UtilCommon;
  12.     public function __construct(ContentTypeManagerInterface $contentTypeManager)
  13.     {
  14.         $this->contentTypeManager $contentTypeManager;
  15.     }
  16.     public function getFilters():array
  17.     {
  18.         return [
  19.             new \Twig\TwigFilter('country', array($this'countryFilter')),
  20.         ];
  21.     }    
  22.     public function getFunctions():array
  23.     {
  24.         return [
  25.             new TwigFunction('status_option', [$this'statusOption'], ['is_safe' => ['html']]),
  26.             new TwigFunction('status_style', [$this'status']),
  27.             new TwigFunction('currency', [$this'formatNumber'], ['is_safe' => ['html']]),
  28.             new TwigFunction('percent', [$this'calculatePercent'], ['is_safe' => ['html']]),
  29.             new TwigFunction('content_types', [$this'contentTypes'], ['is_safe' => ['html']]),
  30.         ];
  31.     }
  32.     public function contentTypes()
  33.     {
  34.         $typeManagers $this->contentTypeManager->getContentTypes();        
  35.         $contentTypes array_filter($typeManagers,function($type){
  36.             return $type instanceof TypeManagerInterface;
  37.         }); 
  38.         
  39.         return $contentTypes;
  40.     }
  41.     public function formatNumber($amount$symbol="$")
  42.     {
  43.         return $this->currency($amount$symbol);
  44.     }
  45.     public function calculatePercent($target$contrib)
  46.     {
  47.         return $this->round_up(($contrib/$target)*100,2) ;
  48.     }
  49.     public function countryFilter($value)
  50.     {
  51.         return Intl::getRegionBundle()->getCountryName($value);
  52.     }    
  53.     private function round_up ($value$places=0) {
  54.         if ($places 0) { $places 0; }
  55.         $mult pow(10$places);
  56.         return ceil($value $mult) / $mult;
  57.     }    
  58. }