Completed
Push — master ( d3a134...42e39c )
by Lars
01:43
created

TemplateFormatter::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace voku\build\Template;
4
5
class TemplateFormatter {
6
    /**
7
     * @var array<string, string>
8
     */
9
    private $vars = [];
10
11
    /**
12
     * @var string
13
     */
14
    private $template;
15
16
    /**
17
     * @param string $template
18
     */
19
    public function __construct(string $template) {
20
        $this->template = $template;
21
    }
22
23
    /**
24
     * @param string $var
25
     * @param string $value
26
     *
27
     * @return $this
28
     */
29
    public function set(string $var, string $value): self {
30
        $this->vars[$var] = $value;
31
32
        return $this;
33
    }
34
35
    /**
36
     * @param string $var
37
     * @param string $value
38
     *
39
     * @return $this
40
     */
41
    public function append(string $var, string $value): self {
42
        $this->vars[$var] = ($this->vars[$var] ?? '') . $value;
43
44
        return $this;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function format(): string {
51
        $s = $this->template;
52
53
        foreach ($this->vars as $name => $value) {
54
            $s = \voku\helper\UTF8::replace($s, sprintf('%%%s%%', $name), $value);
55
        }
56
57
        return $s;
58
    }
59
}