<commit_before><?php
/**
 *
 */

namespace Mvc5\Route\Match;

use Mvc5\Arg;
use Mvc5\Route\Route;
use Mvc5\Route\Request;

class Wildcard
{
   /**
     * @param Request $request
     * @param Route $route
     * @return Request
     */
    function __invoke(Request $request, Route $route)
    {
        if ($request->matched() || !$route->wildcard()) {
            return $request;
        }

        $params = $request[Arg::PARAMS];
        $path   = trim(substr($request->path(), $request->length()), Arg::SEPARATOR);
        $parts  = explode(Arg::SEPARATOR, $path);

        $n = count($parts);

        if (!$path || $n < 2) {
            return null;
        }

        for($i = 0; $i < $n; $i += 2) {
            if (!isset($parts[$i + 1]) || isset($params[$parts[$i]])) {
                continue;
            }

            $params[$parts[$i]] = $parts[$i + 1];
        }

        $request[Arg::MATCHED] = true;
        $request[Arg::PARAMS]  = $params;

        return $request;
    }
}<commit_msg>use a route constraint for wildcard params<commit_after><?php
/**
 *
 */

namespace Mvc5\Route\Match;

use Mvc5\Arg;
use Mvc5\Route\Route;
use Mvc5\Route\Request;

class Wildcard
{
    /**
     * @var array
     */
    protected $options = [
        Arg::WILDCARD => 'wildcard'
    ];

    /**
     * @param array $options
     */
    function __construct(array $options = [])
    {
        $options && $this->options = $options + $this->options;
    }

    /**
     * @param array $params
     * @param array $options
     * @param array $parts
     * @return array
     */
    protected function match($params, $options, $parts)
    {
        return ($n = count($parts)) % 2 ? $params : $this->set($this->remove($params, $options), $parts, $n);
    }

    /**
     * @param Route $route
     * @return array
     */
    protected function options(Route $route)
    {
        return $route[Arg::OPTIONS] ? $route[Arg::OPTIONS] + $this->options : $this->options;
    }

    /**
     * @param Request $request
     * @return array
     */
    protected function params(Request $request)
    {
        return $request->get(Arg::PARAMS) ?: [];
    }

    /**
     * @param $params
     * @param $options
     * @return array
     */
    protected function parts($params, $options)
    {
        return isset($params[$options[Arg::WILDCARD]]) ? explode(Arg::SEPARATOR, $params[$options[Arg::WILDCARD]]) : [];
    }

    /**
     * @param $params
     * @param $options
     * @return mixed
     */
    protected function remove($params, $options)
    {
        unset($params[$options[Arg::WILDCARD]]);

        return $params;
    }

    /**
     * @param array $params
     * @param array $parts
     * @param int $n
     * @return array
     */
    protected function set(array $params, array $parts, $n)
    {
        for($i = 0; $i < $n; $i += 2) {
            if (isset($params[$parts[$i]])) {
                continue;
            }

            $params[$parts[$i]] = $parts[$i + 1];
        }

        return $params;
    }

   /**
     * @param Request $request
     * @param Route $route
     * @return Request
     */
    function __invoke(Request $request, Route $route)
    {
        if (!$route->wildcard()) {
            return $request;
        }

        $params  = $this->params($request);
        $options = $this->options($route);

        $request[Arg::PARAMS] = $this->match($params, $options, $this->parts($params, $options));

        return $request;
    }
}
{'fraction_non_alphanumeric': 0.1720676799541153, 'fraction_numerical': 0.004875250932033267, 'mean_word_length': 1.976109215017065, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 20, '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': '4598691', 'n_tokens_mistral': 1132, 'n_tokens_neox': 1103, 'n_words': 236}