Browse Source

added more constraints check for dbo deserialization

develop
Robin Thoni 8 years ago
parent
commit
9b233b86e0
2 changed files with 31 additions and 19 deletions
  1. 11
    13
      src/Utils/Controller/LuRoute.php
  2. 20
    6
      src/Utils/Dbo/LuDbo.php

+ 11
- 13
src/Utils/Controller/LuRoute.php View File

81
 
81
 
82
         $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
82
         $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
83
         $params = $reflect->getParameters();
83
         $params = $reflect->getParameters();
84
-        $doc = new LuMethodDocParser($reflect->getDocComment());
85
-        $doc->parse();
84
+        $docs = new LuMethodDocParser($reflect->getDocComment());
85
+        $docs->parse();
86
 
86
 
87
         $args = array();
87
         $args = array();
88
         foreach ($params as $param) {
88
         foreach ($params as $param) {
100
                 else {
100
                 else {
101
                     if (array_key_exists($param->getName(), $parameters)) {
101
                     if (array_key_exists($param->getName(), $parameters)) {
102
                         $value = $this->getParam($param, $parameters[$param->getName()]);
102
                         $value = $this->getParam($param, $parameters[$param->getName()]);
103
-                        
104
-                        if (is_null($value) && 
105
-                            array_key_exists($param->getName(), $doc->getParams()) &&
106
-                            $doc->getParams()[$param->getName()]->isNotNull()) {
107
-                            throw new LuDboConstraintException("Parameter '" . $param->getName() . "' can not be null", 400);
108
-                        }
109
                     }
103
                     }
110
                     else {
104
                     else {
111
                         throw new LuDboDeserializeException("Missing parameter '" . $param->getName() . "'", 400);
105
                         throw new LuDboDeserializeException("Missing parameter '" . $param->getName() . "'", 400);
112
                     }
106
                     }
113
                 }
107
                 }
114
-                
115
-                if (array_key_exists($param->getName(), $doc->getParams()) && !is_null($value)) {
116
-                    foreach ($doc->getParams()[$param->getName()]->getConstraints() as $constraint) {
117
-                        call_user_func_array([$value, $constraint->getMethod()],
118
-                            $constraint->getArguments());
108
+
109
+                if (array_key_exists($param->getName(), $docs->getParams())) {
110
+                    $doc = $docs->getParams()[$param->getName()];
111
+                    if (is_null($value) && $doc->isNotNull()) {
112
+                        throw new LuDboConstraintException("Parameter '" . $param->getName() . "' can not be null", 400);
113
+                    }
114
+
115
+                    if (!is_null($value)) {
116
+                        $value->checkConstraints($doc->getConstraints());
119
                     }
117
                     }
120
                 }
118
                 }
121
                 $args[$param->getName()] = $value;
119
                 $args[$param->getName()] = $value;

+ 20
- 6
src/Utils/Dbo/LuDbo.php View File

60
         return $obj;
60
         return $obj;
61
     }
61
     }
62
 
62
 
63
-    public static function deserializeValue($value, $type) {
63
+    public static function deserializeValue($value, $type, $constraints = []) {
64
         if (is_null($value)) {
64
         if (is_null($value)) {
65
             return null;
65
             return null;
66
         }
66
         }
74
 
74
 
75
         if ($type == "string") {
75
         if ($type == "string") {
76
             $dbo = LuStringDbo::jsonDeserialize($value);
76
             $dbo = LuStringDbo::jsonDeserialize($value);
77
+            $dbo->checkConstraints($constraints);
77
             return $dbo->getString();
78
             return $dbo->getString();
78
         }
79
         }
79
         else if ($type == "int") {
80
         else if ($type == "int") {
80
             $dbo = LuIntDbo::jsonDeserialize($value);
81
             $dbo = LuIntDbo::jsonDeserialize($value);
82
+            $dbo->checkConstraints($constraints);
81
             return $dbo->getInt();
83
             return $dbo->getInt();
82
         }
84
         }
83
         else if ($type == "float") {
85
         else if ($type == "float") {
84
             $dbo = LuFloatDbo::jsonDeserialize($value);
86
             $dbo = LuFloatDbo::jsonDeserialize($value);
87
+            $dbo->checkConstraints($constraints);
85
             return $dbo->getFloat();
88
             return $dbo->getFloat();
86
         }
89
         }
87
         else if ($type == "bool") {
90
         else if ($type == "bool") {
88
             $dbo = LuBoolDbo::jsonDeserialize($value);
91
             $dbo = LuBoolDbo::jsonDeserialize($value);
92
+            $dbo->checkConstraints($constraints);
89
             return $dbo->getBool();
93
             return $dbo->getBool();
90
         }
94
         }
91
         else if ($type == "mixed") {
95
         else if ($type == "mixed") {
98
             $type = substr($type, 0, strlen($type) - 2);
102
             $type = substr($type, 0, strlen($type) - 2);
99
             $data = [];
103
             $data = [];
100
             foreach ($value as $v) {
104
             foreach ($value as $v) {
101
-                $data[] = self::deserializeValue($v, $type);
105
+                $value = self::deserializeValue($v, $type, $constraints);
106
+                $data[] = $value;
102
             }
107
             }
103
             return $data;
108
             return $data;
104
         }
109
         }
105
         else {
110
         else {
106
-            return call_user_func_array(array($type, "jsonDeserialize"), array($value));
111
+            $value = call_user_func_array(array($type, "jsonDeserialize"), array($value));
112
+            $value->checkConstraints($constraints);
113
+            return $value;
107
         }
114
         }
108
     }
115
     }
109
 
116
 
138
             }
145
             }
139
 
146
 
140
             if (!is_null($doc) && !is_null($value)) {
147
             if (!is_null($doc) && !is_null($value)) {
141
-                foreach ($doc->getConstraints() as $constraint) {
142
-                    call_user_func_array([$value, $constraint->getMethod()], $constraint->getArguments());
143
-                }
148
+                $value->checkConstraints($doc->getConstraints());
144
             }
149
             }
145
             $property->setAccessible(true);
150
             $property->setAccessible(true);
146
             $property->setValue($dbo, $value);
151
             $property->setValue($dbo, $value);
148
         return $dbo;
153
         return $dbo;
149
     }
154
     }
150
 
155
 
156
+    /**
157
+     * @param $constraints LuParameterConstraintDbo[]
158
+     */
159
+    public function checkConstraints($constraints) {
160
+        foreach ($constraints as $constraint) {
161
+            call_user_func_array([$this, $constraint->getMethod()], $constraint->getArguments());
162
+        }
163
+    } 
164
+
151
     /**
165
     /**
152
      * Generate a sample JSON object for the DBO
166
      * Generate a sample JSON object for the DBO
153
      * @return array
167
      * @return array

Loading…
Cancel
Save