Maurits van der Schee 5 years ago
parent
commit
a0c2984ebb

+ 18
- 3
api.php View File

@@ -5131,6 +5131,8 @@ namespace Tqdev\PhpCrudApi\Database {
5131 5131
         private function convertInputValue($conversion, $value)
5132 5132
         {
5133 5133
             switch ($conversion) {
5134
+                case 'boolean':
5135
+                    return $value ? 1 : 0;
5134 5136
                 case 'base64url_to_base64':
5135 5137
                     return str_pad(strtr($value, '-_', '+/'), ceil(strlen($value) / 4) * 4, '=', STR_PAD_RIGHT);
5136 5138
             }
@@ -5139,6 +5141,9 @@ namespace Tqdev\PhpCrudApi\Database {
5139 5141
 
5140 5142
         private function getInputValueConversion(ReflectedColumn $column): string
5141 5143
         {
5144
+            if ($column->isBoolean()) {
5145
+                return 'boolean';
5146
+            }
5142 5147
             if ($column->isBinary()) {
5143 5148
                 return 'base64url_to_base64';
5144 5149
             }
@@ -9793,18 +9798,28 @@ namespace Tqdev\PhpCrudApi {
9793 9798
 
9794 9799
         private function addParsedBody(ServerRequestInterface $request): ServerRequestInterface
9795 9800
         {
9796
-            $contents = '';
9797 9801
             $parsedBody = $request->getParsedBody();
9798 9802
             if ($parsedBody) {
9799
-                $contents = json_encode($parsedBody);
9803
+                $request = $this->applySlim3Hack($request);
9800 9804
             } else {
9801 9805
                 $body = $request->getBody();
9802 9806
                 if ($body->isReadable() && $body->isSeekable()) {
9803 9807
                     $contents = $body->getContents();
9804 9808
                     $body->rewind();
9809
+                    if ($contents) {
9810
+                        $parsedBody = $this->parseBody($contents);
9811
+                        $request = $request->withParsedBody($parsedBody);
9812
+                    }
9805 9813
                 }
9806 9814
             }
9807
-            if ($contents) {
9815
+            return $request;
9816
+        }
9817
+
9818
+        private function applySlim3Hack(ServerRequestInterface $request): ServerRequestInterface
9819
+        {
9820
+            if (get_class($request) == 'Slim\Http\Request') {
9821
+                $parsedBody = $request->getParsedBody();
9822
+                $contents = json_encode($parsedBody);
9808 9823
                 $parsedBody = $this->parseBody($contents);
9809 9824
                 $request = $request->withParsedBody($parsedBody);
9810 9825
             }

+ 13
- 3
src/Tqdev/PhpCrudApi/Api.php View File

@@ -160,18 +160,28 @@ class Api implements RequestHandlerInterface
160 160
 
161 161
     private function addParsedBody(ServerRequestInterface $request): ServerRequestInterface
162 162
     {
163
-        $contents = '';
164 163
         $parsedBody = $request->getParsedBody();
165 164
         if ($parsedBody) {
166
-            $contents = json_encode($parsedBody);
165
+            $request = $this->applySlim3Hack($request);
167 166
         } else {
168 167
             $body = $request->getBody();
169 168
             if ($body->isReadable() && $body->isSeekable()) {
170 169
                 $contents = $body->getContents();
171 170
                 $body->rewind();
171
+                if ($contents) {
172
+                    $parsedBody = $this->parseBody($contents);
173
+                    $request = $request->withParsedBody($parsedBody);
174
+                }
172 175
             }
173 176
         }
174
-        if ($contents) {
177
+        return $request;
178
+    }
179
+
180
+    private function applySlim3Hack(ServerRequestInterface $request): ServerRequestInterface
181
+    {
182
+        if (get_class($request) == 'Slim\Http\Request') {
183
+            $parsedBody = $request->getParsedBody();
184
+            $contents = json_encode($parsedBody);
175 185
             $parsedBody = $this->parseBody($contents);
176 186
             $request = $request->withParsedBody($parsedBody);
177 187
         }

+ 5
- 0
src/Tqdev/PhpCrudApi/Database/DataConverter.php View File

@@ -56,6 +56,8 @@ class DataConverter
56 56
     private function convertInputValue($conversion, $value)
57 57
     {
58 58
         switch ($conversion) {
59
+            case 'boolean':
60
+                return $value ? 1 : 0;
59 61
             case 'base64url_to_base64':
60 62
                 return str_pad(strtr($value, '-_', '+/'), ceil(strlen($value) / 4) * 4, '=', STR_PAD_RIGHT);
61 63
         }
@@ -64,6 +66,9 @@ class DataConverter
64 66
 
65 67
     private function getInputValueConversion(ReflectedColumn $column): string
66 68
     {
69
+        if ($column->isBoolean()) {
70
+            return 'boolean';
71
+        }
67 72
         if ($column->isBinary()) {
68 73
             return 'base64url_to_base64';
69 74
         }

+ 43
- 0
tests/functional/001_records/084_update_tags_with_boolean.log View File

@@ -0,0 +1,43 @@
1
+GET /records/tags/1
2
+===
3
+200
4
+Content-Type: application/json
5
+Content-Length: 44
6
+
7
+{"id":1,"name":"funny","is_important":false}
8
+===
9
+PUT /records/tags/1
10
+
11
+{"id":1,"name":"funny","is_important":true}
12
+===
13
+200
14
+Content-Type: application/json
15
+Content-Length: 1
16
+
17
+1
18
+===
19
+GET /records/tags/1
20
+===
21
+200
22
+Content-Type: application/json
23
+Content-Length: 43
24
+
25
+{"id":1,"name":"funny","is_important":true}
26
+===
27
+PUT /records/tags/1
28
+
29
+{"id":1,"name":"funny","is_important":false}
30
+===
31
+200
32
+Content-Type: application/json
33
+Content-Length: 1
34
+
35
+1
36
+===
37
+GET /records/tags/1
38
+===
39
+200
40
+Content-Type: application/json
41
+Content-Length: 44
42
+
43
+{"id":1,"name":"funny","is_important":false}

Loading…
Cancel
Save