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