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

ErrorMessage::getVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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