Skip to content

Commit

Permalink
Add StringResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz authored and Milan Felix Šulc committed Feb 6, 2019
1 parent 4fb1783 commit 8b2288a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [PSR7StreamResponse](#flyresponse)
- [FlyResponse - send file/buffer on-the-fly](#flyresponse)
- [XmlResponse](#xmlresponse)
- [StringResponse](#stringresponse)

## UI

Expand Down Expand Up @@ -62,6 +63,7 @@ class YourPresenter extends Presenter
- PSR7StreamResponse
- FlyResponse
- XmlResponse
- StringResponse

### CSVResponse

Expand Down Expand Up @@ -107,6 +109,14 @@ There are 2 types of fly response:
```php
$presenter->sendResponse(new XmlResponse($xml));
```
### StringResponse

```php
$response = new StringResponse($pdfString, 'invoice.pdf', 'application/pdf');
$response->setAttachment(); // browser download the file

$presenter->sendResponse($response);
```

### Adapters

Expand Down
60 changes: 60 additions & 0 deletions src/Response/StringResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace Contributte\Application\Response;

use Nette;
use Nette\Application\IResponse;

final class StringResponse implements IResponse
{

/** @var string */
private $content;

/**
* Name of downloading file.
*
* @var string
*/
private $name;

/**
* Content-Type of the contents.
*
* @var string
*/
private $contentType;

/**
* Contents as http attachment?
*
* @var bool
*/
private $attachment = false;

public function __construct(string $content, string $name, string $contentType = 'text/plain')
{
$this->content = $content;
$this->name = $name;
$this->contentType = $contentType;
}

public function setAttachment(bool $attachment = true): self
{
$this->attachment = $attachment;

return $this;
}

public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse): void
{
$httpResponse->addHeader('Content-Type', $this->contentType);
$httpResponse->addHeader(
'Content-Disposition',
($this->attachment ? 'attachment;' : '') . 'filename="' . $this->name . '"'
);

echo $this->content;
}

}

0 comments on commit 8b2288a

Please # to comment.