View::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php /** @noinspection PhpIncludeInspection */
2
3
declare(strict_types=1);
4
5
namespace Tebe\Pvc\View;
6
7
use Tebe\Pvc\Exception\SystemException;
8
9
class View
10
{
11
    // phpcs:disable
12
13
    /**
14
     * @var ViewHelpers
15
     */
16
    private $helpers;
17
18
    /**
19
     * @var string
20
     */
21
    private $__viewsPath;
22
23
    /**
24
     * @var array
25
     */
26
    private $__vars;
27
28
    // phpcs:enable
29
30
    /**
31
     * View constructor.
32
     * @param string $viewsPath
33
     * @param ViewHelpers $helpers
34
     * @throws SystemException
35
     */
36
    public function __construct(string $viewsPath, ViewHelpers $helpers)
37
    {
38
        $this->setViewsPath($viewsPath);
39
        $this->helpers = $helpers;
40
        $this->__vars = [];
41
    }
42
43
    /**
44
     * @param string $__viewRoute
45
     * @param array $__params
46
     * @return string
47
     * @throws SystemException
48
     */
49
    public function render(string $__viewRoute, array $__params = []): string
50
    {
51
        $__viewPath = $this->resolvePath($__viewRoute);
52
        if (!is_file($__viewPath)) {
53
            throw SystemException::fileNotExist($__viewPath);
54
        }
55
        extract($__params);
56
        ob_start();
57
        require $__viewPath;
58
        $html = ob_get_clean();
59
        return $html;
60
    }
61
62
    /**
63
     * @param string $viewRoute
64
     * @return string
65
     */
66
    private function resolvePath(string $viewRoute): string
67
    {
68
        $viewPath = sprintf(
69
            '%s/%s.php',
70
            $this->__viewsPath,
71
            $viewRoute
72
        );
73
        return $viewPath;
74
    }
75
76
    /**
77
     * @param string $viewRoute
78
     * @return bool
79
     */
80
    public function fileExist(string $viewRoute): bool
81
    {
82
        $viewPath = $this->resolvePath($viewRoute);
83
        return is_file($viewPath);
84
    }
85
86
    /**
87
     * @return string
88
     */
89
    public function getViewsPath(): string
90
    {
91
        return $this->__viewsPath;
92
    }
93
94
    /**
95
     * @param string $viewsPath
96
     * @throws SystemException
97
     */
98
    private function setViewsPath(string $viewsPath)
99
    {
100
        if (!is_dir($viewsPath)) {
101
            throw SystemException::directoryNotExist($viewsPath);
102
        }
103
        $this->__viewsPath = $viewsPath;
104
    }
105
106
    /**
107
     * @param string $name
108
     * @param array $args
109
     * @return mixed
110
     */
111
    public function __call(string $name, array $args)
112
    {
113
        $callable = $this->helpers->get($name);
114
        return $callable(...$args);
115
    }
116
117
    /**
118
     * Register a new template function.
119
     * @param  string   $name;
120
     * @param  callback $callback;
121
     * @return $this
122
     */
123
    public function registerHelper(string $name, callable $callback): self
124
    {
125
        $this->helpers->add($name, $callback);
126
        return $this;
127
    }
128
129
    /**
130
     * Remove a template function.
131
     * @param  string $name;
132
     * @return $this
133
     */
134
    public function removeHelper(string $name): self
135
    {
136
        $this->helpers->remove($name);
137
        return $this;
138
    }
139
140
    /**
141
     * Get a template function.
142
     * @param  string $name
143
     * @return callable
144
     */
145
    public function getHelper(string $name): callable
146
    {
147
        return $this->helpers->get($name);
148
    }
149
150
    /**
151
     * Check if a template function exists.
152
     * @param  string  $name
153
     * @return boolean
154
     */
155
    public function doesHelperExist(string $name): bool
156
    {
157
        return $this->helpers->exists($name);
158
    }
159
160
    /**
161
     * @param ViewExtension $extension
162
     * @return $this
163
     */
164
    public function registerExtension(ViewExtension $extension): self
165
    {
166
        $extension->register($this);
167
        return $this;
168
    }
169
170
    /**
171
     * @param string $name
172
     * @return mixed|null
173
     */
174
    public function __get(string $name)
175
    {
176
        if (array_key_exists($name, $this->__vars)) {
177
            return $this->__vars[$name];
178
        }
179
        return null;
180
    }
181
182
    /**
183
     * @param string $name
184
     * @param mixed $value
185
     */
186
    public function __set(string $name, $value): void
187
    {
188
        $this->__vars[$name] = $value;
189
    }
190
191
    /**
192
     * @param string $name
193
     * @return bool
194
     */
195
    public function __isset(string $name): bool
196
    {
197
        return isset($this->__vars[$name]);
198
    }
199
200
    /**
201
     * @param string $name
202
     */
203
    public function __unset(string $name): void
204
    {
205
        unset($this->__vars[$name]);
206
    }
207
}
208