Some refactorings in content generation

This commit is contained in:
2023-08-10 15:43:18 +08:00
parent 2081aa935a
commit 9947987c20
8 changed files with 194 additions and 103 deletions

View File

@@ -7,47 +7,64 @@ namespace PmConverter\Exporters\Http;
use PmConverter\Exporters\Abstract\AbstractRequest;
/**
*
* Class to determine file content with http request format
*/
class HttpRequest 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%s=%s',
empty($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
{
if ($this->description) {
$output[] = '# ' . str_replace("\n", "\n# ", $this->description);
$output[] = '';
}
$output[] = "$this->verb $this->url $this->http";
foreach ($this->headers as $header_key => $header) {
$output[] = sprintf('%s%s: %s', $header['disabled'] ? '# ' : '', $header_key, $header['value']);
}
$output[] = '';
$output[] = (string)$this->prepareBody();
return $output;
}
/**
* @inheritDoc
*/
protected function prepareBody(): array
{
switch ($this->bodymode) {
case 'formdata':
$output = [''];
foreach ($this->body as $data) {
$output[] = sprintf(
'%s%s=%s',
empty($data->disabled) ? '' : '# ',
$data->key,
$data->type === 'file' ? $data->src : $data->value
);
}
return $output;
default:
return ['', $this->body];
}
}
/**
* @inheritDoc
*/
public function __toString(): string
{
$output = array_merge(
$this->prepareDescription(),
$this->prepareHeaders(),
$this->prepareBody()
);
return implode(PHP_EOL, $output);
}
}