<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\response;
use think\Cookie;
use think\Response;
use think\View as BaseView;
class View extends Response
{
protected $options = [];
protected $vars = [];
protected $filter;
protected $contentType = 'text/html';
protected $view;
protected $isContent = false;
public function __construct(Cookie $cookie, BaseView $view, $data = '', int $code = 200)
{
$this->init($data, $code);
$this->cookie = $cookie;
$this->view = $view;
}
public function isContent(bool $content = true)
{
$this->isContent = $content;
return $this;
}
protected function output($data): string
{
return $this->view->filter($this->filter)
->fetch($data, $this->vars, $this->isContent);
}
public function getVars(string $name = null)
{
if (is_null($name)) {
return $this->vars;
} else {
return $this->vars[$name] ?? null;
}
}
public function assign($name, $value = null)
{
if (is_array($name)) {
$this->vars = array_merge($this->vars, $name);
} else {
$this->vars[$name] = $value;
}
return $this;
}
public function filter(callable $filter = null)
{
$this->filter = $filter;
return $this;
}
public function exists(string $name): bool
{
return $this->view->exists($name);
}
}