<commit_before><?php
/*
 * This file is part of the CSBill package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CSBill\CoreBundle\Twig\Extension;

use CSBill\CoreBundle\CSBillCoreBundle;
use Twig_Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;

class GlobalExtension extends Twig_Extension
{
    /**
     * @var ContainerInterface $container
     */
    protected $container;

    /**
     * Sets the container
     *
     * @param ContainerInterface $container
     */
    public function setContainer(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * Get global twig variables
     *
     * @return array
     */
    public function getGlobals()
    {
        return array(
                    'query'            => $this->getQuery(),
                    'currency'         => $this->container->get('currency'),
                    'settings'         => $this->container->get('settings')->getSettings(),
                    'invoice_manager'  => $this->container->get('invoice.manager'),
                    'app_version'      => CSBillCoreBundle::VERSION,
            );
    }

    /**
     * (non-PHPdoc)
     * @see Twig_Extension::getFilters()
     */
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('percentage', array($this, 'formatPercentage')),
            new \Twig_SimpleFilter('currency', array($this, 'formatCurrency'))
        );
    }

    /**
     * (non-PHPDoc)
     * @return array
     */
    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('icon', array($this, 'displayIcon'), array('is_safe' => array('html')))
        );
    }

    /**
     * Displays an icon
     *
     * @param string $iconName
     * @param array $options
     * @return string
     */
    public function displayIcon($iconName, array $options = array())
    {
        $options = implode('-', $options);
        $class = sprintf('fa fa-%s', $iconName);

        if (!empty($options)) {
            $class .= '-' . $options;
        }

        return sprintf('<i class="%s"></i>', $class);
    }

    /**
     * @param  int|float $amount
     * @return string
     */
    protected function formatCurrency($amount)
    {
        return $this->container->get('currency')->format($amount);
    }

    /**
     * @param  int|float $amount
     * @param  int       $percentage
     * @return float|int
     */
    protected function formatPercentage($amount, $percentage = 0)
    {
        if ($percentage > 0) {
            return ($amount * $percentage) / 100;
        }

        return 0;
    }

    /**
     * Get the url query
     *
     * @throws InactiveScopeException
     * @return array
     */<commit_msg>[CoreBundle] Fixed visibility for formatCurreny twig filter<commit_after><?php
/*
 * This file is part of the CSBill package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CSBill\CoreBundle\Twig\Extension;

use CSBill\CoreBundle\CSBillCoreBundle;
use Twig_Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;

class GlobalExtension extends Twig_Extension
{
    /**
     * @var ContainerInterface $container
     */
    protected $container;

    /**
     * Sets the container
     *
     * @param ContainerInterface $container
     */
    public function setContainer(ContainerInterface $container)
    {
        $this->container = $container;
    }

    /**
     * Get global twig variables
     *
     * @return array
     */
    public function getGlobals()
    {
        return array(
                    'query'            => $this->getQuery(),
                    'currency'         => $this->container->get('currency'),
                    'settings'         => $this->container->get('settings')->getSettings(),
                    'invoice_manager'  => $this->container->get('invoice.manager'),
                    'app_version'      => CSBillCoreBundle::VERSION,
            );
    }

    /**
     * (non-PHPdoc)
     * @see Twig_Extension::getFilters()
     */
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('percentage', array($this, 'formatPercentage')),
            new \Twig_SimpleFilter('currency', array($this, 'formatCurrency'))
        );
    }

    /**
     * (non-PHPDoc)
     * @return array
     */
    public function getFunctions()
    {
        return array(
            new \Twig_SimpleFunction('icon', array($this, 'displayIcon'), array('is_safe' => array('html')))
        );
    }

    /**
     * Displays an icon
     *
     * @param string $iconName
     * @param array $options
     * @return string
     */
    public function displayIcon($iconName, array $options = array())
    {
        $options = implode('-', $options);
        $class = sprintf('fa fa-%s', $iconName);

        if (!empty($options)) {
            $class .= '-' . $options;
        }

        return sprintf('<i class="%s"></i>', $class);
    }

    /**
     * @param  int|float $amount
     * @return string
     */
    public function formatCurrency($amount)
    {
        return $this->container->get('currency')->format($amount);
    }

    /**
     * @param  int|float $amount
     * @param  int       $percentage
     * @return float|int
     */
    protected function formatPercentage($amount, $percentage = 0)
    {
        if ($percentage > 0) {
            return ($amount * $percentage) / 100;
        }

        return 0;
    }

    /**
     * Get the url query
     *
     * @throws InactiveScopeException
     * @return array
     */
{'fraction_non_alphanumeric': 0.13397767038826863, 'fraction_numerical': 0.0019996667222129645, 'mean_word_length': 2.045154743784881, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6342621', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1668, 'n_words': 376}