View::addAssets()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 2
c 4
b 0
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace DevPontes\View;
4
5
use DevPontes\View\Exception\ErrorRender;
6
7
/**
8
 * Class DevPontes View
9
 *
10
 * @author Moises Pontes <[email protected]>
11
 * @package DevPontes\View
12
 */
13
class View
14
{
15
    /** @var string */
16
    private $head;
17
18
    /** @var string */
19
    private $aside;
20
21
    /** @var string */
22
    private $header;
23
24
    /** @var string */
25
    private $footer;
26
27
    private array $data;
28
    public Assets $assets;
29
    private string $viewPath;
30
    private string $extension;
31
32
    /**
33
     * View constructor.
34
     *
35
     * @param string $viewPath ex: src/views
36
     * @param string $extension php or html or other
37
     */
38
    public function __construct(string $viewPath, string $extension)
39
    {
40
        $this->viewPath = $viewPath;
41
        $this->extension = $extension;
42
    }
43
44
    public function __get($name)
45
    {
46
        return $this->data[$name] ?? null;
47
    }
48
49
    public function __isset($name)
50
    {
51
        return isset($this->data[$name]);
52
    }
53
54
    /**
55
     * View data
56
     *
57
     * @return object
58
     */
59
    public function data(): object
60
    {
61
        return json_decode(json_encode($this->data), false);
62
    }
63
64
    /**
65
     * Define head view
66
     *
67
     * @param string $head
68
     * @return View
69
     */
70
    public function setHead(string $head): View
71
    {
72
        $this->head = $head;
73
        return $this;
74
    }
75
76
    /**
77
     * Define header view
78
     *
79
     * @param string $header
80
     * @return View
81
     */
82
    public function setHeader(string $header): View
83
    {
84
        $this->header = $header;
85
        return $this;
86
    }
87
88
    /**
89
     * Define aside view
90
     *
91
     * @param string $aside
92
     * @return View
93
     */
94
    public function setAside(string $aside): View
95
    {
96
        $this->aside = $aside;
97
        return $this;
98
    }
99
100
    /**
101
     * Define footer view
102
     *
103
     * @param string $footer
104
     * @return View
105
     */
106
    public function setFooter(string $footer): View
107
    {
108
        $this->footer = $footer;
109
        return $this;
110
    }
111
112
    /**
113
     * Render the View
114
     * Data array will be converted to stdClass object
115
     *
116
     * @param string $view
117
     * @param array $data
118
     * @return void
119
     * @example - $v->render('home', array('data' => [...]));
120
     */
121
    public function render(string $view, array $data = []): void
122
    {
123
        $this->data = $data;
124
125
        extract($this->data);
126
127
        if ($this->head) {
128
            include $this->resolvePath($this->head);
129
        }
130
131
        if ($this->header) {
132
            include $this->resolvePath($this->header);
133
        }
134
135
        if ($this->aside) {
136
            include $this->resolvePath($this->aside);
137
        }
138
139
        include $this->resolvePath($view);
140
141
        if ($this->footer) {
142
            include $this->resolvePath($this->footer);
143
        }
144
    }
145
146
    /**
147
     * Insert View
148
     *
149
     * @param string $view
150
     * @param string $extension
151
     * @return void
152
     */
153
    public function insert(string $view, string $extension = ''): void
154
    {
155
        extract($this->data);
156
157
        $this->extension = $extension ?: $this->extension;
158
159
        include $this->resolvePath($view);
160
    }
161
162
    /**
163
     * @param string $view
164
     * @return string
165
     */
166
    private function resolvePath(string $view): string
167
    {
168
        if (empty($view)) {
169
            throw new ErrorRender("View name cannot be empty");
170
        }
171
172
        $bar  = DIRECTORY_SEPARATOR;
173
        $view = $view[0] == '.' ? ltrim($view, '.') : $view;
174
        $file = $this->viewPath . $bar . str_replace('.', $bar, $view) . '.' . $this->extension;
175
176
        if (!file_exists($file)) {
177
            throw new ErrorRender("Error loading view {$file}");
178
        }
179
180
        return $file;
181
    }
182
183
    /**
184
     * Assets class injection
185
     *
186
     * @param Assets $assets
187
     * @return View
188
     */
189
    public function addAssets(Assets $assets): View
190
    {
191
        $this->assets = $assets;
192
        return $this;
193
    }
194
}
195