Browse Source

added nullable check

develop
Robin Thoni 8 years ago
parent
commit
3cd6b173c9

+ 27
- 27
src/Utils/Business/LuMethodDocParser.php View File

129
                     }
129
                     }
130
                     else if (!is_null($currentParam)) {
130
                     else if (!is_null($currentParam)) {
131
                         $methodName = $command;
131
                         $methodName = $command;
132
-                        $constraint = new LuParameterConstraintDbo();
133
-                        $constraint->setMethod($methodName);
134
-                        $args = [];
135
-                        $argMatches = [];
136
-                        if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
137
-                            $args = $argMatches[1];
138
-                            foreach ($args as $key => $arg) {
139
-                                $argLower = strtolower($arg);
140
-                                if ($arg[0] == '"' || $arg[0] == "'") {
141
-                                    $args[$key] = substr($arg, 1, count($arg) - 2);
142
-                                }
143
-                                else if ($argLower == "true") {
144
-                                    $args[$key] = true;
145
-                                }
146
-                                else if ($argLower == "false") {
147
-                                    $args[$key] = false;
148
-                                }
149
-                                else if ($argLower == "null") {
150
-                                    $args[$key] = null;
151
-                                }
152
-                                else if (strpos($arg, ".") !== false) {
153
-                                    $args[$key] = floatval($arg);
154
-                                }
155
-                                else {
156
-                                    $args[$key] = intval($arg);
132
+                        if ($methodName == "nullable") {
133
+                            $currentParam->setNotNull(false);
134
+                        }
135
+                        else {
136
+                            $constraint = new LuParameterConstraintDbo();
137
+                            $constraint->setMethod($methodName);
138
+                            $args = [];
139
+                            $argMatches = [];
140
+                            if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
141
+                                $args = $argMatches[1];
142
+                                foreach ($args as $key => $arg) {
143
+                                    $argLower = strtolower($arg);
144
+                                    if ($arg[0] == '"' || $arg[0] == "'") {
145
+                                        $args[$key] = substr($arg, 1, count($arg) - 2);
146
+                                    } else if ($argLower == "true") {
147
+                                        $args[$key] = true;
148
+                                    } else if ($argLower == "false") {
149
+                                        $args[$key] = false;
150
+                                    } else if ($argLower == "null") {
151
+                                        $args[$key] = null;
152
+                                    } else if (strpos($arg, ".") !== false) {
153
+                                        $args[$key] = floatval($arg);
154
+                                    } else {
155
+                                        $args[$key] = intval($arg);
156
+                                    }
157
                                 }
157
                                 }
158
                             }
158
                             }
159
+                            $constraint->setArguments($args);
160
+                            $currentParam->addConstraint($constraint);
159
                         }
161
                         }
160
-                        $constraint->setArguments($args);
161
-                        $currentParam->addConstraint($constraint);
162
                     }
162
                     }
163
                 }
163
                 }
164
                 else {
164
                 else {

+ 6
- 0
src/Utils/Controller/LuRoute.php View File

101
                 else {
101
                 else {
102
                     if (array_key_exists($param->getName(), $parameters)) {
102
                     if (array_key_exists($param->getName(), $parameters)) {
103
                         $args[$param->getName()] = $this->getParam($param, $parameters[$param->getName()]);
103
                         $args[$param->getName()] = $this->getParam($param, $parameters[$param->getName()]);
104
+                        
105
+                        if (is_null($args[$param->getName()]) && 
106
+                            array_key_exists($param->getName(), $doc->getParams()) &&
107
+                            $doc->getParams()[$param->getName()]->isNotNull()) {
108
+                            throw new LuBusinessException("Parameter '" . $param->getName() . "' can not be null", 400);
109
+                        }
104
                     }
110
                     }
105
                     else {
111
                     else {
106
                         throw new LuBusinessException("Missing parameter '" . $param->getName() . "'", 400);
112
                         throw new LuBusinessException("Missing parameter '" . $param->getName() . "'", 400);

+ 3
- 0
src/Utils/Dbo/LuDbo.php View File

114
      */
114
      */
115
     public static function jsonDeserialize($json)
115
     public static function jsonDeserialize($json)
116
     {
116
     {
117
+        if (is_null($json)) {
118
+            return null;
119
+        }
117
         $dbo = new static();
120
         $dbo = new static();
118
         $reflect = new \ReflectionClass(static::class);
121
         $reflect = new \ReflectionClass(static::class);
119
         $properties = $reflect->getProperties();
122
         $properties = $reflect->getProperties();

+ 21
- 14
src/Utils/Dbo/LuParameterDbo.php View File

10
 
10
 
11
 class LuParameterDbo extends LuDbo
11
 class LuParameterDbo extends LuDbo
12
 {
12
 {
13
+    /**
14
+     * @var bool $_notNull
15
+     */
16
+    private $_notNull = true;
17
+
18
+    /**
19
+     * @return boolean
20
+     */
21
+    public function isNotNull()
22
+    {
23
+        return $this->_notNull;
24
+    }
25
+
26
+    /**
27
+     * @param boolean $notNull
28
+     */
29
+    public function setNotNull($notNull)
30
+    {
31
+        $this->_notNull = $notNull;
32
+    }
33
+    
13
     /**
34
     /**
14
      * @var $_summary string
35
      * @var $_summary string
15
      */
36
      */
98
      * @var $_constraints LuParameterConstraintDbo[]
119
      * @var $_constraints LuParameterConstraintDbo[]
99
      */
120
      */
100
     private $_constraints = [];
121
     private $_constraints = [];
101
-    
102
-    function jsonSerialize()
103
-    {
104
-        $constraints = [];
105
-        foreach ($this->_constraints as $constraint) {
106
-            $constraints[] = $constraint->jsonSerialize();
107
-        }
108
-        return [
109
-            "Name" => $this->_name,
110
-            "Type" => $this->_type,
111
-            "Summary" => $this->_summary,
112
-            "Constraints" => $constraints
113
-        ];
114
-    }
115
 }
122
 }

+ 10
- 8
tests/LuMethodDocParserTest.php View File

44
         }
44
         }
45
         
45
         
46
         $expectedParams = [
46
         $expectedParams = [
47
-            ["Name" => 'myvar', "Type" => "MyType", "Summary" => "some doc\n",
47
+            ["NotNull" => true, "Summary" => "some doc\n", "Type" => "MyType", "Name" => 'myvar',
48
                 "Constraints" => []],
48
                 "Constraints" => []],
49
-            ["Name" => 'myvar2', "Type" => "MyType", "Summary" => "some other doc\n",
49
+            ["NotNull" => true, "Summary" => "some other doc\n", "Type" => "MyType", "Name" => 'myvar2',
50
                 "Constraints" => []]
50
                 "Constraints" => []]
51
         ];
51
         ];
52
         
52
         
70
         }
70
         }
71
 
71
 
72
         $expectedParams = [
72
         $expectedParams = [
73
-            'myvar' => ["Name" => 'myvar', "Type" => "MyType", "Summary" => "some doc\n",
73
+            'myvar' => ["NotNull" => true, "Summary" => "some doc\n", "Type" => "MyType", "Name" => 'myvar',
74
                 "Constraints" => []],
74
                 "Constraints" => []],
75
-            'myvar2' => ["Name" => 'myvar2', "Type" => "MyType", "Summary" => "some other doc\non another line\n",
76
-                "Constraints" => []]
75
+            'myvar2' => ["NotNull" => true, "Summary" => "some other doc\non another line\n", "Type" => "MyType",
76
+                "Name" => 'myvar2', "Constraints" => []]
77
         ];
77
         ];
78
 
78
 
79
         $this->assertSame($expectedParams, $params);
79
         $this->assertSame($expectedParams, $params);
88
      * Too much doc
88
      * Too much doc
89
      * @param MyType $myvar2 some other doc
89
      * @param MyType $myvar2 some other doc
90
      * on another line
90
      * on another line
91
+     * @nullable
91
      * @min 42
92
      * @min 42
92
      * @max 42
93
      * @max 42
93
      * @between 0 42
94
      * @between 0 42
105
         }
106
         }
106
 
107
 
107
         $expectedParams = [
108
         $expectedParams = [
108
-            'myvar' => ["Name" => 'myvar', "Type" => "MyType", "Summary" => "some doc\nToo much doc\n",
109
-                "Constraints" => []],
110
-            'myvar2' => ["Name" => 'myvar2', "Type" => "MyType", "Summary" => "some other doc\non another line\nand another\n",
109
+            'myvar' => ["NotNull" => true, "Summary" => "some doc\nToo much doc\n", "Type" => "MyType",
110
+                "Name" => 'myvar', "Constraints" => []],
111
+            'myvar2' => ["NotNull" => false, "Summary" => "some other doc\non another line\nand another\n",
112
+                "Type" => "MyType", "Name" => 'myvar2',
111
                 "Constraints" => [
113
                 "Constraints" => [
112
                     ["Method" => "min", "Arguments" => [42]],
114
                     ["Method" => "min", "Arguments" => [42]],
113
                     ["Method" => "max", "Arguments" => [42]],
115
                     ["Method" => "max", "Arguments" => [42]],

Loading…
Cancel
Save