<commit_before><?php

namespace Repositories;

use Indoctrinated\Repository;
use Lib\Config;
use Lib\Url;
use Repositories\Routes\Bundle;
use Routes as Entity;

/**
 * Routes
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class Routes extends Repository
{
    protected static $_bundle;

    public static function bundle()
    {
        if (static::$_bundle) {
            return static::$_bundle;
        }

        static::$_bundle = new Bundle();

        return static::$_bundle;
    }

    public static function parse(
        Url $url,
        string $method
    )
    {
        $url_string = (string) $url;
        $params = [];
        $counter = 0;

        $locales = Config::global()
            ->get('common')
            ->get('locales')
            ->toArray();

        $callback = function($matches)
        use (&$params, &$counter) {
            $counter += 1;
            $sprintf_params[] = $matches[2];

            return '%' . $counter . '$s';
        };

        $translation = preg_replace_callback(
            '/(-\()([^)]+)(\)-?)/', $callback, $url_string
        );

        $route = static::_getRoute($translation, $locales);

        if (!$route) {
            return;
        }

        return static::_getUrl($route, $url_string, $method, $locales, $params);
    }

    protected static function _getRoute(
        string $translation,
        array $locales
    )
    {
        $conditions = [];

        foreach ($locales as $locale) {
            $conditions[] = 'route.' . $locale . ' = :translation';
        }

        return Entity::getEntityRepository()
            ->createQueryBuilder('route')
            ->andWhere(implode(' OR ', $conditions))
            ->andWhere('route.archivedAt IS NULL')
            ->setParameters([
                'translation' => $translation
            ])
            ->getQuery()
            ->getOneOrNullResult();
    }

    protected static function _getUrl(
        Entity $route,
        string $requested_url,
        string $method,
        array $locales,
        array $params
    )
    {
        $format = $route->getFormat();
        $resolved_url = Url::parse(sprintf($format, ...$params));

        if ($resolved_url->getMethod() !== $method) {
            return;
        }

        $resolved_url->setFormat($format);
        
        $data = $route->toArray();

        foreach ($locales as $locale) {
            $translation = sprintf($data[$locale], ...$params);

            if ($translation === $requested_url && !$resolved_url->getCanonical()) {
                $resolved_url->setLocale($locale);
                $resolved_url->setCanonical($translation);
            } else {
                $resolved_url->addAlternate($locale, $translation);
            }
        }

        return $resolved_url;
    }
}<commit_msg>Update Routes.php<commit_after><?php

namespace Repositories;

use Indoctrinated\Repository;
use Lib\Config;
use Lib\Url;
use Repositories\Routes\Bundle;
use Routes as Entity;

/**
 * Routes
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class Routes extends Repository
{
    protected static $_bundle;

    public static function bundle()
    {
        if (static::$_bundle) {
            return static::$_bundle;
        }

        static::$_bundle = new Bundle();

        return static::$_bundle;
    }

    public static function parse(
        Url $url,
        string $method
    )
    {
        $url_string = (string) $url;

        $locales = Config::global()
            ->get('common')
            ->get('locales')
            ->toArray();

        list($format, $params) = static::_getFormat($url_string);

        $route = static::_getRoute($format, $locales);

        if (!$route) {
            return;
        }

        return static::_getUrl($route, $url_string, $method, $locales, $params);
    }

    protected static function _getFormat(
        string $url
    )
    {
        $params = [];
        $counter = 0;

        $callback = function($matches)
        use (&$params, &$counter) {
            $counter += 1;
            $sprintf_params[] = $matches[2];

            return '%' . $counter . '$s';
        };

        $format = preg_replace_callback(
            '/(-\()([^)]+)(\)-?)/', $callback, $url
        );

        return [$format, $params];
    }

    protected static function _getRoute(
        string $translation,
        array $locales
    )
    {
        $conditions = [];

        foreach ($locales as $locale) {
            $conditions[] = 'route.' . $locale . ' = :translation';
        }

        return Entity::getEntityRepository()
            ->createQueryBuilder('route')
            ->andWhere(implode(' OR ', $conditions))
            ->andWhere('route.archivedAt IS NULL')
            ->setParameters([
                'translation' => $translation
            ])
            ->getQuery()
            ->getOneOrNullResult();
    }

    protected static function _getUrl(
        Entity $route,
        string $requested_url,
        string $method,
        array $locales,
        array $params
    )
    {
        $format = $route->getFormat();
        $resolved_url = Url::parse(sprintf($format, ...$params));

        if ($resolved_url->getMethod() !== $method) {
            return;
        }

        $resolved_url->setFormat($format);
        
        $data = $route->toArray();

        foreach ($locales as $locale) {
            $translation = sprintf($data[$locale], ...$params);

            if ($translation === $requested_url && !$resolved_url->getCanonical()) {
                $resolved_url->setLocale($locale);
                $resolved_url->setCanonical($translation);
            } else {
                $resolved_url->addAlternate($locale, $translation);
            }
        }

        return $resolved_url;
    }

    public static function archive(
        Url $url,
        string $method = 'get'
    )
    {
        $url_string = Url::parse((string) $url)
            ->setMethod($method)
            ->setLocale(null);

        list($format) = static::_getFormat((string) $url_string);

        $route = Entity::getEntityRepository()
            ->selectBy([
                'format' => $format
            ]);

        if ($route) {
            $route->archive();
        }
    }
}
{'fraction_non_alphanumeric': 0.14252226910454757, 'fraction_numerical': 0.0009376465072667605, 'mean_word_length': 1.7777777777777777, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, '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': '3417333', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1748, 'n_words': 384}