HtmlResponseFormatter.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\web;
  8. use yii\base\Component;
  9. /**
  10. * HtmlResponseFormatter formats the given data into an HTML response content.
  11. *
  12. * It is used by [[Response]] to format response data.
  13. *
  14. * @author Qiang Xue <qiang.xue@gmail.com>
  15. * @since 2.0
  16. */
  17. class HtmlResponseFormatter extends Component implements ResponseFormatterInterface
  18. {
  19. /**
  20. * @var string the Content-Type header for the response
  21. */
  22. public $contentType = 'text/html';
  23. /**
  24. * Formats the specified response.
  25. * @param Response $response the response to be formatted.
  26. */
  27. public function format($response)
  28. {
  29. if (stripos($this->contentType, 'charset') === false) {
  30. $this->contentType .= '; charset=' . $response->charset;
  31. }
  32. $response->getHeaders()->set('Content-Type', $this->contentType);
  33. if ($response->data !== null) {
  34. $response->content = $response->data;
  35. }
  36. }
  37. }