| Current Path : /var/www/v3.cesa.co.za/src/CoreBundle/Twig/ |
| Current File : /var/www/v3.cesa.co.za/src/CoreBundle/Twig/CoreExtension.php |
<?php
namespace App\CoreBundle\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use App\CoreBundle\Services\Utils;
class CoreExtension extends AbstractExtension
{
private $container = null;
private $securityContext = null;
/**
* @return null
*/
public function __construct(ContainerInterface $container, TokenStorageInterface $securityContext)
{
$this->container = $container;
$this->securityContext = $securityContext;
}
public function getFunctions(): array
{
return array(
new TwigFunction('isObject', array($this, 'isObject')),
new TwigFunction('getUserString', array($this, 'getUserString')),
);
}
public function getFilters(): array
{
return array(
new TwigFilter('format_currency', array($this, 'formatCurrency')),
new TwigFilter('format_number', array($this, 'formatNumber')),
new TwigFilter('get_parameter', array($this, 'getParameter')),
new TwigFilter('get_setting', array($this, 'getSetting')),
new TwigFilter('id_string', array($this, 'idString')),
new TwigFilter('phrase', array($this, 'getPhrase')),
new TwigFilter('urldecode', array($this, 'urlDecode')),
new TwigFilter('yes_no', array($this, 'yesNo')),
new TwigFilter('json_decode', array($this, 'jsonDecode')),
);
}
public function formatCurrency($value)
{
if (is_null($value) || !is_numeric($value)) {
$value = 0;
}
return 'R ' . number_format($value, 2, '.', ' ');
}
public function formatNumber($value, $decimals = 0)
{
return number_format($value, $decimals, '.', ' ');
}
public function getParameter($value)
{
return $this->container->getParameter($value);
}
/**
* Get phrase
* @return string
*/
public function getPhrase($phrase)
{
return $phrase;
}
public function getSetting($settingName)
{
return $this->container->get('system_setting.manager')->getSetting($settingName);
}
/**
* Get user name, surname and username
* @param string $userListId
* @return string
*/
public function getUserString($userListId)
{
$userList = $this->container->get('user_list.manager')->find($userListId);
if (is_object($userList)) {
return $userList->getFullNameString() . ' (' . $userList->getUsername() . ') - ' . $userList->getEMailAddressString();
}
return '';
}
public function idString($value)
{
return Utils::getIdString($value);
}
/**
* Is Object
* @return boolean
*/
public function isObject($var)
{
return is_object($var);
}
public function urlDecode($strIn)
{
return urldecode($strIn);
}
public function yesNo($value)
{
$languagePhrasesManager = $this->container->get('language_phrases.manager');
if ($value > 0) {
return $languagePhrasesManager->translate('Yes');
}
return $languagePhrasesManager->translate('No');
}
public function jsonDecode($value)
{
return json_decode($value, true);
}
}