Completed
Push — master ( d45ca1...554243 )
by Adrian
01:42
created

ErrorMessage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 0
dl 0
loc 52
ccs 19
cts 23
cp 0.8261
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setTemplate() 0 9 2
A setVariables() 0 8 2
A __construct() 0 5 1
A getTemplate() 0 4 1
A getVariables() 0 4 1
A __toString() 0 11 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Validation;
5
6
class ErrorMessage
7
{
8
    protected $template = 'Invalid';
9
    protected $variables = [];
10
11 36
    public function __construct($template = '', $variables = [])
12
    {
13 36
        $this->setTemplate($template)
14 36
             ->setVariables($variables);
15 36
    }
16
17 36
    public function setTemplate($template)
18
    {
19 36
        $template = trim((string) $template);
20 36
        if ($template) {
21 29
            $this->template = (string) $template;
22
        }
23
24 36
        return $this;
25
    }
26
27
    public function getTemplate()
28
    {
29
        return $this->template;
30
    }
31
32 36
    public function setVariables($variables = [])
33
    {
34 36
        foreach ($variables as $k => $v) {
35 27
            $this->variables[$k] = $v;
36
        }
37
38 36
        return $this;
39
    }
40
41
    public function getVariables()
42
    {
43
        return $this->variables;
44
    }
45
46 20
    public function __toString()
47
    {
48 20
        $result = $this->template;
49 20
        foreach ($this->variables as $k => $v) {
50 18
            if (strpos($result, "{{$k}}") !== false) {
51 14
                $result = str_replace("{{$k}}", $v, $result);
52
            }
53
        }
54
55 20
        return $result;
56
    }
57
}
58