<?php
namespace Boab\CmsBundle\Twig\Extension;
use Boab\CmsBundle\Model\ContentTypeManagerInterface;
use Boab\CmsBundle\Model\TypeManagerInterface;
use Symfony\Component\Intl\Intl;
use Boab\CmsBundle\Util\UtilCommon;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class BoabCmsExtension extends AbstractExtension
{
use UtilCommon;
public function __construct(ContentTypeManagerInterface $contentTypeManager)
{
$this->contentTypeManager = $contentTypeManager;
}
public function getFilters():array
{
return [
new \Twig\TwigFilter('country', array($this, 'countryFilter')),
];
}
public function getFunctions():array
{
return [
new TwigFunction('status_option', [$this, 'statusOption'], ['is_safe' => ['html']]),
new TwigFunction('status_style', [$this, 'status']),
new TwigFunction('currency', [$this, 'formatNumber'], ['is_safe' => ['html']]),
new TwigFunction('percent', [$this, 'calculatePercent'], ['is_safe' => ['html']]),
new TwigFunction('content_types', [$this, 'contentTypes'], ['is_safe' => ['html']]),
];
}
public function contentTypes()
{
$typeManagers = $this->contentTypeManager->getContentTypes();
$contentTypes = array_filter($typeManagers,function($type){
return $type instanceof TypeManagerInterface;
});
return $contentTypes;
}
public function formatNumber($amount, $symbol="$")
{
return $this->currency($amount, $symbol);
}
public function calculatePercent($target, $contrib)
{
return $this->round_up(($contrib/$target)*100,2) ;
}
public function countryFilter($value)
{
return Intl::getRegionBundle()->getCountryName($value);
}
private function round_up ($value, $places=0) {
if ($places < 0) { $places = 0; }
$mult = pow(10, $places);
return ceil($value * $mult) / $mult;
}
}