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

ErrorMessage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 85.19%

Importance

Changes 10
Bugs 4 Features 1
Metric Value
wmc 10
c 10
b 4
f 1
lcom 2
cbo 0
dl 0
loc 52
ccs 23
cts 27
cp 0.8519
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setTemplate() 0 9 2
A getTemplate() 0 4 1
A setVariables() 0 8 2
A getVariables() 0 4 1
A __toString() 0 11 3
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