Completed
Push — master ( aeb11c...f98da5 )
by Adrian
02:32
created

ErrorMessage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Sirius\Validation;
4
5
class ErrorMessage
6
{
7
    protected $template = 'Invalid';
8
    protected $variables = array();
9
10 34
    public function __construct($template = '', $variables = array())
11
    {
12 34
        $this->setTemplate($template)
13 34
             ->setVariables($variables);
14 34
    }
15
16 34
    public function setTemplate($template)
17
    {
18 34
        $template = trim((string) $template);
19 34
        if ($template) {
20 28
            $this->template = (string) $template;
21 28
        }
22
23 34
        return $this;
24
    }
25
26
    public function getTemplate()
27
    {
28
        return $this->template;
29
    }
30
31 34
    public function setVariables($variables = array())
32
    {
33 34
        foreach ($variables as $k => $v) {
34 26
            $this->variables[$k] = $v;
35 34
        }
36
37 34
        return $this;
38
    }
39
40
    public function getVariables()
41
    {
42
        return $this->variables;
43
    }
44
45 20
    public function __toString()
46
    {
47 20
        $result = $this->template;
48 20
        foreach ($this->variables as $k => $v) {
49 18
            if (strpos($result, "{{$k}}") !== false) {
50 14
                $result = str_replace("{{$k}}", $v, $result);
51 14
            }
52 20
        }
53
54 20
        return $result;
55
    }
56
}
57