Passed
Push — 6.0 ( 3dc798...7ca19f )
by liu
02:48
created

View::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
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
class View extends Response
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class View
Loading history...
18
{
19
    // 输出参数
20
    protected $options = [];
21
    protected $vars    = [];
22
    protected $filter;
23
    protected $contentType = 'text/html';
24
    protected $view;
25
26
    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...
27
    {
28
        parent::__construct($data, $code);
29
        $this->view = $view;
30
    }
31
32
    /**
33
     * 处理数据
34
     * @access protected
35
     * @param  mixed $data 要处理的数据
36
     * @return string
37
     */
38
    protected function output($data): string
39
    {
40
        // 渲染模板输出
41
        return $this->view->filter($this->filter)
42
            ->assign($this->vars)
43
            ->fetch($data);
44
    }
45
46
    /**
47
     * 获取视图变量
48
     * @access public
49
     * @param  string $name 模板变量
50
     * @return mixed
51
     */
52
    public function getVars(string $name = null)
53
    {
54
        if (is_null($name)) {
55
            return $this->vars;
56
        } else {
57
            return $this->vars[$name] ?? null;
58
        }
59
    }
60
61
    /**
62
     * 模板变量赋值
63
     * @access public
64
     * @param  array $vars  变量
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
65
     * @return $this
66
     */
67
    public function assign(array $vars)
68
    {
69
        $this->vars = array_merge($this->vars, $vars);
70
71
        return $this;
72
    }
73
74
    /**
75
     * 视图内容过滤
76
     * @access public
77
     * @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...
78
     * @return $this
79
     */
80
    public function filter(callable $filter = null)
81
    {
82
        $this->filter = $filter;
83
        return $this;
84
    }
85
86
    /**
87
     * 检查模板是否存在
88
     * @access public
89
     * @param  string  $name 模板名
90
     * @return bool
91
     */
92
    public function exists(string $name): bool
93
    {
94
        return $this->view->exists($name);
95
    }
96
97
}
98