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