Maurits van der Schee 5 years ago
parent
commit
7168dc087a

+ 6
- 6
src/Tqdev/PhpCrudApi/Controller/ColumnController.php View File

@@ -72,7 +72,7 @@ class ColumnController
72 72
         if (!$this->reflection->hasTable($tableName)) {
73 73
             return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
74 74
         }
75
-        $success = $this->definition->updateTable($tableName, $request->getBody());
75
+        $success = $this->definition->updateTable($tableName, $request->getParsedBody());
76 76
         if ($success) {
77 77
             $this->reflection->refreshTables();
78 78
         }
@@ -90,7 +90,7 @@ class ColumnController
90 90
         if (!$table->hasColumn($columnName)) {
91 91
             return $this->responder->error(ErrorCode::COLUMN_NOT_FOUND, $columnName);
92 92
         }
93
-        $success = $this->definition->updateColumn($tableName, $columnName, $request->getBody());
93
+        $success = $this->definition->updateColumn($tableName, $columnName, $request->getParsedBody());
94 94
         if ($success) {
95 95
             $this->reflection->refreshTable($tableName);
96 96
         }
@@ -99,11 +99,11 @@ class ColumnController
99 99
 
100 100
     public function addTable(ServerRequestInterface $request): Response
101 101
     {
102
-        $tableName = $request->getBody()->name;
102
+        $tableName = $request->getParsedBody()->name;
103 103
         if ($this->reflection->hasTable($tableName)) {
104 104
             return $this->responder->error(ErrorCode::TABLE_ALREADY_EXISTS, $tableName);
105 105
         }
106
-        $success = $this->definition->addTable($request->getBody());
106
+        $success = $this->definition->addTable($request->getParsedBody());
107 107
         if ($success) {
108 108
             $this->reflection->refreshTables();
109 109
         }
@@ -116,12 +116,12 @@ class ColumnController
116 116
         if (!$this->reflection->hasTable($tableName)) {
117 117
             return $this->responder->error(ErrorCode::TABLE_NOT_FOUND, $tableName);
118 118
         }
119
-        $columnName = $request->getBody()->name;
119
+        $columnName = $request->getParsedBody()->name;
120 120
         $table = $this->reflection->getTable($tableName);
121 121
         if ($table->hasColumn($columnName)) {
122 122
             return $this->responder->error(ErrorCode::COLUMN_ALREADY_EXISTS, $columnName);
123 123
         }
124
-        $success = $this->definition->addColumn($tableName, $request->getBody());
124
+        $success = $this->definition->addColumn($tableName, $request->getParsedBody());
125 125
         if ($success) {
126 126
             $this->reflection->refreshTable($tableName);
127 127
         }

+ 3
- 3
src/Tqdev/PhpCrudApi/Controller/RecordController.php View File

@@ -71,7 +71,7 @@ class RecordController
71 71
         if ($this->service->getType($table) != 'table') {
72 72
             return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
73 73
         }
74
-        $record = $request->getBody();
74
+        $record = $request->getParsedBody();
75 75
         if ($record === null) {
76 76
             return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
77 77
         }
@@ -98,7 +98,7 @@ class RecordController
98 98
         }
99 99
         $id = RequestUtils::getPathSegment($request, 3);
100 100
         $params = RequestUtils::getParams($request);
101
-        $record = $request->getBody();
101
+        $record = $request->getParsedBody();
102 102
         if ($record === null) {
103 103
             return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
104 104
         }
@@ -153,7 +153,7 @@ class RecordController
153 153
             return $this->responder->error(ErrorCode::OPERATION_NOT_SUPPORTED, __FUNCTION__);
154 154
         }
155 155
         $id = RequestUtils::getPathSegment($request, 3);
156
-        $record = $request->getBody();
156
+        $record = $request->getParsedBody();
157 157
         if ($record === null) {
158 158
             return $this->responder->error(ErrorCode::HTTP_MESSAGE_NOT_READABLE, '');
159 159
         }

+ 2
- 2
src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php View File

@@ -46,7 +46,7 @@ class IpAddressMiddleware extends Middleware
46 46
             $tableName = RequestUtils::getPathSegment($request, 2);
47 47
             if (!$tableNames || in_array($tableName, explode(',', $tableNames))) {
48 48
                 if ($this->reflection->hasTable($tableName)) {
49
-                    $record = $request->getBody();
49
+                    $record = $request->getParsedBody();
50 50
                     if ($record !== null) {
51 51
                         $table = $this->reflection->getTable($tableName);
52 52
                         if (is_array($record)) {
@@ -56,7 +56,7 @@ class IpAddressMiddleware extends Middleware
56 56
                         } else {
57 57
                             $record = $this->callHandler($record, $operation, $table);
58 58
                         }
59
-                        $request->setBody($record);
59
+                        $request = $request->withParsedBody($record);
60 60
                     }
61 61
                 }
62 62
             }

+ 5
- 5
src/Tqdev/PhpCrudApi/Middleware/MultiTenancyMiddleware.php View File

@@ -46,11 +46,11 @@ class MultiTenancyMiddleware extends Middleware
46 46
         return $result;
47 47
     }
48 48
 
49
-    private function handleRecord(ServerRequestInterface $request, string $operation, array $pairs) /*: void*/
49
+    private function handleRecord(ServerRequestInterface $request, string $operation, array $pairs): ServerRequestInterface
50 50
     {
51
-        $record = $request->getBody();
51
+        $record = $request->getParsedBody();
52 52
         if ($record === null) {
53
-            return;
53
+            return $request;
54 54
         }
55 55
         $multi = is_array($record);
56 56
         $records = $multi ? $record : [$record];
@@ -65,7 +65,7 @@ class MultiTenancyMiddleware extends Middleware
65 65
                 }
66 66
             }
67 67
         }
68
-        $request->setBody($multi ? $records : $records[0]);
68
+        return $request->withParsedBody($multi ? $records : $records[0]);
69 69
     }
70 70
 
71 71
     public function handle(ServerRequestInterface $request): Response
@@ -83,7 +83,7 @@ class MultiTenancyMiddleware extends Middleware
83 83
                     $pairs = $this->getPairs($handler, $operation, $tableName);
84 84
                     if ($i == 0) {
85 85
                         if (in_array($operation, ['create', 'update', 'increment'])) {
86
-                            $this->handleRecord($request, $operation, $pairs);
86
+                            $request = $this->handleRecord($request, $operation, $pairs);
87 87
                         }
88 88
                     }
89 89
                     $condition = $this->getCondition($tableName, $pairs);

+ 2
- 2
src/Tqdev/PhpCrudApi/Middleware/SanitationMiddleware.php View File

@@ -39,7 +39,7 @@ class SanitationMiddleware extends Middleware
39 39
         if (in_array($operation, ['create', 'update', 'increment'])) {
40 40
             $tableName = RequestUtils::getPathSegment($request, 2);
41 41
             if ($this->reflection->hasTable($tableName)) {
42
-                $record = $request->getBody();
42
+                $record = $request->getParsedBody();
43 43
                 if ($record !== null) {
44 44
                     $handler = $this->getProperty('handler', '');
45 45
                     if ($handler !== '') {
@@ -51,7 +51,7 @@ class SanitationMiddleware extends Middleware
51 51
                         } else {
52 52
                             $record = $this->callHandler($handler, $record, $operation, $table);
53 53
                         }
54
-                        $request->setBody($record);
54
+                        $request = $request->withParsedBody($record);
55 55
                     }
56 56
                 }
57 57
             }

+ 1
- 1
src/Tqdev/PhpCrudApi/Middleware/ValidationMiddleware.php View File

@@ -47,7 +47,7 @@ class ValidationMiddleware extends Middleware
47 47
         if (in_array($operation, ['create', 'update', 'increment'])) {
48 48
             $tableName = RequestUtils::getPathSegment($request, 2);
49 49
             if ($this->reflection->hasTable($tableName)) {
50
-                $record = $request->getBody();
50
+                $record = $request->getParsedBody();
51 51
                 if ($record !== null) {
52 52
                     $handler = $this->getProperty('handler', '');
53 53
                     if ($handler !== '') {

+ 34
- 4
src/Tqdev/PhpCrudApi/RequestFactory.php View File

@@ -7,11 +7,38 @@ use Psr\Http\Message\ServerRequestInterface;
7 7
 
8 8
 class RequestFactory
9 9
 {
10
+    private static function parseBody(string $body) /*: ?object*/
11
+    {
12
+        $first = substr($body, 0, 1);
13
+        if ($first == '[' || $first == '{') {
14
+            $object = json_decode($body);
15
+            $causeCode = json_last_error();
16
+            if ($causeCode !== JSON_ERROR_NONE) {
17
+                $object = null;
18
+            }
19
+        } else {
20
+            parse_str($body, $input);
21
+            foreach ($input as $key => $value) {
22
+                if (substr($key, -9) == '__is_null') {
23
+                    $input[substr($key, 0, -9)] = null;
24
+                    unset($input[$key]);
25
+                }
26
+            }
27
+            $object = (object) $input;
28
+        }
29
+        return $object;
30
+    }
31
+
10 32
     public static function fromGlobals(): ServerRequestInterface
11 33
     {
12 34
         $psr17Factory = new Psr17Factory();
13 35
         $creator = new ServerRequestCreator($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
14
-        return $creator->fromGlobals();
36
+        $serverRequest = $creator->fromGlobals();
37
+        $body = file_get_contents('php://input');
38
+        if ($body) {
39
+            $serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
40
+        }
41
+        return $serverRequest;
15 42
     }
16 43
 
17 44
     public static function fromString(string $request): ServerRequestInterface
@@ -25,13 +52,16 @@ class RequestFactory
25 52
 
26 53
         $psr17Factory = new Psr17Factory();
27 54
         $serverRequest = $psr17Factory->createServerRequest($method, $url);
28
-        if ($body) {
29
-            $serverRequest = $serverRequest->withBody($psr17Factory->createStream($body));
30
-        }
31 55
         foreach ($lines as $line) {
32 56
             list($key, $value) = explode(':', $line, 2);
33 57
             $serverRequest = $serverRequest->withAddedHeader($key, $value);
34 58
         }
59
+        if ($body) {
60
+            $stream = $psr17Factory->createStream($body);
61
+            $stream->rewind();
62
+            $serverRequest = $serverRequest->withBody($stream);
63
+            $serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
64
+        }
35 65
         return $serverRequest;
36 66
     }
37 67
 }

+ 3
- 10
src/Tqdev/PhpCrudApi/RequestUtils.php View File

@@ -8,7 +8,8 @@ class RequestUtils
8 8
 {
9 9
     public static function setParams(ServerRequestInterface $request, array $params): ServerRequestInterface
10 10
     {
11
-        return $request->withUri($request->getUri()->withQuery(http_build_query($params)));
11
+        $query = preg_replace('|%5B[0-9]+%5D=|', '=', http_build_query($params));
12
+        return $request->withUri($request->getUri()->withQuery($query));
12 13
     }
13 14
 
14 15
     public static function getHeader(ServerRequestInterface $request, string $header): string
@@ -21,16 +22,8 @@ class RequestUtils
21 22
     {
22 23
         $params = array();
23 24
         $query = $request->getUri()->getQuery();
24
-        if ($query) {
25
-            die(var_dump($query));
26
-        }
27
-
28 25
         $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
29 26
         parse_str($query, $params);
30
-        if ($params) {
31
-            die(var_dump($params));
32
-        }
33
-
34 27
         return $params;
35 28
     }
36 29
 
@@ -40,7 +33,7 @@ class RequestUtils
40 33
         if ($part < 0 || $part >= count($pathSegments)) {
41 34
             return '';
42 35
         }
43
-        return $pathSegments[$part];
36
+        return urldecode($pathSegments[$part]);
44 37
     }
45 38
 
46 39
     public static function getOperation(ServerRequestInterface $request): string

+ 1
- 0
tests/functional/001_records/006_add_post.log View File

@@ -1,4 +1,5 @@
1 1
 POST /records/posts
2
+Content-Type: application/json
2 3
 
3 4
 {"user_id":1,"category_id":1,"content":"test"}
4 5
 ===

Loading…
Cancel
Save