-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticTemplateResolver.php
More file actions
42 lines (38 loc) · 1.04 KB
/
Copy pathStaticTemplateResolver.php
File metadata and controls
42 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php declare(strict_types=1);
namespace Computator\FrameworkUtils\PHPTemplate;
use function array_key_exists;
use function is_array;
/**
* Maps a static array of template names to template content.
*
* # Example
*
* ```php
* <?php
* new StaticTemplateResolver(
* [
* 'tpl_one' => "Template one contents",
* 'tpl_two' => <<<'EOT'
* Template <?= number_format(1 + 1) ?> contents
* EOT,
* ],
* Templates\PHPString::class,
* );
* ```
*/
class StaticTemplateResolver extends TemplateResolver {
public function __construct(
protected array $templates,
string $new_template_class = Templates\PHPString::class,
) {
parent::__construct($new_template_class);
}
protected function map(string $template): Templates\Base {
if (!array_key_exists($template, $this->templates))
throw new Exceptions\TemplateNotFoundException("template '{$template}' not found");
$args = is_array($this->templates[$template])
? $this->templates[$template]
: [$this->templates[$template]];
return new $this->new_template_class(...$args);
}
}