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

TemplateFormatter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1

4 Methods

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