<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2020 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Router\Match;

use Leevel\Http\Request;
use Leevel\Router\IRouter;

/**
 * 注解路由匹配.
 */
class Annotation extends Match implements IMatch
{
    /**
     * 匹配变量.
     *
     * @var array
     */
    protected array $matchedVars = [];

    /**
     * 匹配数据项.
     */
    public function matche(IRouter $router, Request $request): array
    {
        $this->setRouterAndRequest($router, $request);

        return $this->matchMain();
    }

    /**
     * 主匹配.
     */
    protected function matchMain(): array
    {
        if (!($routers = $this->router->getRouters())) {
            return [];
        }

        // 匹配路由请求方法
        if (false === ($routers = $this->matcheMethod($routers))) {
            return [];
        }

        // 匹配 PathInfo
        $pathInfo = $this->matchePathInfo();

        // 静态路由匹配
        if (false !== ($result = $this->matcheStatic($routers))) {
            return $result;
        }

        // 匹配首字母
        if (false === ($routers = $this->matcheFirstLetter($pathInfo, $routers))) {
            return [];
        }

        // 匹配分组
        $routers = $this->matcheGroups($pathInfo, $routers);

        // 路由匹配
        if (false !== ($result = $this->matcheRegexGroups($routers))) {
            return $result;
        }

        return [];
    }

    /**
     * 匹配路由方法.
     *
     * @return array|false
     */
    protected function matcheMethod(array $router)
    {
        return $router[strtolower($this->request->getMethod())] ?? false;
    }

    /**
     * 匹配静态路由.
     *
     * @return array|false
     */
    protected function matcheStatic(array $router)
    {
        $pathInfo = $this->getPathInfo();
        if (isset($router['static'], $router['static'][$pathInfo])) {
            return $this->matcheSuccessed($router['static'][$pathInfo]);
        }

        return false;
    }

    /**
     * 匹配首字母.
     *
     * @return array|false
     */
    protected function matcheFirstLetter(string $pathInfo, array $router)
    {
        return $router[$pathInfo[1]] ?? false;
    }

    /**
     * 匹配路由分组.
     */
    protected function matcheGroups(string $pathInfo, array $routers): array
    {
        $matchGroup = false;
        foreach ($this->router->getGroups() as $group) {
            if (0 === strpos($pathInfo, $group)) {
                $routers = $routers[$group];
                $matchGroup = true;

                break;
            }
        }

        if (false === $matchGroup) {
            $routers = $routers['_'] ?? [];
        }

        return $routers;
    }

    /**
     * 匹配路由正则分组.
     *
     * @return array|false
     */
    protected function matcheRegexGroups(array $router)
    {
        $pathInfo = $this->getPathInfo();
        foreach ($router['regex'] as $key => $regex) {
            if (!preg_match($regex, $pathInfo, $matches)) {
                continue;
            }

            $matchedRouter = $router['map'][$key][count($matches)];
            $router = $router[$matchedRouter];

            return $this->matcheSuccessed($router, $this->matcheVariable($router, $matches));
        }

        return false;
    }<commit_msg>refactor(router): optimize code<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2020 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Router\Match;

use Leevel\Http\Request;
use Leevel\Router\IRouter;

/**
 * 注解路由匹配.
 */
class Annotation extends Match implements IMatch
{
    /**
     * 匹配变量.
     *
     * @var array
     */
    protected array $matchedVars = [];

    /**
     * 匹配数据项.
     */
    public function matche(IRouter $router, Request $request): array
    {
        $this->setRouterAndRequest($router, $request);

        return $this->matchMain();
    }

    /**
     * 主匹配.
     */
    protected function matchMain(): array
    {
        if (!($routers = $this->router->getRouters())) {
            return [];
        }

        // 匹配路由请求方法
        if (false === ($routers = $this->matcheMethod($routers))) {
            return [];
        }

        // 匹配 PathInfo
        $pathInfo = $this->matchePathInfo();

        // 静态路由匹配
        if (false !== ($result = $this->matcheStatic($routers))) {
            return $result;
        }

        // 匹配首字母
        if (false === ($routers = $this->matcheFirstLetter($pathInfo, $routers))) {
            return [];
        }

        // 匹配分组
        $routers = $this->matcheGroups($pathInfo, $routers);

        // 路由匹配
        if (false !== ($result = $this->matcheRegexGroups($routers))) {
            return $result;
        }

        return [];
    }

    /**
     * 匹配路由方法.
     *
     * @return array|false
     */
    protected function matcheMethod(array $router)
    {
        return $router[strtolower($this->request->getMethod())] ?? false;
    }

    /**
     * 匹配静态路由.
     *
     * @return array|false
     */
    protected function matcheStatic(array $router)
    {
        $pathInfo = $this->getPathInfo();
        if (isset($router['static'], $router['static'][$pathInfo])) {
            return $this->matcheSuccessed($router['static'][$pathInfo]);
        }

        return false;
    }

    /**
     * 匹配首字母.
     *
     * @return array|false
     */
    protected function matcheFirstLetter(string $pathInfo, array $router)
    {
        return $router[$pathInfo[1]] ?? false;
    }

    /**
     * 匹配路由分组.
     */
    protected function matcheGroups(string $pathInfo, array $routers): array
    {
        $matchGroup = false;
        foreach ($this->router->getGroups() as $group) {
            if (0 === strpos($pathInfo, $group)) {
                $routers = $routers[$group];
                $matchGroup = true;

                break;
            }
        }

        if (false === $matchGroup) {
            $routers = $routers['_'] ?? [];
        }

        return $routers;
    }

    /**
     * 匹配路由正则分组.
     *
     * @return array|false
     */
    protected function matcheRegexGroups(array $routers)
    {
        $pathInfo = $this->getPathInfo();
        foreach ($routers['regex'] as $key => $regex) {
            if (!preg_match($regex, $pathInfo, $matches)) {
                continue;
            }

            $matchedRouter = $routers['map'][$key][count($matches)];
            $router = $routers[$matchedRouter];

            return $this->matcheSuccessed($router, $this->matcheVariable($router, $matches));
        }

        return false;
    }
{'fraction_non_alphanumeric': 0.1604091144484723, 'fraction_numerical': 0.0028482651475919213, 'mean_word_length': 1.6720857834659288, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '115945', 'n_tokens_mistral': 2502, 'n_tokens_neox': 2455, 'n_words': 442}