Completed
Push — 6.0 ( 2254d3...b94f98 )
by liu
02:43
created

View::getVars()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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
12
namespace think\response;
13
14
use think\Response;
15
use think\View as BaseView;
16
17
/**
18
 * View Response
19
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
20
class View extends Response
21
{
22
    /**
23
     * 输出参数
24
     * @var array
25
     */
26
    protected $options = [];
27
28
    /**
29
     * 输出变量
30
     * @var array
31
     */
32
    protected $vars = [];
33
34
    /**
35
     * 输出过滤
36
     * @var mixed
37
     */
38
    protected $filter;
39
40
    /**
41
     * 输出type
42
     * @var string
43
     */
44
    protected $contentType = 'text/html';
45
46
    /**
47
     * View对象
48
     * @var BaseView
49
     */
50
    protected $view;
51
52
    /**
53
     * 是否内容渲染
54
     * @var bool
55
     */
56
    protected $isContent = false;
57
58
    public function __construct(BaseView $view, $data = '', int $code = 200)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
59
    {
60
        parent::__construct($data, $code);
61
        $this->view = $view;
62
    }
63
64
    /**
65
     * 设置是否为内容渲染
66
     * @access public
67
     * @param  bool $content
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
68
     * @return $this
69
     */
70
    public function isContent(bool $content = true)
71
    {
72
        $this->isContent = $content;
73
        return $this;
74
    }
75
76
    /**
77
     * 处理数据
78
     * @access protected
79
     * @param  mixed $data 要处理的数据
80
     * @return string
81
     */
82
    protected function output($data): string
83
    {
84
        // 渲染模板输出
85
        return $this->view->filter($this->filter)
86
            ->assign($this->vars)
87
            ->fetch($data, $this->isContent);
88
    }
89
90
    /**
91
     * 获取视图变量
92
     * @access public
93
     * @param  string $name 模板变量
94
     * @return mixed
95
     */
96
    public function getVars(string $name = null)
97
    {
98
        if (is_null($name)) {
99
            return $this->vars;
100
        } else {
101
            return $this->vars[$name] ?? null;
102
        }
103
    }
104
105
    /**
106
     * 模板变量赋值
107
     * @access public
108
     * @param  array $vars  变量
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
109
     * @return $this
110
     */
111
    public function assign(array $vars)
112
    {
113
        $this->vars = array_merge($this->vars, $vars);
114
115
        return $this;
116
    }
117
118
    /**
119
     * 视图内容过滤
120
     * @access public
121
     * @param callable $filter
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
122
     * @return $this
123
     */
124
    public function filter(callable $filter = null)
125
    {
126
        $this->filter = $filter;
127
        return $this;
128
    }
129
130
    /**
131
     * 检查模板是否存在
132
     * @access public
133
     * @param  string  $name 模板名
134
     * @return bool
135
     */
136
    public function exists(string $name): bool
137
    {
138
        return $this->view->exists($name);
139
    }
140
141
}
142