Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| StaticTemplateResolver | |
100.00% |
7 / 7 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| map | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Computator\FrameworkUtils\PHPTemplate; |
| 4 | |
| 5 | use function array_key_exists; |
| 6 | use function is_array; |
| 7 | |
| 8 | /** |
| 9 | * Maps a static array of template names to template content. |
| 10 | * |
| 11 | * # Example |
| 12 | * |
| 13 | * ```php |
| 14 | * <?php |
| 15 | * new StaticTemplateResolver( |
| 16 | * [ |
| 17 | * 'tpl_one' => "Template one contents", |
| 18 | * 'tpl_two' => <<<'EOT' |
| 19 | * Template <?= number_format(1 + 1) ?> contents |
| 20 | * EOT, |
| 21 | * ], |
| 22 | * Templates\PHPString::class, |
| 23 | * ); |
| 24 | * ``` |
| 25 | */ |
| 26 | class StaticTemplateResolver extends TemplateResolver { |
| 27 | public function __construct( |
| 28 | protected array $templates, |
| 29 | string $new_template_class = Templates\PHPString::class, |
| 30 | ) { |
| 31 | parent::__construct($new_template_class); |
| 32 | } |
| 33 | |
| 34 | protected function map(string $template): Templates\Base { |
| 35 | if (!array_key_exists($template, $this->templates)) |
| 36 | throw new Exceptions\TemplateNotFoundException("template '{$template}' not found"); |
| 37 | $args = is_array($this->templates[$template]) |
| 38 | ? $this->templates[$template] |
| 39 | : [$this->templates[$template]]; |
| 40 | return new $this->new_template_class(...$args); |
| 41 | } |
| 42 | } |