1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Phauthentic\Presentation\View; |
5
|
|
|
|
6
|
|
|
use RuntimeException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* View Data Transfer Object |
10
|
|
|
* |
11
|
|
|
* A Data Transfer Object that is passed from the application layer to a |
12
|
|
|
* renderer to render something. |
13
|
|
|
*/ |
14
|
|
|
class View implements ViewInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* View vars |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $viewVars = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Template Path |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $templatePath = ''; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Layout Path |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $layoutPath = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Template |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $template = ''; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Layout |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $layout = ''; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Template File Extension |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $templateFileExtension = 'php'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
*/ |
61
|
1 |
|
public function setVars(array $vars, bool $merge = true): ViewInterface |
62
|
|
|
{ |
63
|
1 |
|
if ($merge) { |
64
|
1 |
|
$this->viewVars = array_merge($this->viewVars, $vars); |
65
|
|
|
} else { |
66
|
|
|
$this->viewVars = $vars; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
4 |
|
public function setVar(string $name, $value): ViewInterface |
76
|
|
|
{ |
77
|
4 |
|
if (isset($this->viewVars[$name])) { |
78
|
|
|
trigger_error( |
79
|
|
|
sprintf('A variable `%s` is already set', $name), |
80
|
|
|
E_USER_NOTICE |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
$this->viewVars[$name] = $value; |
85
|
|
|
|
86
|
4 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
4 |
|
public function setTemplate(string $template): ViewInterface |
93
|
|
|
{ |
94
|
4 |
|
$this->template = $template; |
95
|
|
|
|
96
|
4 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
|
|
public function setLayout(string $layout): ViewInterface |
103
|
|
|
{ |
104
|
|
|
$this->layout = $layout; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritDoc |
111
|
|
|
*/ |
112
|
1 |
|
public function setTemplatePath(string $path): ViewInterface |
113
|
|
|
{ |
114
|
1 |
|
$this->templatePath = $path; |
115
|
|
|
|
116
|
1 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @inheritDoc |
121
|
|
|
*/ |
122
|
|
|
public function setLayoutPath(string $path): ViewInterface |
123
|
|
|
{ |
124
|
|
|
$this->layoutPath = $path; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
3 |
|
public function getTemplate(): string |
133
|
|
|
{ |
134
|
3 |
|
if (empty($this->template)) { |
135
|
|
|
throw new RuntimeException('No template specified for view'); |
136
|
|
|
} |
137
|
|
|
|
138
|
3 |
|
return $this->template; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritDoc |
143
|
|
|
*/ |
144
|
|
|
public function getLayout(): string |
145
|
|
|
{ |
146
|
|
|
if (empty($this->template)) { |
147
|
|
|
throw new RuntimeException('No layout specified for view'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->layout; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritDoc |
155
|
|
|
*/ |
156
|
2 |
|
public function getTemplatePath(): string |
157
|
|
|
{ |
158
|
2 |
|
return $this->templatePath; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @inheritDoc |
163
|
|
|
*/ |
164
|
|
|
public function getLayoutPath(): string |
165
|
|
|
{ |
166
|
|
|
return $this->layoutPath; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @inheritDoc |
171
|
|
|
*/ |
172
|
4 |
|
public function getViewVars(): array |
173
|
|
|
{ |
174
|
4 |
|
return $this->viewVars; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|