Pārlūkot izejas kodu

Improve body handling in request

Maurits van der Schee 6 gadus atpakaļ
vecāks
revīzija
8cfe7cb86f
2 mainītis faili ar 64 papildinājumiem un 50 dzēšanām
  1. 32
    25
      api.php
  2. 32
    25
      src/Tqdev/PhpCrudApi/Request.php

+ 32
- 25
api.php Parādīt failu

@@ -5375,16 +5375,41 @@ class Request
5375 5375
         $this->headers = $headers;
5376 5376
     }
5377 5377
 
5378
-    private function parseBody(String $body = null)
5378
+    private function decodeBody(String $body) /*: ?object*/
5379 5379
     {
5380
-        if (!$body) {
5380
+        $first = substr($body, 0, 1);
5381
+        if ($first == '[' || $first == '{') {
5382
+            $object = json_decode($body);
5383
+            $causeCode = json_last_error();
5384
+            if ($causeCode !== JSON_ERROR_NONE) {
5385
+                $object = null;
5386
+            }
5387
+        } else {
5388
+            parse_str($body, $input);
5389
+            foreach ($input as $key => $value) {
5390
+                if (substr($key, -9) == '__is_null') {
5391
+                    $input[substr($key, 0, -9)] = null;
5392
+                    unset($input[$key]);
5393
+                }
5394
+            }
5395
+            $object = (object) $input;
5396
+        }
5397
+        return $object;
5398
+    }
5399
+
5400
+    private function parseBody(String $body = null) /*: void*/
5401
+    {
5402
+        if ($body) {
5403
+            $object = $this->decodeBody($body);
5404
+        } else {
5381 5405
             if (!empty($_FILES)) {
5382
-                $body = json_encode($_POST);
5406
+                $object = (object) $_POST;
5383 5407
             } else {
5384
-                $body = file_get_contents('php://input');
5408
+                $input = file_get_contents('php://input');
5409
+                $object = $this->decodeBody($input);
5385 5410
             }
5386 5411
         }
5387
-        $this->body = $body;
5412
+        $this->body = $object;
5388 5413
     }
5389 5414
 
5390 5415
     public function getMethod(): String
@@ -5412,30 +5437,12 @@ class Request
5412 5437
 
5413 5438
     public function getBody() /*: ?array*/
5414 5439
     {
5415
-        $body = $this->body;
5416
-        $first = substr($body, 0, 1);
5417
-        if ($first == '[' || $first == '{') {
5418
-            $body = json_decode($body);
5419
-            $causeCode = json_last_error();
5420
-            if ($causeCode !== JSON_ERROR_NONE) {
5421
-                return null;
5422
-            }
5423
-        } else {
5424
-            parse_str($body, $input);
5425
-            foreach ($input as $key => $value) {
5426
-                if (substr($key, -9) == '__is_null') {
5427
-                    $input[substr($key, 0, -9)] = null;
5428
-                    unset($input[$key]);
5429
-                }
5430
-            }
5431
-            $body = (object) $input;
5432
-        }
5433
-        return $body;
5440
+        return $this->body;
5434 5441
     }
5435 5442
 
5436 5443
     public function setBody($body) /*: void*/
5437 5444
     {
5438
-        $this->body = json_encode($body);
5445
+        $this->body = $body;
5439 5446
     }
5440 5447
 
5441 5448
     public function addHeader(String $key, String $value)

+ 32
- 25
src/Tqdev/PhpCrudApi/Request.php Parādīt failu

@@ -75,16 +75,41 @@ class Request
75 75
         $this->headers = $headers;
76 76
     }
77 77
 
78
-    private function parseBody(String $body = null)
78
+    private function decodeBody(String $body) /*: ?object*/
79 79
     {
80
-        if (!$body) {
80
+        $first = substr($body, 0, 1);
81
+        if ($first == '[' || $first == '{') {
82
+            $object = json_decode($body);
83
+            $causeCode = json_last_error();
84
+            if ($causeCode !== JSON_ERROR_NONE) {
85
+                $object = null;
86
+            }
87
+        } else {
88
+            parse_str($body, $input);
89
+            foreach ($input as $key => $value) {
90
+                if (substr($key, -9) == '__is_null') {
91
+                    $input[substr($key, 0, -9)] = null;
92
+                    unset($input[$key]);
93
+                }
94
+            }
95
+            $object = (object) $input;
96
+        }
97
+        return $object;
98
+    }
99
+
100
+    private function parseBody(String $body = null) /*: void*/
101
+    {
102
+        if ($body) {
103
+            $object = $this->decodeBody($body);
104
+        } else {
81 105
             if (!empty($_FILES)) {
82
-                $body = json_encode($_POST);
106
+                $object = (object) $_POST;
83 107
             } else {
84
-                $body = file_get_contents('php://input');
108
+                $input = file_get_contents('php://input');
109
+                $object = $this->decodeBody($input);
85 110
             }
86 111
         }
87
-        $this->body = $body;
112
+        $this->body = $object;
88 113
     }
89 114
 
90 115
     public function getMethod(): String
@@ -112,30 +137,12 @@ class Request
112 137
 
113 138
     public function getBody() /*: ?array*/
114 139
     {
115
-        $body = $this->body;
116
-        $first = substr($body, 0, 1);
117
-        if ($first == '[' || $first == '{') {
118
-            $body = json_decode($body);
119
-            $causeCode = json_last_error();
120
-            if ($causeCode !== JSON_ERROR_NONE) {
121
-                return null;
122
-            }
123
-        } else {
124
-            parse_str($body, $input);
125
-            foreach ($input as $key => $value) {
126
-                if (substr($key, -9) == '__is_null') {
127
-                    $input[substr($key, 0, -9)] = null;
128
-                    unset($input[$key]);
129
-                }
130
-            }
131
-            $body = (object) $input;
132
-        }
133
-        return $body;
140
+        return $this->body;
134 141
     }
135 142
 
136 143
     public function setBody($body) /*: void*/
137 144
     {
138
-        $this->body = json_encode($body);
145
+        $this->body = $body;
139 146
     }
140 147
 
141 148
     public function addHeader(String $key, String $value)

Notiek ielāde…
Atcelt
Saglabāt