Completed
Push — 6.0 ( 4d37f8...8a13c8 )
by liu
08:28
created

View::assign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 2.032
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\response;
14
15
use think\Cookie;
16
use think\Response;
17
use think\View as BaseView;
18
19
/**
20
 * View Response
21
 */
22
class View extends Response
23
{
24
    /**
25
     * 输出参数
26
     * @var array
27
     */
28
    protected $options = [];
29
30
    /**
31
     * 输出变量
32
     * @var array
33
     */
34
    protected $vars = [];
35
36
    /**
37
     * 输出过滤
38
     * @var mixed
39
     */
40
    protected $filter;
41
42
    /**
43
     * 输出type
44
     * @var string
45
     */
46
    protected $contentType = 'text/html';
47
48
    /**
49
     * View对象
50
     * @var BaseView
51
     */
52
    protected $view;
53
54
    /**
55
     * 是否内容渲染
56
     * @var bool
57
     */
58
    protected $isContent = false;
59
60 3
    public function __construct(Cookie $cookie, BaseView $view, $data = '', int $code = 200)
61
    {
62 3
        $this->init($data, $code);
63
64 3
        $this->cookie = $cookie;
65 3
        $this->view   = $view;
66 3
    }
67
68
    /**
69
     * 设置是否为内容渲染
70
     * @access public
71
     * @param  bool $content
72
     * @return $this
73
     */
74
    public function isContent(bool $content = true)
75
    {
76
        $this->isContent = $content;
77
        return $this;
78
    }
79
80
    /**
81
     * 处理数据
82
     * @access protected
83
     * @param  mixed $data 要处理的数据
84
     * @return string
85
     */
86
    protected function output($data): string
87
    {
88
        // 渲染模板输出
89
        $this->view->filter($this->filter);
90
        return $this->isContent ?
91
        $this->view->display($data, $this->vars) :
92
        $this->view->fetch($data, $this->vars);
93
    }
94
95
    /**
96
     * 获取视图变量
97
     * @access public
98
     * @param  string $name 模板变量
99
     * @return mixed
100
     */
101 3
    public function getVars(string $name = null)
102
    {
103 3
        if (is_null($name)) {
104 3
            return $this->vars;
105
        } else {
106
            return $this->vars[$name] ?? null;
107
        }
108
    }
109
110
    /**
111
     * 模板变量赋值
112
     * @access public
113
     * @param  string|array $name  模板变量
114
     * @param  mixed        $value 变量值
115
     * @return $this
116
     */
117 3
    public function assign($name, $value = null)
118
    {
119 3
        if (is_array($name)) {
120 3
            $this->vars = array_merge($this->vars, $name);
121
        } else {
122
            $this->vars[$name] = $value;
123
        }
124
125 3
        return $this;
126
    }
127
128
    /**
129
     * 视图内容过滤
130
     * @access public
131
     * @param callable $filter
132
     * @return $this
133
     */
134
    public function filter(callable $filter = null)
135
    {
136
        $this->filter = $filter;
137
        return $this;
138
    }
139
140
    /**
141
     * 检查模板是否存在
142
     * @access public
143
     * @param  string  $name 模板名
144
     * @return bool
145
     */
146
    public function exists(string $name): bool
147
    {
148
        return $this->view->exists($name);
0 ignored issues
show
Bug introduced by
The method exists() does not exist on think\View. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        return $this->view->/** @scrutinizer ignore-call */ exists($name);
Loading history...
149
    }
150
151
}
152