-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseFormatter.php
45 lines (42 loc) · 1.28 KB
/
BaseFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
namespace boundstate\htmlconverter;
use yii\base\Component;
use yii\web\Response;
use yii\web\ResponseFormatterInterface;
use Yii;
/**
* BaseFormatter is the base class for the PDF and image response formatters.
*
* @author Bound State Software <[email protected]>
*/
abstract class BaseFormatter extends Component implements ResponseFormatterInterface
{
/**
* @var string the name of the app component to perform the conversion
*/
public $converter;
/**
* @var string the Content-Type header for the response
*/
public $contentType;
/**
* @var array options (overrides the converter options)
*/
public $options = [];
/**
* @var string the filename if the response should be a file download
*/
public $filename;
/**
* Formats the specified response.
* @param Response $response the response to be formatted.
*/
public function format($response)
{
$response->getHeaders()->set('Content-Type', $this->contentType);
if ($this->filename !== null) {
$response->getHeaders()->set('Content-Disposition', "attachment; filename=\"{$this->filename}\"");
}
$response->content = Yii::$app->{$this->converter}->convert($response->data, $this->options);
}
}