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,57 +7,76 @@ namespace PmConverter\Exporters\Wget;
use PmConverter\Exporters\Abstract\AbstractRequest;
/**
*
* Class to determine file content with wget request format
*/
class WgetRequest extends AbstractRequest
{
/**
* @return string
* @inheritDoc
*/
protected function prepareBody(): ?string
protected function prepareDescription(): array
{
switch ($this->bodymode) {
case 'formdata':
$lines = [];
foreach ($this->body as &$data) {
if ($data->type === 'file') {
continue;
}
$lines[$data->key] = $data->value;
}
$body[] = http_build_query($lines);
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[] = 'wget \ ';
$output[] = "\t--no-check-certificate \ ";
$output[] = "\t--quiet \ ";
$output[] = "\t--timeout=0 \ ";
$output[] = "\t--method $this->verb \ ";
$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[] = "\t--body-data '$body' \ ";
return $output;
}
/**
* @inheritDoc
*/
protected function prepareBody(): array
{
switch ($this->bodymode) {
case 'formdata':
$params = [];
foreach ($this->body as $data) {
if ($data->type === 'file') {
continue;
}
$params[$data->key] = $data->value;
}
$output[] = http_build_query($params);
return $output;
default:
return ["\t$this->body"];
}
}
/**
* @inheritDoc
*/
public function __toString(): string
{
$output = array_merge(
['#!/bin/sh'],
$this->prepareDescription(),
[
'wget \ ',
"\t--no-check-certificate \ ",
"\t--quiet \ ",
"\t--timeout=0 \ ",
"\t--method $this->verb \ ",
],
$this->prepareHeaders(),
$this->prepareBody()
);
$output[] = rtrim(array_pop($output), '\ ');
$output[] = "\t'$this->url'";
return implode(PHP_EOL, $output);
return implode(PHP_EOL, array_merge($output, ['']));
}
}