1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_templates; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class ATemplate |
8
|
|
|
* @package kalanis\kw_templates |
9
|
|
|
* Main work with templates - process them as either string in object or blob with points of interests |
10
|
|
|
*/ |
11
|
|
|
abstract class ATemplate |
12
|
|
|
{ |
13
|
|
|
protected string $template = ''; |
14
|
|
|
protected string $defaultTemplate = ''; |
15
|
|
|
protected static ?Template\Item $item = null; |
16
|
|
|
/** @var Template\Item[] */ |
17
|
|
|
protected array $items = []; |
18
|
|
|
|
19
|
8 |
|
public function __construct() |
20
|
|
|
{ |
21
|
8 |
|
$this->setTemplate($this->loadTemplate()); |
22
|
7 |
|
$this->fillInputs(); |
23
|
7 |
|
} |
24
|
|
|
|
25
|
2 |
|
protected function getTemplateItem(): Template\Item |
26
|
|
|
{ |
27
|
2 |
|
if (empty(static::$item)) { |
28
|
1 |
|
static::$item = new Template\Item(); |
29
|
|
|
} |
30
|
2 |
|
return static::$item; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
7 |
|
protected function setTemplate(string $content): self |
34
|
|
|
{ |
35
|
7 |
|
$this->defaultTemplate = $content; |
36
|
7 |
|
$this->reset(); |
37
|
7 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Here directly set or load template from external source |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
abstract protected function loadTemplate(): string; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Fill inputs when need - usually at the start |
48
|
|
|
*/ |
49
|
|
|
abstract protected function fillInputs(): void; |
50
|
|
|
|
51
|
2 |
|
protected function addInput(string $key, string $default = '', ?string $value = null): self |
52
|
|
|
{ |
53
|
2 |
|
$copy = clone $this->getTemplateItem(); |
54
|
2 |
|
$this->addItem($copy->setData($key, $default)->setValue($value)); |
55
|
2 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
protected function getItem(string $key): ?Template\Item |
59
|
|
|
{ |
60
|
1 |
|
return isset($this->items[$key]) ? $this->items[$key] : null ; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
protected function updateItem(string $key, ?string $value): self |
64
|
|
|
{ |
65
|
1 |
|
$this->items[$key]->setValue($value); |
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
protected function addItem(Template\Item $item): self |
70
|
|
|
{ |
71
|
2 |
|
$this->items[$item->getKey()] = $item; |
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Render template with inserted inputs |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
5 |
|
public function render(): string |
80
|
|
|
{ |
81
|
5 |
|
$this->processItems(); |
82
|
5 |
|
return $this->get(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Process items inputs in template |
87
|
|
|
*/ |
88
|
5 |
|
protected function processItems(): void |
89
|
|
|
{ |
90
|
5 |
|
$map = []; |
91
|
5 |
|
foreach ($this->items as $item) { |
92
|
2 |
|
$map[(string) $item->getKey()] = (string) $item->getValue(); |
93
|
|
|
} |
94
|
5 |
|
$this->template = strtr($this->template, $map); |
95
|
5 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* replace part in template with another one |
99
|
|
|
* |
100
|
|
|
* @param string $which |
101
|
|
|
* @param string $to |
102
|
|
|
*/ |
103
|
1 |
|
public function change(string $which, string $to): void |
104
|
|
|
{ |
105
|
1 |
|
$this->template = str_replace($which, $to, $this->template); |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* get part of template |
110
|
|
|
* |
111
|
|
|
* @param int $begin where may begin |
112
|
|
|
* @param int|null $length how long it is |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
1 |
|
public function getSubstring(int $begin, ?int $length = null): string |
116
|
|
|
{ |
117
|
1 |
|
return is_null($length) ? substr($this->template, $begin) : substr($this->template, $begin, $length); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* get position of sub-string |
122
|
|
|
* |
123
|
|
|
* @param string $what looking for |
124
|
|
|
* @param int $begin after... |
125
|
|
|
* @throws TemplateException |
126
|
|
|
* @return int |
127
|
|
|
*/ |
128
|
1 |
|
public function position(string $what, int $begin = 0): int |
129
|
|
|
{ |
130
|
1 |
|
$w = $this->template; |
131
|
1 |
|
$w = substr($w, $begin); |
132
|
1 |
|
$p = strpos($w, $what); |
133
|
1 |
|
if (false === $p) { |
134
|
1 |
|
throw new TemplateException('Not found'); |
135
|
|
|
} |
136
|
1 |
|
return $p; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* paste (include) content on position - can rewrite old one |
141
|
|
|
* |
142
|
|
|
* @param string $newString |
143
|
|
|
* @param int $fromBeing in original string |
144
|
|
|
* @param int $skip in original string |
145
|
|
|
*/ |
146
|
1 |
|
public function paste(string $newString, int $fromBeing, int $skip = 0): void |
147
|
|
|
{ |
148
|
|
|
# prepare |
149
|
1 |
|
$fromBeing = intval(abs($fromBeing)); |
150
|
1 |
|
$skip = intval(abs($skip)); |
151
|
|
|
# run |
152
|
1 |
|
$leftFromBegin = substr($this->template, 0, $fromBeing); |
153
|
1 |
|
$leftFromEnd = (0 == $skip) ? substr($this->template, $fromBeing) : substr($this->template, $fromBeing + $skip); |
154
|
1 |
|
$this->template = $leftFromBegin . $newString . $leftFromEnd; |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* return actual template |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
5 |
|
public function get(): string |
162
|
|
|
{ |
163
|
5 |
|
return $this->template; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* reload the template |
168
|
|
|
* @return $this |
169
|
|
|
*/ |
170
|
7 |
|
public function reset(): self |
171
|
|
|
{ |
172
|
7 |
|
$this->template = sprintf('%s', $this->defaultTemplate); // COPY!!! |
173
|
7 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|