MessageStack   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 1
dl 0
loc 167
ccs 50
cts 50
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A append() 0 15 3
A getOverwrite() 0 12 3
A overwriteMessages() 0 5 1
A overwriteDefaultMessages() 0 5 1
A merge() 0 5 1
A reset() 0 5 1
A mergeDefaultMessages() 0 8 3
A mergeOverwrites() 0 10 4
A hasOverwrite() 0 4 1
A getFailures() 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
/**
12
 * The MessageStack is responsible for keeping track of all validation messages, including their overwrites.
13
 *
14
 * @package Particle\Validator
15
 */
16
class MessageStack
17
{
18
    /**
19
     * Contains a list of all validation messages.
20
     *
21
     * @var array
22
     */
23
    protected $messages = [];
24
25
    /**
26
     * Contains an array of field and reason specific message overwrites.
27
     *
28
     * @var array
29
     */
30
    protected $overwrites = [];
31
32
    /**
33
     * Contains an array of global message overwrites.
34
     *
35
     * @var array
36
     */
37
    protected $defaultMessages = [];
38
39
    /**
40
     * @var Failure[]
41
     */
42
    protected $failures = [];
43
44
    /**
45
     * Will append an error message for the target $key with $reason to the stack.
46
     *
47
     * @param Failure $failure
48
     */
49 126
    public function append(Failure $failure)
50
    {
51 126
        $key = $failure->getKey();
52 126
        $reason = $failure->getReason();
53
54 126
        if (isset($this->defaultMessages[$reason])) {
55 4
            $failure->overwriteMessageTemplate($this->defaultMessages[$reason]);
56 4
        }
57
58 126
        if (isset($this->overwrites[$key][$reason])) {
59 7
            $failure->overwriteMessageTemplate($this->overwrites[$key][$reason]);
60 7
        }
61
62 126
        $this->failures[] = $failure;
63 126
    }
64
65
    /**
66
     * Returns an overwrite (either default or specific message) for the reason and key, or false.
67
     *
68
     * @param string $reason
69
     * @param string $key
70
     * @return string|bool
71
     */
72 3
    public function getOverwrite($reason, $key)
73
    {
74 3
        if ($this->hasOverwrite($key, $reason)) {
75 2
            return $this->overwrites[$key][$reason];
76
        }
77
78 3
        if (array_key_exists($reason, $this->defaultMessages)) {
79 2
            return $this->defaultMessages[$reason];
80
        }
81
82 2
        return false;
83
    }
84
85
    /**
86
     * Overwrite key-validator specific messages (so [first_name => [Length::TOO_SHORT => 'Message']]).
87
     *
88
     * @param array $messages
89
     * @return $this
90
     */
91 9
    public function overwriteMessages(array $messages)
92
    {
93 9
        $this->overwrites = $messages;
94 9
        return $this;
95
    }
96
97
    /**
98
     * Overwrite the default validator-specific messages (so [Length::TOO_SHORT => 'Generic message'].
99
     *
100
     * @param array $messages
101
     * @return $this
102
     */
103 6
    public function overwriteDefaultMessages(array $messages)
104
    {
105 6
        $this->defaultMessages = $messages;
106 6
        return $this;
107
    }
108
109
    /**
110
     * Merges an existing MessageStack into this one by taking over it's overwrites and defaults.
111
     *
112
     * @param MessageStack $messageStack
113
     */
114 7
    public function merge(MessageStack $messageStack)
115
    {
116 7
        $this->mergeDefaultMessages($messageStack);
117 7
        $this->mergeOverwrites($messageStack);
118 7
    }
119
120
    /**
121
     * Reset the messages to an empty array.
122
     *
123
     * @return $this
124
     */
125 260
    public function reset()
126
    {
127 260
        $this->failures = [];
128 260
        return $this;
129
    }
130
131
    /**
132
     * Merges the default messages from $messageStack to this MessageStack.
133
     *
134
     * @param MessageStack $messageStack
135
     */
136 7
    protected function mergeDefaultMessages(MessageStack $messageStack)
137
    {
138 7
        foreach ($messageStack->defaultMessages as $key => $message) {
139 1
            if (!array_key_exists($key, $this->defaultMessages)) {
140 1
                $this->defaultMessages[$key] = $message;
141 1
            }
142 7
        }
143 7
    }
144
145
    /**
146
     * Merges the message overwrites from $messageStack to this MessageStack.
147
     *
148
     * @param MessageStack $messageStack
149
     */
150 7
    protected function mergeOverwrites(MessageStack $messageStack)
151
    {
152 7
        foreach ($messageStack->overwrites as $key => $reasons) {
153 4
            foreach ($reasons as $reason => $message) {
154 4
                if (!$this->hasOverwrite($key, $reason)) {
155 3
                    $this->overwrites[$key][$reason] = $message;
156 3
                }
157 4
            }
158 7
        }
159 7
    }
160
161
    /**
162
     * Returns whether an overwrite exists for the key $key with reason $reason.
163
     *
164
     * @param string $key
165
     * @param string $reason
166
     * @return bool
167
     */
168 6
    protected function hasOverwrite($key, $reason)
169
    {
170 6
        return isset($this->overwrites[$key][$reason]);
171
    }
172
173
    /**
174
     * Returns an array of all failures of the last validation run.
175
     *
176
     * @return Failure[]
177
     */
178 262
    public function getFailures()
179
    {
180 262
        return $this->failures;
181
    }
182
}
183