Преглед изворни кода

added more constraints check for dbo deserialization

develop
Robin Thoni пре 8 година
родитељ
комит
9b233b86e0
2 измењених фајлова са 31 додато и 19 уклоњено
  1. 11
    13
      src/Utils/Controller/LuRoute.php
  2. 20
    6
      src/Utils/Dbo/LuDbo.php

+ 11
- 13
src/Utils/Controller/LuRoute.php Прегледај датотеку

@@ -81,8 +81,8 @@ class LuRoute {
81 81
 
82 82
         $reflect = new \ReflectionMethod($route->getControllerClass(), $route->getControllerMethod());
83 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 87
         $args = array();
88 88
         foreach ($params as $param) {
@@ -100,22 +100,20 @@ class LuRoute {
100 100
                 else {
101 101
                     if (array_key_exists($param->getName(), $parameters)) {
102 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 104
                     else {
111 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 119
                 $args[$param->getName()] = $value;

+ 20
- 6
src/Utils/Dbo/LuDbo.php Прегледај датотеку

@@ -60,7 +60,7 @@ abstract class LuDbo implements \JsonSerializable {
60 60
         return $obj;
61 61
     }
62 62
 
63
-    public static function deserializeValue($value, $type) {
63
+    public static function deserializeValue($value, $type, $constraints = []) {
64 64
         if (is_null($value)) {
65 65
             return null;
66 66
         }
@@ -74,18 +74,22 @@ abstract class LuDbo implements \JsonSerializable {
74 74
 
75 75
         if ($type == "string") {
76 76
             $dbo = LuStringDbo::jsonDeserialize($value);
77
+            $dbo->checkConstraints($constraints);
77 78
             return $dbo->getString();
78 79
         }
79 80
         else if ($type == "int") {
80 81
             $dbo = LuIntDbo::jsonDeserialize($value);
82
+            $dbo->checkConstraints($constraints);
81 83
             return $dbo->getInt();
82 84
         }
83 85
         else if ($type == "float") {
84 86
             $dbo = LuFloatDbo::jsonDeserialize($value);
87
+            $dbo->checkConstraints($constraints);
85 88
             return $dbo->getFloat();
86 89
         }
87 90
         else if ($type == "bool") {
88 91
             $dbo = LuBoolDbo::jsonDeserialize($value);
92
+            $dbo->checkConstraints($constraints);
89 93
             return $dbo->getBool();
90 94
         }
91 95
         else if ($type == "mixed") {
@@ -98,12 +102,15 @@ abstract class LuDbo implements \JsonSerializable {
98 102
             $type = substr($type, 0, strlen($type) - 2);
99 103
             $data = [];
100 104
             foreach ($value as $v) {
101
-                $data[] = self::deserializeValue($v, $type);
105
+                $value = self::deserializeValue($v, $type, $constraints);
106
+                $data[] = $value;
102 107
             }
103 108
             return $data;
104 109
         }
105 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,9 +145,7 @@ abstract class LuDbo implements \JsonSerializable {
138 145
             }
139 146
 
140 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 150
             $property->setAccessible(true);
146 151
             $property->setValue($dbo, $value);
@@ -148,6 +153,15 @@ abstract class LuDbo implements \JsonSerializable {
148 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 166
      * Generate a sample JSON object for the DBO
153 167
      * @return array

Loading…
Откажи
Сачувај