<commit_before><?php

namespace Reliv\PipeRat\Middleware\Router;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Reliv\PipeRat\Exception\ConfigException;
use Reliv\PipeRat\Exception\RouteException;
use Reliv\PipeRat\Middleware\AbstractModelMiddleware;
use Reliv\PipeRat\Middleware\Middleware;
use Reliv\PipeRat\RequestAttribute\Paths;
use Reliv\PipeRat\RequestAttribute\ResourceKey;
use Reliv\PipeRat\RequestAttribute\RouteParams;
use Reliv\PipeRat\ServiceModel\MethodModel;
use Reliv\PipeRat\ServiceModel\ResourceModel;
use Reliv\PipeRat\ServiceModel\RouteModel;

/**
 * Class CurlyBraceVarRouter This is a router that allows paths like /fun/{id}
 *
 * PHP version 5
 *
 * @category  Reliv
 * @package   Reliv\PipeRat\Middleware
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 Reliv International
 * @license   License.txt New BSD License
 * @version   Release: <package_version>
 * @link      https://github.com/reliv
 */
class CurlyBraceVarRouter extends AbstractModelMiddleware implements Middleware
{
    /**
     * __invoke
     *
     * @param Request $request
     * @param Response $response
     * @param callable|null $out
     *
     * @return mixed
     * @throws RouteException
     */
    public function __invoke(
        Request $request,
        Response $response,
        callable $out = null
    ) {
        $aPathMatched = false;

        /** @var MethodModel $availablePath */
        foreach ($request->getAttribute(Paths::ATTRIBUTE_NAME) as $availablePath => $availableVerbs) {
            $regex = '/^' . str_replace(['{', '}', '/'], ['(?<', '>[^/]+)', '\/'], $availablePath) . '$/';
            if (preg_match($regex, $request->getUri(), $captures)) {
                $aPathMatched = true;

                foreach ($availableVerbs as $availableVerb => $routeData) {
                    if (strcasecmp($availableVerb, $request->getMethod())) {
                        //Put the route params in the request
                        $routeParams = [];
                        foreach ($captures as $key => $val) {
                            if (!is_numeric($key)) {
                                $routeParams[$key] = $val;
                            }
                        }

                        return $out(
                            $request->withAttribute(ResourceKey::ATTRIBUTE_NAME, $routeData)
                                ->withAttribute(RouteParams::ATTRIBUTE_NAME, new RouteParams($routeParams)),
                            $response
                        );
                    }
                }
                break;
            }
        }

        if ($aPathMatched) {
            //If a Path matched but an http verb did not, return 405 Method not allowed
            return $response->withStatus(405);
        }

        //Route is not for us so leave
        return $out($request, $response);
    }
}<commit_msg>make curly brace router work without models<commit_after><?php

namespace Reliv\PipeRat\Middleware\Router;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Reliv\PipeRat\Exception\ConfigException;
use Reliv\PipeRat\Exception\RouteException;
use Reliv\PipeRat\Middleware\AbstractModelMiddleware;
use Reliv\PipeRat\Middleware\Middleware;
use Reliv\PipeRat\RequestAttribute\Paths;
use Reliv\PipeRat\RequestAttribute\ResourceKey;
use Reliv\PipeRat\RequestAttribute\RouteParams;
use Reliv\PipeRat\ServiceModel\MethodModel;
use Reliv\PipeRat\ServiceModel\ResourceModel;
use Reliv\PipeRat\ServiceModel\RouteModel;

/**
 * Class CurlyBraceVarRouter This is a router that allows paths like /fun/{id}
 *
 * PHP version 5
 *
 * @category  Reliv
 * @package   Reliv\PipeRat\Middleware
 * @author    <NAME> <<EMAIL>>
 * @copyright 2016 Reliv International
 * @license   License.txt New BSD License
 * @version   Release: <package_version>
 * @link      https://github.com/reliv
 */
class CurlyBraceVarRouter extends AbstractModelMiddleware implements Middleware
{
    /**
     * Our job as a pipe rat router is to set the "ResourceKey" route param to the
     * data that we are given for the matched route.
     *
     * @param Request $request
     * @param Response $response
     * @param callable|null $out
     *
     * @return mixed
     * @throws RouteException
     */
    public function __invoke(
        Request $request,
        Response $response,
        callable $out = null
    ) {
        $aPathMatched = false;

        /** @var MethodModel $availablePath */
        foreach ($request->getAttribute(Paths::ATTRIBUTE_NAME) as $availablePath => $availableVerbs) {
            $regex = '/^' . str_replace(['{', '}', '/'], ['(?<', '>[^/]+)', '\/'], $availablePath) . '$/';
            if (preg_match($regex, $request->getUri(), $captures)) {
                $aPathMatched = true;

                foreach ($availableVerbs as $availableVerb => $routeData) {
                    if (strcasecmp($availableVerb, $request->getMethod())) {
                        return $out(
                            $request->withAttribute(
                                ResourceKey::ATTRIBUTE_NAME,
                                $routeData
                            )->withAttribute(
                                RouteParams::ATTRIBUTE_NAME,
                                new RouteParams($this->parseNamedCaptures($captures))
                            ),
                            $response
                        );
                    }
                }
                break;
            }
        }

        if ($aPathMatched) {
            //If a path matched but an http verb did not, return 405 method not allowed
            return $response->withStatus(405);
        }

        //Route is not for us so leave
        return $out($request, $response);
    }

    /**
     * Filters the junk numericly keyed captures out of a preg_match captures array
     * leaving us with only the juicy named captures.
     *
     * @param $captures
     * @return array
     */
    protected function parseNamedCaptures($captures)
    {
        $filteredCaptures = [];
        foreach ($captures as $key => $val) {
            if (!is_numeric($key)) {
                $filteredCaptures[$key] = $val;
            }
        }

        return $filteredCaptures;
    }
}
{'fraction_non_alphanumeric': 0.10896530067514523, 'fraction_numerical': 0.0034542314335060447, 'mean_word_length': 1.994828396803009, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 32, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4617795', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1710, 'n_words': 410}