1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\View\State; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\View\Theme; |
9
|
|
|
|
10
|
|
|
use function array_key_exists; |
11
|
|
|
use function func_get_args; |
12
|
|
|
use function is_array; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @internal |
16
|
|
|
*/ |
17
|
|
|
trait StateTrait |
18
|
|
|
{ |
19
|
|
|
private ?Theme $theme = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array Parameters that are common for all view templates. |
23
|
|
|
* @psalm-var array<string, mixed> |
24
|
|
|
*/ |
25
|
|
|
private array $parameters = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array Named content blocks that are common for all view templates. |
29
|
|
|
* @psalm-var array<string, string> |
30
|
|
|
*/ |
31
|
|
|
private array $blocks = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the specified theme instance. |
35
|
|
|
* |
36
|
|
|
* @param Theme|null $theme The theme instance or `null` for reset theme. |
37
|
|
|
* |
38
|
|
|
* @return static |
39
|
|
|
*/ |
40
|
1 |
|
public function setTheme(?Theme $theme): self |
41
|
|
|
{ |
42
|
1 |
|
$this->theme = $theme; |
43
|
1 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Gets the theme instance, or `null` if no theme has been set. |
48
|
|
|
* |
49
|
|
|
* @return Theme|null The theme instance, or `null` if no theme has been set. |
50
|
|
|
*/ |
51
|
60 |
|
public function getTheme(): ?Theme |
52
|
|
|
{ |
53
|
60 |
|
return $this->theme; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets a common parameters that is accessible in all view templates. |
58
|
|
|
* |
59
|
|
|
* @param array $parameters Parameters that are common for all view templates. |
60
|
|
|
* |
61
|
|
|
* @psalm-param array<string, mixed> $parameters |
62
|
|
|
* |
63
|
|
|
* @return static |
64
|
|
|
* |
65
|
|
|
* @see setParameter() |
66
|
|
|
*/ |
67
|
3 |
|
public function setParameters(array $parameters): self |
68
|
|
|
{ |
69
|
|
|
/** @var mixed $value */ |
70
|
3 |
|
foreach ($parameters as $id => $value) { |
71
|
1 |
|
$this->setParameter($id, $value); |
72
|
|
|
} |
73
|
3 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sets a common parameter that is accessible in all view templates. |
78
|
|
|
* |
79
|
|
|
* @param string $id The unique identifier of the parameter. |
80
|
|
|
* @param mixed $value The value of the parameter. |
81
|
|
|
* |
82
|
|
|
* @return static |
83
|
|
|
*/ |
84
|
15 |
|
public function setParameter(string $id, $value): self |
85
|
|
|
{ |
86
|
15 |
|
$this->parameters[$id] = $value; |
87
|
15 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add values to end of common array parameter. If specified parameter does not exist or him is not array, |
92
|
|
|
* then parameter will be added as empty array. |
93
|
|
|
* |
94
|
|
|
* @param string $id The unique identifier of the parameter. |
95
|
|
|
* @param mixed ...$value Value(s) for add to end of array parameter. |
96
|
|
|
* |
97
|
|
|
* @throws InvalidArgumentException When specified parameter already exists and is not an array. |
98
|
|
|
* |
99
|
|
|
* @return static |
100
|
|
|
*/ |
101
|
6 |
|
public function addToParameter(string $id, ...$value): self |
102
|
|
|
{ |
103
|
|
|
/** @var mixed $array */ |
104
|
6 |
|
$array = $this->parameters[$id] ?? []; |
105
|
6 |
|
if (!is_array($array)) { |
106
|
1 |
|
throw new InvalidArgumentException( |
107
|
1 |
|
sprintf('The "%s" parameter already exists and is not an array.', $id) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
$this->setParameter($id, array_merge($array, $value)); |
112
|
|
|
|
113
|
5 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Removes a common parameter. |
118
|
|
|
* |
119
|
|
|
* @param string $id The unique identifier of the parameter. |
120
|
|
|
* |
121
|
|
|
* @return static |
122
|
|
|
*/ |
123
|
3 |
|
public function removeParameter(string $id): self |
124
|
|
|
{ |
125
|
3 |
|
unset($this->parameters[$id]); |
126
|
3 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Gets a common parameter value by ID. |
131
|
|
|
* |
132
|
|
|
* @param string $id The unique identifier of the parameter. |
133
|
|
|
* @param mixed $default The default value to be returned if the specified parameter does not exist. |
134
|
|
|
* |
135
|
|
|
* @throws InvalidArgumentException If specified parameter does not exist and not passed default value. |
136
|
|
|
* |
137
|
|
|
* @return mixed The value of the parameter. |
138
|
|
|
*/ |
139
|
8 |
|
public function getParameter(string $id) |
140
|
|
|
{ |
141
|
8 |
|
if (isset($this->parameters[$id])) { |
142
|
6 |
|
return $this->parameters[$id]; |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
$args = func_get_args(); |
146
|
3 |
|
if (array_key_exists(1, $args)) { |
147
|
1 |
|
return $args[1]; |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
throw new InvalidArgumentException('Parameter "' . $id . '" not found.'); |
151
|
|
|
} |
152
|
|
|
|
153
|
60 |
|
public function getParameters(): array |
154
|
|
|
{ |
155
|
60 |
|
return $this->parameters; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Checks the existence of a common parameter by ID. |
160
|
|
|
* |
161
|
|
|
* @param string $id The unique identifier of the parameter. |
162
|
|
|
* |
163
|
|
|
* @return bool Whether a custom parameter that is common for all view templates exists. |
164
|
|
|
*/ |
165
|
5 |
|
public function hasParameter(string $id): bool |
166
|
|
|
{ |
167
|
5 |
|
return isset($this->parameters[$id]); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Sets a content block. |
172
|
|
|
* |
173
|
|
|
* @param string $id The unique identifier of the block. |
174
|
|
|
* @param string $content The content of the block. |
175
|
|
|
* |
176
|
|
|
* @return static |
177
|
|
|
*/ |
178
|
7 |
|
public function setBlock(string $id, string $content): self |
179
|
|
|
{ |
180
|
7 |
|
$this->blocks[$id] = $content; |
181
|
7 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Removes a content block. |
186
|
|
|
* |
187
|
|
|
* @param string $id The unique identifier of the block. |
188
|
|
|
* |
189
|
|
|
* @return static |
190
|
|
|
*/ |
191
|
3 |
|
public function removeBlock(string $id): self |
192
|
|
|
{ |
193
|
3 |
|
unset($this->blocks[$id]); |
194
|
3 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Gets content of the block by ID. |
199
|
|
|
* |
200
|
|
|
* @param string $id The unique identifier of the block. |
201
|
|
|
* |
202
|
|
|
* @return string The content of the block. |
203
|
|
|
*/ |
204
|
2 |
|
public function getBlock(string $id): string |
205
|
|
|
{ |
206
|
2 |
|
if (isset($this->blocks[$id])) { |
207
|
1 |
|
return $this->blocks[$id]; |
208
|
|
|
} |
209
|
|
|
|
210
|
2 |
|
throw new InvalidArgumentException('Block "' . $id . '" not found.'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Checks the existence of a content block by ID. |
215
|
|
|
* |
216
|
|
|
* @param string $id The unique identifier of the block. |
217
|
|
|
* |
218
|
|
|
* @return bool Whether a content block exists. |
219
|
|
|
*/ |
220
|
5 |
|
public function hasBlock(string $id): bool |
221
|
|
|
{ |
222
|
5 |
|
return isset($this->blocks[$id]); |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|