Failure   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 0
dl 0
loc 91
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A format() 0 11 2
A getKey() 0 4 1
A getReason() 0 4 1
A overwriteMessageTemplate() 0 4 1
A overwriteKey() 0 4 1
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator;
10
11
class Failure
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $key;
17
18
    /**
19
     * @var string
20
     */
21
    protected $reason;
22
23
    /**
24
     * @var string
25
     */
26
    protected $template;
27
28
    /**
29
     * @var array
30
     */
31
    protected $parameters = [];
32
33
    /**
34
     * @param string $key
35
     * @param string $reason
36
     * @param string $template
37
     * @param array $parameters
38
     */
39 130
    public function __construct($key, $reason, $template, $parameters)
40
    {
41 130
        $this->key = $key;
42 130
        $this->reason = $reason;
43 130
        $this->template = $template;
44 130
        $this->parameters = $parameters;
45 130
    }
46
47
    /**
48
     * Formats and returns the error message for this failure.
49
     *
50
     * @return string
51
     */
52
    public function format()
53
    {
54 125
        $replace = function ($matches) {
55 119
            if (array_key_exists($matches[1], $this->parameters)) {
56 118
                return $this->parameters[$matches[1]];
57
            }
58 1
            return $matches[0];
59 125
        };
60
61 125
        return preg_replace_callback('~{{\s*([^}\s]+)\s*}}~', $replace, $this->template);
62
    }
63
64
    /**
65
     * Returns the key for this failure.
66
     *
67
     * @return string
68
     */
69 127
    public function getKey()
70
    {
71 127
        return $this->key;
72
    }
73
74
    /**
75
     * Returns the reason for failure.
76
     *
77
     * @return string
78
     */
79 127
    public function getReason()
80
    {
81 127
        return $this->reason;
82
    }
83
84
    /**
85
     * Overwrites the message template.
86
     *
87
     * @param string $template
88
     */
89 10
    public function overwriteMessageTemplate($template)
90
    {
91 10
        $this->template = $template;
92 10
    }
93
94
    /**
95
     * @param string $key
96
     */
97 3
    public function overwriteKey($key)
98
    {
99 3
        $this->key = $key;
100 3
    }
101
}
102