|
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 different template renderers |
|
29
|
|
|
* |
|
30
|
|
|
* @author ProgMiner |
|
31
|
|
|
*/ |
|
32
|
|
|
class DelegatingTemplateRenderer implements ITemplateRenderer { |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ITemplateRenderer[] Template renderers |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $renderers = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param ITemplateRenderer[] $renderers Template renderers |
|
41
|
|
|
*/ |
|
42
|
10 |
|
public function __construct(array $renderers) { |
|
43
|
10 |
|
foreach ($renderers as $value) { |
|
44
|
8 |
|
if (is_a($value, ITemplateRenderer::class, false)) { |
|
45
|
8 |
|
$this->renderers[] = $value; |
|
46
|
|
|
} else { |
|
47
|
8 |
|
throw new \InvalidArgumentException('Template renderers must be ITemplateRenderer[]'); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
10 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
8 |
|
public function render(string $template, array $variables = []): string { |
|
56
|
8 |
|
foreach ($this->renderers as $renderer) { |
|
57
|
8 |
|
if ($renderer->supports($template)) { |
|
58
|
8 |
|
return $renderer->render($template, $variables); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
throw new \UnexpectedValueException("Template \"$template\" is not supported"); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
6 |
|
public function supports(string $template): bool { |
|
69
|
6 |
|
foreach ($this->renderers as $renderer) { |
|
70
|
6 |
|
if ($renderer->supports($template)) { |
|
71
|
6 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|