Skip to content

Commit 8cfe7cb

Browse files
committed
Improve body handling in request
1 parent 8bbc782 commit 8cfe7cb

File tree

2 files changed

+64
-50
lines changed

2 files changed

+64
-50
lines changed

api.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5375,16 +5375,41 @@ private function parseHeaders(array $headers = null)
53755375
$this->headers = $headers;
53765376
}
53775377

5378-
private function parseBody(String $body = null)
5378+
private function decodeBody(String $body) /*: ?object*/
53795379
{
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 {
53815405
if (!empty($_FILES)) {
5382-
$body = json_encode($_POST);
5406+
$object = (object) $_POST;
53835407
} else {
5384-
$body = file_get_contents('php://input');
5408+
$input = file_get_contents('php://input');
5409+
$object = $this->decodeBody($input);
53855410
}
53865411
}
5387-
$this->body = $body;
5412+
$this->body = $object;
53885413
}
53895414

53905415
public function getMethod(): String
@@ -5412,30 +5437,12 @@ public function getParams(): array
54125437

54135438
public function getBody() /*: ?array*/
54145439
{
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;
54345441
}
54355442

54365443
public function setBody($body) /*: void*/
54375444
{
5438-
$this->body = json_encode($body);
5445+
$this->body = $body;
54395446
}
54405447

54415448
public function addHeader(String $key, String $value)

src/Tqdev/PhpCrudApi/Request.php

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,41 @@ private function parseHeaders(array $headers = null)
7575
$this->headers = $headers;
7676
}
7777

78-
private function parseBody(String $body = null)
78+
private function decodeBody(String $body) /*: ?object*/
7979
{
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 {
81105
if (!empty($_FILES)) {
82-
$body = json_encode($_POST);
106+
$object = (object) $_POST;
83107
} else {
84-
$body = file_get_contents('php://input');
108+
$input = file_get_contents('php://input');
109+
$object = $this->decodeBody($input);
85110
}
86111
}
87-
$this->body = $body;
112+
$this->body = $object;
88113
}
89114

90115
public function getMethod(): String
@@ -112,30 +137,12 @@ public function getParams(): array
112137

113138
public function getBody() /*: ?array*/
114139
{
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;
134141
}
135142

136143
public function setBody($body) /*: void*/
137144
{
138-
$this->body = json_encode($body);
145+
$this->body = $body;
139146
}
140147

141148
public function addHeader(String $key, String $value)

0 commit comments

Comments
 (0)