Browse Source

json deserialisation

tags/0.1.1^0
Robin Thoni 8 years ago
parent
commit
6f5a6a34a6
1 changed files with 32 additions and 3 deletions
  1. 32
    3
      src/Utils/LuRoute.php

+ 32
- 3
src/Utils/LuRoute.php View File

@@ -49,6 +49,26 @@ class LuRoute {
49 49
         $this->middleware[] = $middleware;
50 50
     }
51 51
 
52
+    private function getClassName(\ReflectionParameter $param) {
53
+        preg_match('/\[\s\<\w+?>\s([\w]+)/s', $param->__toString(), $matches);
54
+        return isset($matches[1]) ? $matches[1] : null;
55
+    }
56
+
57
+    private function getParam(\ReflectionParameter $param, $value)
58
+    {
59
+        $typedValue = null;
60
+        $className = $this->getClassName($param);
61
+        if (is_null($className)) {
62
+            $typedValue = $value;
63
+        }
64
+        else {
65
+            $class = $param->getClass();
66
+            $json = json_decode($value, true);
67
+            $typedValue = call_user_func_array(array($class->getName(), "jsonDeserialize"), array($json));
68
+        }
69
+        return $typedValue;
70
+    }
71
+
52 72
     private function getOptions($httpMethod, $url, $controller, $method, $permissions, $middleware)
53 73
     {
54 74
         if (!is_array($permissions)) {
@@ -77,7 +97,9 @@ class LuRoute {
77 97
         $route->setMethod($httpMethod);
78 98
         $this->routes[] = $route;
79 99
 
80
-        return [function() use($controller, $method)
100
+        $ctrl = $this;
101
+
102
+        return [function() use($controller, $method, $ctrl)
81 103
         {
82 104
             $reflect = new \ReflectionMethod($controller, $method);
83 105
             $params = $reflect->getParameters();
@@ -85,10 +107,17 @@ class LuRoute {
85 107
             $args = array();
86 108
             foreach ($params as $param) {
87 109
                 if ($param->isOptional()) {
88
-                    $args[$param->getName()] = LuBusiness::getParam($param->getName(), $param->getDefaultValue());
110
+                    $value = null;
111
+                    if (LuBusiness::hasParam([$param->getName()])) {
112
+                        $value = $ctrl->getParam($param, LuBusiness::getParam($param->getName()));
113
+                    }
114
+                    else {
115
+                        $value = $param->getDefaultValue();
116
+                    }
117
+                    $args[$param->getName()] = $value;
89 118
                 }
90 119
                 else {
91
-                    $args[$param->getName()] = LuBusiness::checkParam($param->getName());
120
+                    $args[$param->getName()] = $ctrl->getParam($param, LuBusiness::checkParam($param->getName()));
92 121
                 }
93 122
             }
94 123
 

Loading…
Cancel
Save