|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author stev leibelt <[email protected]> |
|
4
|
|
|
* @since 2015-10-01 |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Net\Bazzline\Component\Template; |
|
7
|
|
|
|
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
abstract class AbstractTemplate implements TemplateInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array */ |
|
14
|
|
|
private $variables; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param array $variables |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct($variables = array()) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->assignMany($variables, false); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $variables |
|
26
|
|
|
* @return string |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __invoke($variables = array()) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->assignMany($variables); |
|
31
|
|
|
|
|
32
|
|
|
return $this->render(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param array $variables |
|
37
|
|
|
* @param bool $mergeWithExisting |
|
38
|
|
|
*/ |
|
39
|
|
|
public function assignMany(array $variables, $mergeWithExisting = true) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($mergeWithExisting) { |
|
42
|
|
|
$this->variables = array_merge($this->variables, $variables); |
|
43
|
|
|
} else { |
|
44
|
|
|
$this->variables = $variables; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $key |
|
50
|
|
|
* @param mixed $value |
|
51
|
|
|
*/ |
|
52
|
|
|
public function assignOne($key, $value) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->assignMany(array($key => $value)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $key |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
|
|
public function isAssigned($key) |
|
62
|
|
|
{ |
|
63
|
|
|
return (isset($this->variables[$key])); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
* @throws RuntimeException |
|
69
|
|
|
*/ |
|
70
|
|
|
public function render() |
|
71
|
|
|
{ |
|
72
|
|
|
$content = $this->getContent(); |
|
73
|
|
|
$variables = $this->getVariables(); |
|
74
|
|
|
$keys = array_keys($variables); |
|
75
|
|
|
|
|
76
|
|
|
return str_replace($keys, $variables, $content); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function reset() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->assignMany(array(), false); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string |
|
86
|
|
|
* @throws RuntimeException |
|
87
|
|
|
*/ |
|
88
|
|
|
public function __toString() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->render(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
* @throws RuntimeException |
|
96
|
|
|
*/ |
|
97
|
|
|
abstract protected function getContent(); |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $key |
|
101
|
|
|
* @return null|mixed |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getValueByKeyOrNull($key) |
|
104
|
|
|
{ |
|
105
|
|
|
return ($this->isAssigned($key)) |
|
106
|
|
|
? $this->variables[$key] |
|
107
|
|
|
: null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function getVariables() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->variables; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|