OSDN Git Service

add method reflection and parameter sorting
[php-libraries/Router.git] / src / Route.php
index bff2403..1dc3880 100755 (executable)
@@ -42,13 +42,6 @@ class Route
     private $parameters = array();
 
     /**
-     * Set named parameters to target method
-     * @example [ [0] => [ ["link_id"] => "12312" ] ]
-     * @var bool
-     */
-    private $parametersByName;
-
-    /**
      * @var null|string
      */
     private $action;
@@ -75,6 +68,7 @@ class Route
         $this->target     = isset($config['target']) ? $config['target'] : null;
         $this->name       = isset($config['name']) ? $config['name'] : null;
         $this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
+        $this->filters    = isset($config['filters']) ? $config['filters'] : array();
         $action           = explode('::', $this->config['_controller']);
         $this->class      = isset($action[0]) ? $action[0] : null;
         $this->action     = isset($action[1]) ? $action[1] : null;
@@ -127,10 +121,14 @@ class Route
         $this->name = (string)$name;
     }
 
-    public function setFilters(array $filters, $parametersByName = false)
+    public function setFilters(array $filters)
     {
-        $this->filters          = $filters;
-        $this->parametersByName = $parametersByName;
+        $this->filters = $filters;
+    }
+
+    public function getFilters()
+    {
+        return $this->filters;
     }
 
     public function getRegex()
@@ -162,7 +160,7 @@ class Route
         $class  = $this->class;
         $method = $this->action;
 
-        if ( !class_exists($class)) {
+        if (!class_exists($class)) {
             return null;
         }
 
@@ -185,14 +183,63 @@ class Route
         return $this->config['_controller'];
     }
 
+    /**
+     * sort parameters according the the method's arguments
+     *
+     * @return array
+     *
+     * @throws \ReflectionException
+     */
+    private function sortParameters()
+    {
+        $class      = $this->class;
+        $method     = $this->action;
+        $parameters = $this->parameters;
+        $arguments  = array();
+
+        if (empty($method) || trim($method) === '') {
+            $method = "__invoke";
+        }
+
+        $rexl = new \ReflectionMethod($class, $method);
+
+        foreach ($rexl->getParameters() as $methArgs) {
+            $arg = $methArgs->getName();
+
+            if (array_key_exists($arg, $parameters)) {
+                $arguments[$arg] = $parameters[$arg];
+
+                unset($parameters[$arg]);
+            } else {
+                // argument is not in the parameters
+                $arguments[$arg] = null;
+            }
+        }
+
+        if (count($parameters) > 0) {
+            // fill the unset arguments
+            foreach ($arguments as $arg => &$v) {
+                if ($v === null) {
+                    //$key = array_keys($parameters)[0];
+
+                    $v = array_shift($parameters);
+                }
+
+                if (count($parameters) <= 0) {
+                    break;
+                }
+            }
+        }
+
+        // merge the remaining parameters
+        return array_merge($arguments, $parameters);
+    }
+
     public function dispatch($instance = null)
     {
         is_null($instance) and $instance = new $this->class();
 
-        // todo figure out what parametersByName is for
-        $param = $this->parametersByName ?
-            array($this->parameters) :
-            $this->parameters;
+        $param = $this->sortParameters();
 
         ob_start();