Преглед на файлове

added nullable check

develop
Robin Thoni преди 8 години
родител
ревизия
3cd6b173c9
променени са 5 файла, в които са добавени 67 реда и са изтрити 49 реда
  1. 27
    27
      src/Utils/Business/LuMethodDocParser.php
  2. 6
    0
      src/Utils/Controller/LuRoute.php
  3. 3
    0
      src/Utils/Dbo/LuDbo.php
  4. 21
    14
      src/Utils/Dbo/LuParameterDbo.php
  5. 10
    8
      tests/LuMethodDocParserTest.php

+ 27
- 27
src/Utils/Business/LuMethodDocParser.php Целия файл

@@ -129,36 +129,36 @@ class LuMethodDocParser
129 129
                     }
130 130
                     else if (!is_null($currentParam)) {
131 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 164
                 else {

+ 6
- 0
src/Utils/Controller/LuRoute.php Целия файл

@@ -101,6 +101,12 @@ class LuRoute {
101 101
                 else {
102 102
                     if (array_key_exists($param->getName(), $parameters)) {
103 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 111
                     else {
106 112
                         throw new LuBusinessException("Missing parameter '" . $param->getName() . "'", 400);

+ 3
- 0
src/Utils/Dbo/LuDbo.php Целия файл

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

+ 21
- 14
src/Utils/Dbo/LuParameterDbo.php Целия файл

@@ -10,6 +10,27 @@ namespace Luticate\Utils\Dbo;
10 10
 
11 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 35
      * @var $_summary string
15 36
      */
@@ -98,18 +119,4 @@ class LuParameterDbo extends LuDbo
98 119
      * @var $_constraints LuParameterConstraintDbo[]
99 120
      */
100 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 Целия файл

@@ -44,9 +44,9 @@ class LuDocParserTest extends \PHPUnit_Framework_TestCase
44 44
         }
45 45
         
46 46
         $expectedParams = [
47
-            ["Name" => 'myvar', "Type" => "MyType", "Summary" => "some doc\n",
47
+            ["NotNull" => true, "Summary" => "some doc\n", "Type" => "MyType", "Name" => 'myvar',
48 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 50
                 "Constraints" => []]
51 51
         ];
52 52
         
@@ -70,10 +70,10 @@ class LuDocParserTest extends \PHPUnit_Framework_TestCase
70 70
         }
71 71
 
72 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 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 79
         $this->assertSame($expectedParams, $params);
@@ -88,6 +88,7 @@ class LuDocParserTest extends \PHPUnit_Framework_TestCase
88 88
      * Too much doc
89 89
      * @param MyType $myvar2 some other doc
90 90
      * on another line
91
+     * @nullable
91 92
      * @min 42
92 93
      * @max 42
93 94
      * @between 0 42
@@ -105,9 +106,10 @@ class LuDocParserTest extends \PHPUnit_Framework_TestCase
105 106
         }
106 107
 
107 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 113
                 "Constraints" => [
112 114
                     ["Method" => "min", "Arguments" => [42]],
113 115
                     ["Method" => "max", "Arguments" => [42]],

Loading…
Отказ
Запис