WIP
This commit is contained in:
112
src/Converters/OpenCollection/OpenCollectionRequest.php
Normal file
112
src/Converters/OpenCollection/OpenCollectionRequest.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PmConverter\Converters\OpenCollection;
|
||||
|
||||
use PmConverter\Converters\Abstract\AbstractRequest;
|
||||
use PmConverter\Exceptions\EmptyHttpVerbException;
|
||||
|
||||
/**
|
||||
* Class to determine file content with OpenCollection format
|
||||
*/
|
||||
class OpenCollectionRequest extends AbstractRequest
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareDescription(): array
|
||||
{
|
||||
return empty($this->description)
|
||||
? []
|
||||
: ['description: |', ' ' . str_replace("\n", "\n ", $this->description), ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws EmptyHttpVerbException
|
||||
*/
|
||||
protected function prepareHeaders(): array
|
||||
{
|
||||
$output = ['headers:'];
|
||||
foreach ($this->headers as $name => $data) {
|
||||
$prefix = $data['disabled'] ? '# ' : '';
|
||||
$output[] = sprintf('%s %s: %s', $prefix, $name, $data['value']);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function prepareBody(): array
|
||||
{
|
||||
$output = ['body:'];
|
||||
|
||||
switch ($this->getBodymode()) {
|
||||
case 'formdata':
|
||||
$output[] = ' type: formdata';
|
||||
$output[] = ' files:';
|
||||
foreach ($this->body as $key => $data) {
|
||||
$disabled = $data['disabled'] ?? false;
|
||||
$prefix = $disabled ? '# ' : ' ';
|
||||
if ($data['type'] === 'file') {
|
||||
$output[] = sprintf('%s- name: %s', $prefix, $key);
|
||||
$output[] = sprintf('%s type: file', $prefix);
|
||||
$output[] = sprintf('%s value: %s', $prefix, $data['value']);
|
||||
} else {
|
||||
$output[] = sprintf('%s- name: %s', $prefix, $key);
|
||||
$output[] = sprintf('%s type: text', $prefix);
|
||||
$output[] = sprintf('%s value: %s', $prefix, $data['value']);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$output[] = ' type: raw';
|
||||
if (!empty($this->body)) {
|
||||
$output[] = ' content: |';
|
||||
$bodyContent = is_string($this->body) ? $this->body : json_encode($this->body, JSON_PRETTY_PRINT);
|
||||
foreach (explode("\n", $bodyContent) as $line) {
|
||||
$output[] = ' ' . $line;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws EmptyHttpVerbException
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
$output = [
|
||||
'name: ' . $this->getName(),
|
||||
'type: request',
|
||||
'',
|
||||
];
|
||||
|
||||
$output[] = 'method: ' . strtolower($this->getVerb());
|
||||
$output[] = '';
|
||||
|
||||
$url = $this->getRawUrl();
|
||||
$output[] = 'url: ' . $url;
|
||||
$output[] = '';
|
||||
|
||||
$output = array_merge($output, $this->prepareDescription());
|
||||
|
||||
if (!empty($this->headers)) {
|
||||
$output = array_merge($output, $this->prepareHeaders());
|
||||
$output[] = '';
|
||||
}
|
||||
|
||||
if ($this->verb !== 'GET' && !empty($this->body)) {
|
||||
$output = array_merge($output, $this->prepareBody());
|
||||
$output[] = '';
|
||||
}
|
||||
|
||||
return implode("\n", $output);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user