Browse Source

json deserialisation

tags/0.1.1^0
Robin Thoni 9 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
         $this->middleware[] = $middleware;
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
     private function getOptions($httpMethod, $url, $controller, $method, $permissions, $middleware)
72
     private function getOptions($httpMethod, $url, $controller, $method, $permissions, $middleware)
53
     {
73
     {
54
         if (!is_array($permissions)) {
74
         if (!is_array($permissions)) {
77
         $route->setMethod($httpMethod);
97
         $route->setMethod($httpMethod);
78
         $this->routes[] = $route;
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
             $reflect = new \ReflectionMethod($controller, $method);
104
             $reflect = new \ReflectionMethod($controller, $method);
83
             $params = $reflect->getParameters();
105
             $params = $reflect->getParameters();
85
             $args = array();
107
             $args = array();
86
             foreach ($params as $param) {
108
             foreach ($params as $param) {
87
                 if ($param->isOptional()) {
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
                 else {
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