Skip to content

Commit 2d8d972

Browse files
authored
Fix #94: Refactor default parameters feature to BaseView (#155)
1 parent d2c063a commit 2d8d972

2 files changed

Lines changed: 107 additions & 27 deletions

File tree

src/BaseView.php

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

tests/ViewTest.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\View\Tests;
66

7+
use InvalidArgumentException;
78
use PHPUnit\Framework\TestCase;
89
use Psr\Log\NullLogger;
910
use RuntimeException;
@@ -173,26 +174,57 @@ private function createFileStructure(array $items, string $baseDirectory = null)
173174
}
174175
}
175176

176-
public function testDefaultParameterIsPassedToView(): void
177+
public function testCommonParameter(): void
177178
{
178-
$view = TestHelper::createView()
179-
->withDefaultParameters(['parameter' => 'default_parameter']);
179+
$view = TestHelper::createView();
180+
$this->assertFalse($view->hasCommonParameter('id'));
181+
182+
$view->setCommonParameter('id', 42);
183+
$this->assertTrue($view->hasCommonParameter('id'));
184+
$this->assertSame(42, $view->getCommonParameter('id'));
185+
186+
$view->removeCommonParameter('id');
187+
$this->assertFalse($view->hasCommonParameter('id'));
188+
189+
$this->expectException(InvalidArgumentException::class);
190+
$view->getCommonParameter('id');
191+
}
180192

193+
public function testCommonParameterIsPassedToView(): void
194+
{
195+
$view = TestHelper::createView();
196+
$view->setCommonParameter('parameter', 'global-parameter');
181197
$output = $view->render('//parameters');
182198

183-
$this->assertSame('default_parameter', $output);
199+
$this->assertSame('global-parameter', $output);
184200
}
185201

186-
public function testDefaultParameterIsOverwrittenByLocalParameter(): void
202+
public function testCommonParameterIsOverwrittenByLocalParameter(): void
187203
{
188-
$view = TestHelper::createView()
189-
->withDefaultParameters(['parameter' => 'default_parameter']);
204+
$view = TestHelper::createView();
205+
$view->setCommonParameter('parameter', 'global-parameter');
190206

191207
$output = $view->render('//parameters', [
192-
'parameter' => 'local_parameter',
208+
'parameter' => 'local-parameter',
193209
]);
194210

195-
$this->assertSame('local_parameter', $output);
211+
$this->assertSame('local-parameter', $output);
212+
}
213+
214+
public function testBlock(): void
215+
{
216+
$view = TestHelper::createView();
217+
$this->assertFalse($view->hasBlock('id'));
218+
219+
$view->setBlock('id', 'content');
220+
$this->assertTrue($view->hasBlock('id'));
221+
$this->assertSame('content', $view->getBlock('id'));
222+
223+
$view->removeBlock('id');
224+
$this->assertFalse($view->hasBlock('id'));
225+
226+
$this->expectException(InvalidArgumentException::class);
227+
$view->getBlock('id');
196228
}
197229

198230
public function testPlaceholderSalt(): void

0 commit comments

Comments
 (0)