<commit_before><?php
namespace Ojs\SiteBundle\Twig;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ORM\EntityManagerInterface;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Ojs\Common\Services\OrcidService;
use Ojs\UserBundle\Entity\User;
use Symfony\Component\HttpFoundation\Request;

class CommonExtension extends \Twig_Extension
{

    /**
     * @var EntityManagerInterface
     */
    private $em;

    /**
     * @var DocumentManager
     */
    private $dm;

    /**
     * @var OrcidService
     */
    private $orcidService;

    /**
     * @var FilterManager
     */
    private $filterManager;

    /**
     * @param EntityManagerInterface $entityManager
     * @param DocumentManager        $documentManager
     * @param OrcidService           $orcidService
     * @param FilterManager          $filterManager
     */
    public function __construct(
        EntityManagerInterface $entityManager,
        DocumentManager $documentManager,
        OrcidService $orcidService,
        FilterManager $filterManager
    ) {
        $this->em = $entityManager;
        $this->dm = $documentManager;
        $this->orcidService = $orcidService;
        $this->filterManager = $filterManager;
    }

    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('user', [$this, 'getUserByIdOrUsername']),

        ];
    }

    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('addFilters', [$this, 'addFilters']),
            new \Twig_SimpleFunction('getImageOptions', [$this, 'getImageOptions']),
            new \Twig_SimpleFunction('orcidLoginUrl', [$this, 'orcidLoginUrl']),
        ];
    }

    /**
     * @param $key
     * @param $value
     * @return string
     */
    public function addFilters($key = null, $value = null)
    {
        $request = Request::createFromGlobals();
        $filters = [];
        $filters['institution'] = $request->get('institution') ? $this->parseFilters($request->get('institution')) : [];
        $filters['subject'] = $request->get('subject') ? $this->parseFilters($request->get('subject')) : [];
        if (isset($filters[$key]) && count($filters[$key]) > 4) {
            unset($filters[$key][0]);
        }
        $filters[$key][] = $value;

        return $this->convertToUrl($filters);
    }

    /**
     * @param $filter
     * @return array
     */
    private function parseFilters($filter)
    {
        return explode('|', $filter);
    }

    /**
     * @param  array  $filters
     * @return string
     */
    private function convertToUrl($filters = [])
    {
        $data = [];
        foreach ($filters as $key => $value) {
            if (empty($value)) {
                continue;
            }
            $data[] = "{$key}=".implode('|', $value);
        }

        return implode('&', $data);
    }

    /**
     * @param $entity
     * @param $type
     * @param  null        $filter
     * @return array|mixed
     */
    public function getImageOptions($entity, $type, $filter = null)
    {
        $options = $entity->{'get'.ucfirst($type).'Options'}();
        $options = json_decode($options, true);
        if (
            !$options ||
            ((array_key_exists('height', $options) && $options['height'] < 1) ||
                (array_key_exists('width', $options) && $options['width'] < 1))
        ) {
            $defaultConfig = [
                'height' => 0,
                'width' => 0,
                'x' => 0,
                'y' => 0,
            ];

            if ($filter) {<commit_msg>fixed #760<commit_after><?php
namespace Ojs\SiteBundle\Twig;

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ORM\EntityManagerInterface;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Ojs\Common\Services\OrcidService;
use Ojs\UserBundle\Entity\User;
use Symfony\Component\HttpFoundation\Request;

class CommonExtension extends \Twig_Extension
{

    /**
     * @var EntityManagerInterface
     */
    private $em;

    /**
     * @var DocumentManager
     */
    private $dm;

    /**
     * @var OrcidService
     */
    private $orcidService;

    /**
     * @var FilterManager
     */
    private $filterManager;

    /**
     * @param EntityManagerInterface $entityManager
     * @param DocumentManager        $documentManager
     * @param OrcidService           $orcidService
     * @param FilterManager          $filterManager
     */
    public function __construct(
        EntityManagerInterface $entityManager,
        DocumentManager $documentManager,
        OrcidService $orcidService,
        FilterManager $filterManager
    ) {
        $this->em = $entityManager;
        $this->dm = $documentManager;
        $this->orcidService = $orcidService;
        $this->filterManager = $filterManager;
    }

    /**
     * @return array
     */
    public function getFilters()
    {
        return [
            new \Twig_SimpleFilter('user', [$this, 'getUserByIdOrUsername']),

        ];
    }

    /**
     * @return array
     */
    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('getImageOptions', [$this, 'getImageOptions']),
            new \Twig_SimpleFunction('orcidLoginUrl', [$this, 'orcidLoginUrl']),
        ];
    }



    /**
     * @param $entity
     * @param $type
     * @param  null        $filter
     * @return array|mixed
     */
    public function getImageOptions($entity, $type, $filter = null)
    {
        $options = $entity->{'get'.ucfirst($type).'Options'}();
        $options = json_decode($options, true);
        if (
            !$options ||
            ((array_key_exists('height', $options) && $options['height'] < 1) ||
                (array_key_exists('width', $options) && $options['width'] < 1))
        ) {
            $defaultConfig = [
                'height' => 0,
                'width' => 0,
                'x' => 0,
                'y' => 0,
            ];

            if ($filter) {
{'fraction_non_alphanumeric': 0.14245204336947456, 'fraction_numerical': 0.002835696413678065, 'mean_word_length': 2.046747967479675, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 30, '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': '3390007', 'n_tokens_mistral': 1765, 'n_tokens_neox': 1714, 'n_words': 326}