|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* MIT License |
|
4
|
|
|
|
|
5
|
|
|
Copyright (c) 2018 Eridan Domoratskiy |
|
6
|
|
|
|
|
7
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
in the Software without restriction, including without limitation the rights |
|
10
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
furnished to do so, subject to the following conditions: |
|
13
|
|
|
|
|
14
|
|
|
The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
copies or substantial portions of the Software. |
|
16
|
|
|
|
|
17
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
SOFTWARE. */ |
|
24
|
|
|
|
|
25
|
|
|
namespace ProgMinerUtils\TemplateRenderer; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Template renderer for callable templates |
|
29
|
|
|
* |
|
30
|
|
|
* @author Eridan Domoratskiy |
|
31
|
|
|
*/ |
|
32
|
|
|
class CallableTemplateRenderer implements ITemplateRenderer { |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var callable[] Templates |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $templates = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param callable[] $templates Templates |
|
41
|
|
|
*/ |
|
42
|
16 |
|
public function __construct(array $templates) { |
|
43
|
16 |
|
foreach ($templates as $key => $value) { |
|
44
|
14 |
|
if (is_string($key) && is_callable($value)) { |
|
45
|
14 |
|
$this->templates[$key] = $value; |
|
46
|
|
|
} else { |
|
47
|
14 |
|
throw new \InvalidArgumentException('Templates must be callable[] with string keys'); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
16 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
14 |
|
public function render(string $template, array $variables = []): string { |
|
56
|
14 |
|
if (!isset($this->templates[$template])) { |
|
57
|
2 |
|
throw new \UnexpectedValueException("Template \"$template\" is not supported"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
14 |
|
ob_start(); |
|
61
|
|
|
|
|
62
|
14 |
|
call_user_func($this->templates[$template], $variables); |
|
63
|
|
|
|
|
64
|
14 |
|
$ret = ob_get_contents(); |
|
65
|
14 |
|
ob_end_clean(); |
|
66
|
|
|
|
|
67
|
14 |
|
return $ret; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
14 |
|
public function supports(string $template): bool { |
|
74
|
14 |
|
return isset($this->templates[$template]); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|