<?php

namespace Application\Service\Extension\Helper;

class RouterHelper
{
    public static function generateRouteMap(array $controllers): array
    {
        $routes = [];
        foreach ($controllers as $controllerName => $controller) {
            foreach ($controller as &$item) {
                $routes[key($item)] = [
                    'type' => 'Literal',
                    'options' => [
                        'route' => key($item),
                        'defaults' => [
                            'controller' => $controllerName,
                            'action' => current($item)
                        ]
                    ]
                ];
            }
        }

        return $routes;
    }

    /**
     * Notes : 使用反射获取指定方法参数名称
     * @param $class
     * @param $function
     * @return array
     * @throws \ReflectionException
     * Author:唐伯虎不虎
     * Date:2024/10/29 13:31
     */
    public function getmethodparams($class,$function)
    {
        $reflection = new \ReflectionMethod($class, $function);
        $params = $reflection->getParameters();

        $paramNames = [];
        foreach ($params as $param) {
            $paramNames[] = $param->getName();
        }
        return $paramNames;
    }

    public static function literalRoute(string $route, string $controllerClass, string $actionName): array
    {
        return [
            'type' => 'Literal',
            'options' => [
                'route' => $route,
                'defaults' => [
                    'controller' => $controllerClass,
                    'action' => $actionName,
                ],
            ],
        ];
    }
}