Passed
Push — master ( ba186c...8baf10 )
by Lars
03:46
created

TemplateFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 53
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A set() 0 4 1
A format() 0 8 2
A append() 0 4 1
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
}