Skip to content

Commit a0c2984

Browse files
committed
Fix for #568
1 parent c90fc06 commit a0c2984

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

api.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5131,6 +5131,8 @@ public function convertRecords(ReflectedTable $table, array $columnNames, array
51315131
private function convertInputValue($conversion, $value)
51325132
{
51335133
switch ($conversion) {
5134+
case 'boolean':
5135+
return $value ? 1 : 0;
51345136
case 'base64url_to_base64':
51355137
return str_pad(strtr($value, '-_', '+/'), ceil(strlen($value) / 4) * 4, '=', STR_PAD_RIGHT);
51365138
}
@@ -5139,6 +5141,9 @@ private function convertInputValue($conversion, $value)
51395141

51405142
private function getInputValueConversion(ReflectedColumn $column): string
51415143
{
5144+
if ($column->isBoolean()) {
5145+
return 'boolean';
5146+
}
51425147
if ($column->isBinary()) {
51435148
return 'base64url_to_base64';
51445149
}
@@ -9793,18 +9798,28 @@ private function parseBody(string $body) /*: ?object*/
97939798

97949799
private function addParsedBody(ServerRequestInterface $request): ServerRequestInterface
97959800
{
9796-
$contents = '';
97979801
$parsedBody = $request->getParsedBody();
97989802
if ($parsedBody) {
9799-
$contents = json_encode($parsedBody);
9803+
$request = $this->applySlim3Hack($request);
98009804
} else {
98019805
$body = $request->getBody();
98029806
if ($body->isReadable() && $body->isSeekable()) {
98039807
$contents = $body->getContents();
98049808
$body->rewind();
9809+
if ($contents) {
9810+
$parsedBody = $this->parseBody($contents);
9811+
$request = $request->withParsedBody($parsedBody);
9812+
}
98059813
}
98069814
}
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);
98089823
$parsedBody = $this->parseBody($contents);
98099824
$request = $request->withParsedBody($parsedBody);
98109825
}

src/Tqdev/PhpCrudApi/Api.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,28 @@ private function parseBody(string $body) /*: ?object*/
160160

161161
private function addParsedBody(ServerRequestInterface $request): ServerRequestInterface
162162
{
163-
$contents = '';
164163
$parsedBody = $request->getParsedBody();
165164
if ($parsedBody) {
166-
$contents = json_encode($parsedBody);
165+
$request = $this->applySlim3Hack($request);
167166
} else {
168167
$body = $request->getBody();
169168
if ($body->isReadable() && $body->isSeekable()) {
170169
$contents = $body->getContents();
171170
$body->rewind();
171+
if ($contents) {
172+
$parsedBody = $this->parseBody($contents);
173+
$request = $request->withParsedBody($parsedBody);
174+
}
172175
}
173176
}
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);
175185
$parsedBody = $this->parseBody($contents);
176186
$request = $request->withParsedBody($parsedBody);
177187
}

src/Tqdev/PhpCrudApi/Database/DataConverter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function convertRecords(ReflectedTable $table, array $columnNames, array
5656
private function convertInputValue($conversion, $value)
5757
{
5858
switch ($conversion) {
59+
case 'boolean':
60+
return $value ? 1 : 0;
5961
case 'base64url_to_base64':
6062
return str_pad(strtr($value, '-_', '+/'), ceil(strlen($value) / 4) * 4, '=', STR_PAD_RIGHT);
6163
}
@@ -64,6 +66,9 @@ private function convertInputValue($conversion, $value)
6466

6567
private function getInputValueConversion(ReflectedColumn $column): string
6668
{
69+
if ($column->isBoolean()) {
70+
return 'boolean';
71+
}
6772
if ($column->isBinary()) {
6873
return 'base64url_to_base64';
6974
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}

0 commit comments

Comments
 (0)