<commit_before><?php

/*
 * This file is part of the Search PHP Library.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Feel free to edit as you please, and have fun.
 *
 * @author <NAME> <<EMAIL>>
 * @author PuntMig Technologies
 */

declare(strict_types=1);

namespace Puntmig\Search\Url;

use Symfony\Component\Routing\RouterInterface;

use Puntmig\Search\Query\Filter;
use Puntmig\Search\Query\SortBy;
use Puntmig\Search\Result\Result;

/**
 * Class UrlBuilder.
 */
class UrlBuilder
{
    /**
     * Routes dictionary.
     */
    private $routesDictionary = [];

    /**
     * @var RouterInterface
     *
     * Router
     */
    private $router;

    /**
     * @var array
     *
     * Routes cache
     */
    private $routesCache = [];

    /**
     * UrlBuilder constructor.
     *
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    /**
     * Set routes dictionary.
     *
     * @param array $routesDictionary
     */
    public function setRoutesDictionary(array $routesDictionary)
    {
        $this->routesDictionary = $routesDictionary;
    }

    /**
     * Add filter into query.
     *
     * @param Result $result
     * @param string $filterName
     * @param string $value
     *
     * @return string
     */
    public function addFilterValue(
        Result $result,
        string $filterName,
        string $value
    ) : string {
        if (isset($this->routesCache[spl_object_hash($result)][$filterName])) {
            return str_replace(
                ['{id}', '{slug}'],
                $result->getAggregation($filterName)->getCounter($value)->getValues(),
                $this->routesCache[spl_object_hash($result)][$filterName]
            );
        }

        $urlParameters = $this->generateQueryUrlParameters($result, $filterName);
        if (
            !isset($urlParameters[$filterName]) ||
            !in_array($value, $urlParameters[$filterName])
        ) {
            $urlParameters[$filterName][] = $value;
        }

        $urlElements = $this->createUrlByUrlParameters(
            $result,
            $urlParameters
        );

        $templateRoute = $urlElements['template_path'];
        if ($templateRoute === $this->routesDictionary['main']) {
            $templateRoute = str_replace(
                "{$filterName}[]=$value",
                "{$filterName}[]={id}",
                $templateRoute
            );
        }
        $this->routesCache[spl_object_hash($result)][$filterName] = $templateRoute;
        var_dump($this->routesCache);

        return $urlElements['route'];
    }

    /**
     * Remove filter from query.
     *
     * @param Result $result
     * @param string $filterName
     * @param string $value
     *
     * @return string
     */
    public function removeFilterValue(
        Result $result,
        string $filterName,
        string $value = null
    ) : string {<commit_msg>Removed var_dump<commit_after><?php

/*
 * This file is part of the Search PHP Library.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Feel free to edit as you please, and have fun.
 *
 * @author <NAME> <<EMAIL>>
 * @author PuntMig Technologies
 */

declare(strict_types=1);

namespace Puntmig\Search\Url;

use Symfony\Component\Routing\RouterInterface;

use Puntmig\Search\Query\Filter;
use Puntmig\Search\Query\SortBy;
use Puntmig\Search\Result\Result;

/**
 * Class UrlBuilder.
 */
class UrlBuilder
{
    /**
     * Routes dictionary.
     */
    private $routesDictionary = [];

    /**
     * @var RouterInterface
     *
     * Router
     */
    private $router;

    /**
     * @var array
     *
     * Routes cache
     */
    private $routesCache = [];

    /**
     * UrlBuilder constructor.
     *
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    /**
     * Set routes dictionary.
     *
     * @param array $routesDictionary
     */
    public function setRoutesDictionary(array $routesDictionary)
    {
        $this->routesDictionary = $routesDictionary;
    }

    /**
     * Add filter into query.
     *
     * @param Result $result
     * @param string $filterName
     * @param string $value
     *
     * @return string
     */
    public function addFilterValue(
        Result $result,
        string $filterName,
        string $value
    ) : string {
        if (isset($this->routesCache[spl_object_hash($result)][$filterName])) {
            return str_replace(
                ['{id}', '{slug}'],
                $result->getAggregation($filterName)->getCounter($value)->getValues(),
                $this->routesCache[spl_object_hash($result)][$filterName]
            );
        }

        $urlParameters = $this->generateQueryUrlParameters($result, $filterName);
        if (
            !isset($urlParameters[$filterName]) ||
            !in_array($value, $urlParameters[$filterName])
        ) {
            $urlParameters[$filterName][] = $value;
        }

        $urlElements = $this->createUrlByUrlParameters(
            $result,
            $urlParameters
        );

        $templateRoute = $urlElements['template_path'];
        if ($templateRoute === $this->routesDictionary['main']) {
            $templateRoute = str_replace(
                "{$filterName}[]=$value",
                "{$filterName}[]={id}",
                $templateRoute
            );
        }
        $this->routesCache[spl_object_hash($result)][$filterName] = $templateRoute;

        return $urlElements['route'];
    }

    /**
     * Remove filter from query.
     *
     * @param Result $result
     * @param string $filterName
     * @param string $value
     *
     * @return string
     */
    public function removeFilterValue(
        Result $result,
        string $filterName,
        string $value = null
    ) : string {
{'fraction_non_alphanumeric': 0.12681810753391076, 'fraction_numerical': 0.0003268507926131721, 'mean_word_length': 2.097165991902834, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 32, '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': '871750', 'n_tokens_mistral': 1742, 'n_tokens_neox': 1734, 'n_words': 370}