|
10 | 10 | use function array_key_exists; |
11 | 11 | use function assert; |
12 | 12 | use function explode; |
| 13 | +use function is_array; |
13 | 14 | use function is_callable; |
14 | 15 | use function is_string; |
15 | 16 | use function Safe\fopen; |
@@ -60,22 +61,31 @@ private function processBody( |
60 | 61 |
|
61 | 62 | // application/json; charset=utf-8 |
62 | 63 | if (strpos($contentType, 'application/json') === 0) { |
63 | | - $mockRequestBuilder->json(json_decode($body, true)); |
| 64 | + if (is_string($body)) { |
| 65 | + $mockRequestBuilder->json(json_decode($body, true)); |
| 66 | + } elseif (is_callable($body)) { |
| 67 | + $mockRequestBuilder->json(json_decode((string) $body(), true)); |
| 68 | + } elseif (is_array($body)) { |
| 69 | + $mockRequestBuilder->json($body); |
| 70 | + } else { |
| 71 | + throw UnprocessableBody::create(); |
| 72 | + } |
64 | 73 |
|
65 | 74 | return; |
66 | 75 | } |
67 | 76 |
|
68 | | - if ( |
69 | | - strpos($contentType, 'application/x-www-form-urlencoded') === 0 |
70 | | - && preg_match('/[^=]+=[^=]*(&[^=]+=[^=]*)*/', (string) $body) |
71 | | - ) { |
72 | | - foreach (explode('&', $body) as $keyValue) { |
73 | | - [$key, $value] = explode('=', $keyValue); |
| 77 | + if (strpos($contentType, 'application/x-www-form-urlencoded') === 0) { |
| 78 | + assert(is_string($body)); |
74 | 79 |
|
75 | | - $mockRequestBuilder->requestParam(urldecode($key), urldecode($value)); |
76 | | - } |
| 80 | + if (preg_match('/[^=]+=[^=]*(&[^=]+=[^=]*)*/', $body)) { |
| 81 | + foreach (explode('&', $body) as $keyValue) { |
| 82 | + [$key, $value] = explode('=', $keyValue); |
77 | 83 |
|
78 | | - return; |
| 84 | + $mockRequestBuilder->requestParam(urldecode($key), urldecode($value)); |
| 85 | + } |
| 86 | + |
| 87 | + return; |
| 88 | + } |
79 | 89 | } |
80 | 90 |
|
81 | 91 | // multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__ |
@@ -114,6 +124,18 @@ private function processBody( |
114 | 124 | return; |
115 | 125 | } |
116 | 126 |
|
117 | | - $mockRequestBuilder->content($body); |
| 127 | + if (is_string($body)) { |
| 128 | + $mockRequestBuilder->content($body); |
| 129 | + |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + if (is_callable($body)) { |
| 134 | + $mockRequestBuilder->content((string) $body()); |
| 135 | + |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + throw UnprocessableBody::create(); |
118 | 140 | } |
119 | 141 | } |
0 commit comments