Some refactorings in content generation
This commit is contained in:
@@ -7,59 +7,77 @@ namespace PmConverter\Exporters\Curl;
|
||||
use PmConverter\Exporters\Abstract\AbstractRequest;
|
||||
|
||||
/**
|
||||
*
|
||||
* Class to determine file content with curl request format
|
||||
*/
|
||||
class CurlRequest extends AbstractRequest
|
||||
{
|
||||
/**
|
||||
* @return string
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareBody(): ?string
|
||||
protected function prepareDescription(): array
|
||||
{
|
||||
switch ($this->bodymode) {
|
||||
case 'formdata':
|
||||
$body = [];
|
||||
foreach ($this->body as $data) {
|
||||
$body[] = sprintf(
|
||||
"%s\t--form '%s=%s' \ ",
|
||||
isset($data->disabled) ? '# ' : '',
|
||||
$data->key,
|
||||
$data->type === 'file' ? "@$data->src" : $data->value
|
||||
);
|
||||
}
|
||||
return implode(PHP_EOL, $body);
|
||||
default:
|
||||
return $this->body;
|
||||
}
|
||||
return empty($this->description)
|
||||
? []
|
||||
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __toString(): string
|
||||
protected function prepareHeaders(): array
|
||||
{
|
||||
$output[] = '#!/bin/sh';
|
||||
if ($this->description) {
|
||||
$output[] = '# ' . str_replace("\n", "\n# ", $this->description);
|
||||
$output[] = '';
|
||||
}
|
||||
$output[] = "curl \ ";
|
||||
$output[] = "\t--http1.1 \ "; //TODO verb
|
||||
$output[] = "\t--request $this->verb \ ";
|
||||
$output[] = "\t--location $this->url \ ";
|
||||
$output = [];
|
||||
foreach ($this->headers as $header_key => $header) {
|
||||
if ($header['disabled']) {
|
||||
continue;
|
||||
}
|
||||
$output[] = sprintf("\t--header '%s=%s' \ ", $header_key, $header['value']);
|
||||
}
|
||||
if (!is_null($body = $this->prepareBody())) {
|
||||
$output[] = match ($this->bodymode) {
|
||||
'formdata' => $body,
|
||||
default => "\t--data '$body'",
|
||||
};
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareBody(): array
|
||||
{
|
||||
$output = [];
|
||||
switch ($this->bodymode) {
|
||||
case 'formdata':
|
||||
foreach ($this->body as $data) {
|
||||
$output[] = sprintf(
|
||||
"%s\t--form '%s=%s' \ ",
|
||||
isset($data->disabled) ? '# ' : '',
|
||||
$data->key,
|
||||
$data->type === 'file' ? "@$data->src" : $data->value
|
||||
);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$output = ["\t--data '$this->body'"];
|
||||
break;
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
$output = array_merge(
|
||||
['#!/bin/sh'],
|
||||
$this->prepareDescription(),
|
||||
[
|
||||
"curl \ ",
|
||||
"\t--http1.1 \ ", //TODO proto
|
||||
"\t--request $this->verb \ ",
|
||||
"\t--location $this->url \ ",
|
||||
],
|
||||
$this->prepareHeaders(),
|
||||
$this->prepareBody()
|
||||
);
|
||||
$output[] = rtrim(array_pop($output), '\ ');
|
||||
return implode(PHP_EOL, $output);
|
||||
return implode(PHP_EOL, array_merge($output, ['']));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user