Skip to content

Commit 1277306

Browse files
committed
fix(php): review notes
1 parent f40135b commit 1277306

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

mock-server/app/http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@
316316
->param('x', '', new Text(100), 'Sample string param')
317317
->param('y', '', new Integer(true), 'Sample numeric param')
318318
->param('z', null, new ArrayList(new Text(256), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Sample array param')
319-
->param('payload', [], new File(), 'Sample file param', skipValidation: true)
319+
->param('file', [], new File(), 'Sample file param', skipValidation: true)
320320
->inject('request')
321321
->inject('response')
322322
->action(function (string $x, int $y, array $z, mixed $file, Request $request, Response $response) {

templates/php/base/params.twig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
{%~ for parameter in method.parameters.header %}
2121
$apiHeaders['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
2222
{%~ endfor %}
23-
{%~ if method.name | lower == "createexecution" %}
24-
$apiHeaders['accept'] = 'multipart/form-data';
25-
{%~ endif %}
2623
{%~ for key, header in method.headers %}
2724
$apiHeaders['{{ key }}'] = '{{ header }}';
2825
{%~ endfor %}

templates/php/src/Client.php.twig

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class Client
138138
break;
139139
140140
case 'multipart/form-data':
141+
$headers['accept'] = 'multipart/form-data';
141142
$query = $this->flatten($params);
142143
break;
143144
@@ -259,21 +260,31 @@ class Client
259260
array_shift($lines);
260261
if(isset($lines[0]) && $lines[0] === 'Content-Type: application/json'){
261262
array_shift($lines);
262-
$headers = json_decode(implode($lines), true);
263-
$headers = array_combine(
264-
array_map(fn($header)=> $header['name'], $headers),
265-
array_map(fn($header)=> $header['value'], $headers)
266-
);
267-
$data[$matches['name']] = $headers;
263+
$json = json_decode(implode($lines), true);
264+
265+
if (count($json) > 0 && isset($json[0]['name']) && isset($json[0]['value'])) {
266+
$json = array_combine(
267+
array_map(fn($header) => $header['name'], $json),
268+
array_map(fn($header) => $header['value'], $json)
269+
);
270+
}
271+
272+
$data[$matches['name']] = $json;
268273
continue;
269274
}
270275
$data[$matches['name']] = implode("\r\n",$lines) ?? '';;
271276
}
272277
}
273278
274-
$data['responseStatusCode'] = (int) ($data['responseStatusCode'] ?? '');
275-
$data['duration'] = ((float) ($data['duration'] ?? ''));
276-
$data['responseBody'] = Payload::fromString($data['responseBody'] ?? '');
279+
if(isset($data['responseStatusCode'])) {
280+
$data['responseStatusCode'] = (int) ($data['responseStatusCode'] ?? '');
281+
}
282+
if(isset($data['duration'])) {
283+
$data['duration'] = ((float) ($data['duration'] ?? ''));
284+
}
285+
if(isset($data['responseBody'])) {
286+
$data['responseBody'] = Payload::fromString($data['responseBody'] ?? '');
287+
}
277288
278289
return $data;
279290
}

templates/php/src/Payload.php.twig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Payload {
2929
return $this->filename;
3030
}
3131
32-
public static function fromFile(string $path, ?string $mimeType = null, ?string $filename = null): Payload
32+
public static function fromFile(string $path, ?string $mimeType = null, ?string $filename = null): self
3333
{
3434
$instance = new Payload();
3535
$instance->path = $path;
@@ -39,7 +39,7 @@ class Payload {
3939
return $instance;
4040
}
4141
42-
public static function fromBinary(string $data, ?string $mimeType = null, ?string $filename = null): Payload
42+
public static function fromBinary(string $data, ?string $mimeType = null, ?string $filename = null): self
4343
{
4444
$instance = new Payload();
4545
$instance->path = null;
@@ -49,14 +49,16 @@ class Payload {
4949
return $instance;
5050
}
5151
52-
public static function fromJson(array $data) {
52+
public static function fromJson(array $data): self
53+
{
5354
$instance = new Payload();
5455
$instance->path = null;
5556
$instance->data = json_encode($data);
5657
return $instance;
5758
}
5859
59-
public static function fromString(string $data) {
60+
public static function fromString(string $data): self
61+
{
6062
$instance = new Payload();
6163
$instance->path = null;
6264
$instance->data = $data;

templates/php/src/Services/Service.php.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class {{ service.name | caseUcfirst }} extends Service
5353
);
5454
5555
{{~ include('php/base/params.twig') -}}
56-
{%~ if 'multipart/form-data' in method.consumes and method.name | lower != "createexecution" %}
56+
{%~ if 'multipart/form-data' in method.consumes and method.type != "upload" %}
5757
{{~ include('php/base/requests/file.twig') }}
5858
{%~ else %}
5959
@@ -64,4 +64,4 @@ class {{ service.name | caseUcfirst }} extends Service
6464
6565
{%~ endif %}
6666
{%~ endfor %}
67-
}
67+
}

0 commit comments

Comments
 (0)