Passed
Push — 5.2 ( ab77ce...f44aef )
by liu
02:47
created

View   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 71
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A output() 0 6 1
A filter() 0 4 1
A getVars() 0 6 2
A assign() 0 5 1
A exists() 0 3 1
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
16
class View extends Response
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
17
{
18
    // 输出参数
19
    protected $options = [];
20
    protected $vars    = [];
21
    protected $filter;
22
    protected $contentType = 'text/html';
23
24
    /**
25
     * 处理数据
26
     * @access protected
27
     * @param  mixed $data 要处理的数据
28
     * @return string
29
     */
30
    protected function output($data): string
31
    {
32
        // 渲染模板输出
33
        return \think\facade\View::filter($this->filter)
34
            ->assign($this->vars)
35
            ->fetch($data);
36
    }
37
38
    /**
39
     * 获取视图变量
40
     * @access public
41
     * @param  string $name 模板变量
42
     * @return mixed
43
     */
44
    public function getVars(string $name = null)
45
    {
46
        if (is_null($name)) {
47
            return $this->vars;
48
        } else {
49
            return $this->vars[$name] ?? null;
50
        }
51
    }
52
53
    /**
54
     * 模板变量赋值
55
     * @access public
56
     * @param  array $vars  变量
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
57
     * @return $this
58
     */
59
    public function assign(array $vars)
60
    {
61
        $this->vars = array_merge($this->vars, $vars);
62
63
        return $this;
64
    }
65
66
    /**
67
     * 视图内容过滤
68
     * @access public
69
     * @param callable $filter
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
70
     * @return $this
71
     */
72
    public function filter(callable $filter = null)
73
    {
74
        $this->filter = $filter;
75
        return $this;
76
    }
77
78
    /**
79
     * 检查模板是否存在
80
     * @access public
81
     * @param  string  $name 模板名
82
     * @return bool
83
     */
84
    public function exists(string $name): bool
85
    {
86
        return \think\facade\View::exists($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return think\facade\View::exists($name) returns the type think\View which is incompatible with the type-hinted return boolean.
Loading history...
87
    }
88
89
}
90