Refactorings

- important curl and wget improvements
- initial test coverage
- setters and getters in Request classes
- namespace fixes
- some additions in README
- docblocks and code-style
This commit is contained in:
2023-09-10 09:03:54 +08:00
parent af9c360684
commit 1345b7eddb
26 changed files with 2901 additions and 300 deletions

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace PmConverter\Converters\Curl;
use PmConverter\Converters\{
Abstract\AbstractConverter,
ConverterContract};
class CurlConverter extends AbstractConverter implements ConverterContract
{
protected const FILE_EXT = 'sh';
protected const OUTPUT_DIR = 'curl';
protected const REQUEST_CLASS = CurlRequest::class;
}

View File

@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace PmConverter\Converters\Curl;
use PmConverter\Converters\Abstract\AbstractRequest;
/**
* Class to determine file content with curl request format
*/
class CurlRequest extends AbstractRequest
{
/**
* @inheritDoc
*/
protected function prepareDescription(): array
{
return empty($this->description)
? []
: ['# ' . str_replace("\n", "\n# ", $this->description), ''];
}
/**
* @inheritDoc
*/
protected function prepareHeaders(): array
{
$output = [];
foreach ($this->headers as $header_key => $header) {
if ($header['disabled']) {
continue;
}
$output[] = sprintf("\t--header '%s: %s' \ ", $header_key, $header['value']);
}
return $output;
}
/**
* @inheritDoc
*/
protected function prepareBody(): array
{
$output = [];
switch ($this->bodymode) {
case 'formdata':
foreach ($this->body as $key => $data) {
$output[] = sprintf(
"%s\t--form '%s=%s' \ ",
isset($data['disabled']) ? '# ' : '',
$key,
$data['type'] === 'file' ? "@" . $data['value'] : $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, array_merge($output, ['']));
}
}