DigiComp.AttachmentViewUtility/Classes/AttachmentViewTrait.php
Ferdinand Kuhl 57d3efe1df
All checks were successful
ci/woodpecker/push/code-style Pipeline was successful
ci/woodpecker/push/functional-tests Pipeline was successful
Initial version
2023-10-17 22:52:28 +02:00

61 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace DigiComp\AttachmentViewUtility;
use cardinalby\ContentDisposition\ContentDisposition;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
trait AttachmentViewTrait
{
abstract protected function getAttachmentName(): string;
/**
* @return string|resource|StreamInterface|null
*/
abstract protected function getAttachmentContent();
abstract protected function getAttachmentMimeType(): string;
protected function addOptionsForAttachment(): void
{
if (\property_exists($this, 'supportedOptions')) {
$this->supportedOptions['attachmentCharset'] = [
'utf-8',
'Charset of the content or FALSE if you want to suppress the information in header',
'string|false',
];
$this->supportedOptions['attachmentDisposition'] = [
'attachment',
'One of "inline" or "attachment"',
'string',
];
} else {
throw new \RuntimeException('supported option could not be set', 1697552694);
}
}
public function render(): ResponseInterface
{
if ($this->options['attachmentCharset'] === false) {
$charset = '';
} else {
$charset = '; charset=' . $this->options['attachmentCharset'];
}
return new Response(
200,
[
'Content-Disposition' => ContentDisposition::create(
$this->getAttachmentName(),
true,
$this->options['attachmentDisposition']
)->format(),
'Content-Type' => $this->getAttachmentMimeType() . $charset,
],
$this->getAttachmentContent()
);
}
}