Skip to content

Commit 0b24d5a

Browse files
authored
add class DefaultDataTemplate (#2)
1 parent 481f20f commit 0b24d5a

7 files changed

Lines changed: 121 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
.idea
33
.php_cs.cache
44
composer.lock
5+
.phpunit.result.cache

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
# php-template
55
Template rendering abstraction library. Abstract your libraries from specific templating engines.
66

7-
This library provides the [```TemplateInterface```](https://github.com/slepic/php-template/blob/master/src/TemplateInterface.php), which is the abstraction of any data rendering template.
8-
And it also provides one simple implementation called [```OutputBufferTemplate```](https://github.com/slepic/php-template/blob/master/src/OutputBufferTemplate.php), which renders the data using another PHP script and PHP ob_* functions.
9-
107
## Requirements
118

129
PHP 7.4 or 8
@@ -17,6 +14,19 @@ Install with composer
1714

1815
```composer require slepic/php-template```
1916

17+
## Interfaces
18+
19+
### [```TemplateInterface```](https://github.com/slepic/php-template/blob/master/src/TemplateInterface.php)
20+
This is the abstraction of any data rendering template.
21+
22+
## Classes
23+
24+
### [```OutputBufferTemplate```](https://github.com/slepic/php-template/blob/master/src/OutputBufferTemplate.php)
25+
A simple template implementation, which renders the data using another PHP script (given its filename) and PHP ob_* functions.
26+
27+
### [```DefaultDataTemplate```](https://github.com/slepic/php-template/blob/master/src/DefaultDataTemplate.php)
28+
Template decorator which allows to feed your templates with default data hidden from the template consumer.
29+
2030
## Contribution
2131

2232
If you create a library that depends on this one and you use composer, please consider the following:
@@ -25,6 +35,11 @@ If you create a library that depends on this one and you use composer, please co
2535

2636
## Changelog
2737

38+
### 1.1.0
39+
* added new class `DefaultDataTemplate`
40+
* `OutputBufferTemplate` now ends the output buffer if the included template throws an exception.
41+
* `OutputBufferTemplate` now uses `include` instead of `require` to execute the template script .
42+
2843
### 1.0.0
2944

3045
* bump PHP to ^7.4 || ^8.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Simple PHP abstraction of template rendering.",
44
"type": "library",
55
"require": {
6-
"php": "^7.4 || ^8.0"
6+
"php": ">=7.4"
77
},
88
"autoload": {
99
"psr-4": {

src/DefaultDataTemplate.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Slepic\Templating\Template;
6+
7+
/**
8+
* An implementation of TemplateInterface which wraps another implementation and keeps some default data
9+
* which it then supplies to the inner template upon rendering.
10+
*
11+
* Overriding default values is allowed.
12+
* A simple array_merge is used and so nested arrays are not merged recursively.
13+
* However, this may change in future.
14+
*/
15+
class DefaultDataTemplate implements TemplateInterface
16+
{
17+
private TemplateInterface $template;
18+
private array $data;
19+
20+
/**
21+
* @param TemplateInterface $template
22+
* @param array<string,mixed> $data
23+
*/
24+
public function __construct(TemplateInterface $template, array $data)
25+
{
26+
$this->template = $template;
27+
$this->data = $data;
28+
}
29+
30+
public function render(array $data): string
31+
{
32+
$allData = \array_merge($this->data, $data);
33+
return $this->template->render($allData);
34+
}
35+
}

src/OutputBufferTemplate.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ public function render(array $data): string
2323
'Expected associative array where keys are valid variable names.'
2424
);
2525
}
26+
2627
\ob_start();
27-
require($this->templateFile);
28+
try {
29+
include($this->templateFile);
30+
} catch (\Throwable $e) {
31+
\ob_end_clean();
32+
throw $e;
33+
}
34+
2835
return \ob_get_clean();
2936
}
3037
}

src/TemplateInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface TemplateInterface
1212
/**
1313
* Renders the template with given data.
1414
*
15-
* @param array $data
15+
* @param array<string,mixed> $data
1616
* @return string
1717
*/
1818
public function render(array $data): string;

tests/DefaultDataTemplateTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Slepic\Tests\Templating\Template;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Slepic\Templating\Template\DefaultDataTemplate;
9+
use Slepic\Templating\Template\TemplateInterface;
10+
11+
final class DefaultDataTemplateTest extends TestCase
12+
{
13+
public function testImplements(): void
14+
{
15+
$innerTemplate = self::createMock(TemplateInterface::class);
16+
$template = new DefaultDataTemplate($innerTemplate, []);
17+
$this->assertInstanceOf(TemplateInterface::class, $template);
18+
}
19+
20+
/**
21+
* @param array $defaultData
22+
* @param array $data
23+
* @param array $allData
24+
*
25+
* @dataProvider provideRenderData
26+
*/
27+
public function testRender(array $defaultData, array $data, array $allData): void
28+
{
29+
$hash = \md5((string) \time());
30+
$innerTemplate = self::createMock(TemplateInterface::class);
31+
$innerTemplate->expects(self::once())
32+
->method('render')
33+
->with($allData)
34+
->willReturn($hash);
35+
$template = new DefaultDataTemplate($innerTemplate, $defaultData);
36+
$output = $template->render($data);
37+
self::assertSame($hash, $output);
38+
}
39+
40+
public function provideRenderData(): array
41+
{
42+
$hash = \md5((string) \time());
43+
return [
44+
[[], [], []],
45+
[
46+
['default' => 'value'],
47+
['test' => $hash],
48+
['test' => $hash, 'default' => 'value']
49+
],
50+
[
51+
['override' => 'default'],
52+
['keep' => $hash, 'override' => 'overridden'],
53+
['keep' => $hash, 'override' => 'overridden']
54+
],
55+
];
56+
}
57+
}

0 commit comments

Comments
 (0)