<commit_before><?php

/* This file is part of the Staq project, which is under MIT license */


namespace Staq\Core\Router\Stack;

class Route
{


    /* ATTRIBUTES
     *************************************************************************/
    protected $callable;
    protected $uri;
    protected $exceptions = [];
    protected $aliases = [];
    protected $parameters = [];


    /* CONSTRUCTOR
     *************************************************************************/
    public function __construct($callable = NULL, $uri = NULL, $exceptions = [], $aliases = [])
    {
        \UArray::doConvertToArray($exceptions);
        \UArray::doConvertToArray($aliases);
        $this->callable = $callable;
        $this->uri = $uri;
        $this->exceptions = $exceptions;
        $this->aliases = $aliases;
    }

    public function bySetting($controller, $action, $setting)
    {
        if (!\UString::isStartWith($action, 'action')) {
            $action = 'action' . ucfirst($action);
        }
        $callable = [$controller, $action];
        if (!is_callable($callable)) {
            $message = get_class($controller) . '::' . $action . ' is not callable';
            throw new \Stack\Exception\NoCallable($message);
        }
        $uri = NULL;
        $exceptions = [];
        $aliases = [];
        if (is_array($setting)) {
            $uri = isset($setting['uri']) ? $setting['uri'] : $uri;
            $exceptions = isset($setting['exceptions']) ? $setting['exceptions'] : $exceptions;
            $aliases = isset($setting['aliases']) ? $setting['aliases'] : $aliases;
        } else if (is_string($setting)) {
            $uri = $setting;
        }
        return new $this($callable, $uri, $exceptions, $aliases);
    }


    /* PUBLIC METHODS
     *************************************************************************/
    public function getUri($parameters = [])
    {
        $uri = $this->uri;
        foreach ($parameters as $name => $value) {
            if (!is_numeric($name)) {
                $uri = str_replace(['::'.$name, ':'.$name], $value, $uri);
                unset($parameters[$name]);
            }
        }
        ksort($parameters);
        foreach ($parameters as $value) {
            $uri = preg_replace('#^([^:]*)::?\w+#', '${1}'.$value, $uri);
        }
        $uri = preg_replace('#\(([^):]*)\)#', '${1}', $uri);
        $uri = preg_replace('#\([^)]*\)#', '', $uri);
        $uri = preg_replace('#::?(\w+)#', '', $uri);
        return $uri;
    }
<commit_msg>Bugfix on numbered typed url reconstitution<commit_after><?php

/* This file is part of the Staq project, which is under MIT license */


namespace Staq\Core\Router\Stack;

class Route
{


    /* ATTRIBUTES
     *************************************************************************/
    protected $callable;
    protected $uri;
    protected $exceptions = [];
    protected $aliases = [];
    protected $parameters = [];


    /* CONSTRUCTOR
     *************************************************************************/
    public function __construct($callable = NULL, $uri = NULL, $exceptions = [], $aliases = [])
    {
        \UArray::doConvertToArray($exceptions);
        \UArray::doConvertToArray($aliases);
        $this->callable = $callable;
        $this->uri = $uri;
        $this->exceptions = $exceptions;
        $this->aliases = $aliases;
    }

    public function bySetting($controller, $action, $setting)
    {
        if (!\UString::isStartWith($action, 'action')) {
            $action = 'action' . ucfirst($action);
        }
        $callable = [$controller, $action];
        if (!is_callable($callable)) {
            $message = get_class($controller) . '::' . $action . ' is not callable';
            throw new \Stack\Exception\NoCallable($message);
        }
        $uri = NULL;
        $exceptions = [];
        $aliases = [];
        if (is_array($setting)) {
            $uri = isset($setting['uri']) ? $setting['uri'] : $uri;
            $exceptions = isset($setting['exceptions']) ? $setting['exceptions'] : $exceptions;
            $aliases = isset($setting['aliases']) ? $setting['aliases'] : $aliases;
        } else if (is_string($setting)) {
            $uri = $setting;
        }
        return new $this($callable, $uri, $exceptions, $aliases);
    }


    /* PUBLIC METHODS
     *************************************************************************/
    public function getUri($parameters = [])
    {
        $uri = $this->uri;
        foreach ($parameters as $name => $value) {
            if (!is_numeric($name)) {
                $uri = str_replace(['#'.$name, ':'.$name], $value, $uri);
                unset($parameters[$name]);
            }
        }
        ksort($parameters);
        foreach ($parameters as $value) {
            $uri = preg_replace('@^([^:#]*)[:#]\w+@', '${1}'.$value, $uri);
        }
        $uri = preg_replace('@\(([^):#]*)\)@', '${1}', $uri);
        $uri = preg_replace('@\([^)]*\)@', '', $uri);
        $uri = preg_replace('@[:#](\w+)@', '', $uri);
        return $uri;
    }

{'fraction_non_alphanumeric': 0.2613390928725702, 'fraction_numerical': 0.0007853917141174161, 'mean_word_length': 2.4842681258549932, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '1541788', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1435, 'n_words': 291}