Ver código fonte

added constraints check for dbo deserialization

develop
Robin Thoni 8 anos atrás
pai
commit
6c914cf293

+ 27
- 27
src/Utils/Business/LuPropertyDocParser.php Ver arquivo

@@ -63,36 +63,36 @@ class LuPropertyDocParser
63 63
                     }
64 64
                     else {
65 65
                         $methodName = $command;
66
-                        $constraint = new LuParameterConstraintDbo();
67
-                        $constraint->setMethod($methodName);
68
-                        $args = [];
69
-                        $argMatches = [];
70
-                        if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
71
-                            $args = $argMatches[1];
72
-                            foreach ($args as $key => $arg) {
73
-                                $argLower = strtolower($arg);
74
-                                if ($arg[0] == '"' || $arg[0] == "'") {
75
-                                    $args[$key] = substr($arg, 1, count($arg) - 2);
76
-                                }
77
-                                else if ($argLower == "true") {
78
-                                    $args[$key] = true;
79
-                                }
80
-                                else if ($argLower == "false") {
81
-                                    $args[$key] = false;
82
-                                }
83
-                                else if ($argLower == "null") {
84
-                                    $args[$key] = null;
85
-                                }
86
-                                else if (strpos($arg, ".") !== false) {
87
-                                    $args[$key] = floatval($arg);
88
-                                }
89
-                                else {
90
-                                    $args[$key] = intval($arg);
66
+                        if ($methodName == "nullable") {
67
+                            $currentParam->setNotNull(false);
68
+                        }
69
+                        else {
70
+                            $constraint = new LuParameterConstraintDbo();
71
+                            $constraint->setMethod($methodName);
72
+                            $args = [];
73
+                            $argMatches = [];
74
+                            if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
75
+                                $args = $argMatches[1];
76
+                                foreach ($args as $key => $arg) {
77
+                                    $argLower = strtolower($arg);
78
+                                    if ($arg[0] == '"' || $arg[0] == "'") {
79
+                                        $args[$key] = substr($arg, 1, strlen($arg) - 2);
80
+                                    } else if ($argLower == "true") {
81
+                                        $args[$key] = true;
82
+                                    } else if ($argLower == "false") {
83
+                                        $args[$key] = false;
84
+                                    } else if ($argLower == "null") {
85
+                                        $args[$key] = null;
86
+                                    } else if (strpos($arg, ".") !== false) {
87
+                                        $args[$key] = floatval($arg);
88
+                                    } else {
89
+                                        $args[$key] = intval($arg);
90
+                                    }
91 91
                                 }
92 92
                             }
93
+                            $constraint->setArguments($args);
94
+                            $currentParam->addConstraint($constraint);
93 95
                         }
94
-                        $constraint->setArguments($args);
95
-                        $currentParam->addConstraint($constraint);
96 96
                     }
97 97
                 }
98 98
                 else {

+ 7
- 7
src/Utils/Controller/LuRoute.php Ver arquivo

@@ -96,29 +96,29 @@ class LuRoute {
96 96
                     else {
97 97
                         $value = $param->getDefaultValue();
98 98
                     }
99
-                    $args[$param->getName()] = $value;
100 99
                 }
101 100
                 else {
102 101
                     if (array_key_exists($param->getName(), $parameters)) {
103
-                        $args[$param->getName()] = $this->getParam($param, $parameters[$param->getName()]);
102
+                        $value = $this->getParam($param, $parameters[$param->getName()]);
104 103
                         
105
-                        if (is_null($args[$param->getName()]) && 
104
+                        if (is_null($value) && 
106 105
                             array_key_exists($param->getName(), $doc->getParams()) &&
107 106
                             $doc->getParams()[$param->getName()]->isNotNull()) {
108
-                            throw new LuBusinessException("Parameter '" . $param->getName() . "' can not be null", 400);
107
+                            throw new LuDboConstraintException("Parameter '" . $param->getName() . "' can not be null", 400);
109 108
                         }
110 109
                     }
111 110
                     else {
112
-                        throw new LuBusinessException("Missing parameter '" . $param->getName() . "'", 400);
111
+                        throw new LuDboDeserializeException("Missing parameter '" . $param->getName() . "'", 400);
113 112
                     }
114 113
                 }
115 114
                 
116
-                if (array_key_exists($param->getName(), $doc->getParams())) {
115
+                if (array_key_exists($param->getName(), $doc->getParams()) && !is_null($value)) {
117 116
                     foreach ($doc->getParams()[$param->getName()]->getConstraints() as $constraint) {
118
-                        call_user_func_array([$args[$param->getName()], $constraint->getMethod()],
117
+                        call_user_func_array([$value, $constraint->getMethod()],
119 118
                             $constraint->getArguments());
120 119
                     }
121 120
                 }
121
+                $args[$param->getName()] = $value;
122 122
             }
123 123
             catch (LuDboConstraintException $e)
124 124
             {

+ 12
- 0
src/Utils/Dbo/LuDbo.php Ver arquivo

@@ -111,6 +111,8 @@ abstract class LuDbo implements \JsonSerializable {
111 111
      * Deserialize from a JSON object
112 112
      * @param $json mixed The JSON data to deserialize
113 113
      * @return LuDbo
114
+     * @throws LuDboConstraintException
115
+     * @throws LuDboDeserializeException
114 116
      */
115 117
     public static function jsonDeserialize($json)
116 118
     {
@@ -130,6 +132,16 @@ abstract class LuDbo implements \JsonSerializable {
130 132
                 $value = static::deserializeValue($json[$name], $type);
131 133
                 $property->setAccessible(true);
132 134
                 $property->setValue($dbo, $value);
135
+                
136
+                if ($doc->isNotNull() && is_null($value)) {
137
+                    throw new LuDboConstraintException("Field '" . $name . "' can not be null");
138
+                }
139
+
140
+                if (!is_null($doc) && !is_null($value)) {
141
+                    foreach ($doc->getConstraints() as $constraint) {
142
+                        call_user_func_array([$value, $constraint->getMethod()], $constraint->getArguments());
143
+                    }
144
+                }
133 145
             }
134 146
         }
135 147
         return $dbo;

Carregando…
Cancelar
Salvar