@@ -68,14 +68,12 @@ abstract class BaseView implements DynamicContentAwareInterface
6868 private string $ defaultExtension = 'php ' ;
6969
7070 /**
71- * @var array Custom parameters that are shared among view templates.
71+ * @var array<string, mixed> Parameters that are common for all view templates.
7272 */
73- private array $ defaultParameters = [];
73+ private array $ commonParameters = [];
7474
7575 /**
76- * @var array A list of named output blocks. The keys are the block names and the values are the corresponding block
77- * content. You can call {@see beginBlock()} and {@see endBlock()} to capture small fragments of a view.
78- * They can be later accessed somewhere else through this property.
76+ * @var array<string, string> Named content blocks that are common for all view templates.
7977 */
8078 private array $ blocks = [];
8179
@@ -168,36 +166,82 @@ public function withDefaultExtension(string $defaultExtension): self
168166 return $ new ;
169167 }
170168
171- public function getDefaultParameters (): array
169+ /**
170+ * Sets a common parameter that is accessible in all view templates.
171+ *
172+ * @param string $id The unique identifier of the parameter.
173+ * @param mixed $value The value of the parameter.
174+ */
175+ public function setCommonParameter (string $ id , $ value ): void
172176 {
173- return $ this ->defaultParameters ;
177+ $ this ->commonParameters [ $ id ] = $ value ;
174178 }
175179
176- public function withDefaultParameters (array $ defaultParameters ): self
180+ /**
181+ * Removes a common parameter.
182+ *
183+ * @param string $id The unique identifier of the parameter.
184+ */
185+ public function removeCommonParameter (string $ id ): void
177186 {
178- $ new = clone $ this ;
179- $ new ->defaultParameters = $ defaultParameters ;
180- return $ new ;
187+ unset($ this ->commonParameters [$ id ]);
188+ }
189+
190+ /**
191+ * Gets a common parameter value by ID.
192+ *
193+ * @param string $id The unique identifier of the parameter.
194+ *
195+ * @return mixed The value of the parameter.
196+ */
197+ public function getCommonParameter (string $ id )
198+ {
199+ if (isset ($ this ->commonParameters [$ id ])) {
200+ return $ this ->commonParameters [$ id ];
201+ }
202+
203+ throw new InvalidArgumentException ('Common parameter: " ' . $ id . '" not found. ' );
181204 }
182205
183206 /**
184- * {@see blocks}
207+ * Checks the existence of a common parameter by ID.
208+ *
209+ * @param string $id The unique identifier of the parameter.
210+ *
211+ * @return bool Whether a custom parameter that is common for all view templates exists.
185212 */
186- public function setBlock (string $ id, string $ value ): void
213+ public function hasCommonParameter (string $ id ): bool
187214 {
188- $ this ->blocks [$ id ] = $ value ;
215+ return isset ( $ this ->commonParameters [$ id ]) ;
189216 }
190217
191218 /**
192- * {@see blocks}
219+ * Sets a content block.
220+ *
221+ * @param string $id The unique identifier of the block.
222+ * @param mixed $content The content of the block.
223+ */
224+ public function setBlock (string $ id , string $ content ): void
225+ {
226+ $ this ->blocks [$ id ] = $ content ;
227+ }
228+
229+ /**
230+ * Removes a content block.
231+ *
232+ * @param string $id The unique identifier of the block.
193233 */
194234 public function removeBlock (string $ id ): void
195235 {
196236 unset($ this ->blocks [$ id ]);
197237 }
198238
199239 /**
200- * {@see blocks}
240+ * Gets content of the block by ID.
241+ *
242+ * @param string $id The unique identifier of the block.
243+ *
244+ * @return string The content of the block.
201245 */
202246 public function getBlock (string $ id ): string
203247 {
@@ -209,7 +253,11 @@ public function getBlock(string $id): string
209253 }
210254
211255 /**
212- * {@see blocks}
256+ * Checks the existence of a content block by ID.
257+ *
258+ * @param string $id The unique identifier of the block.
259+ *
260+ * @return bool Whether a content block exists.
213261 */
214262 public function hasBlock (string $ id ): bool
215263 {
@@ -280,7 +328,7 @@ public function render(string $view, array $parameters = []): string
280328 */
281329 public function renderFile (string $ viewFile , array $ parameters = []): string
282330 {
283- $ parameters = array_merge ($ this ->defaultParameters , $ parameters );
331+ $ parameters = array_merge ($ this ->commonParameters , $ parameters );
284332
285333 // TODO: these two match now
286334 $ requestedFile = $ viewFile ;
0 commit comments