Rule::shouldBreakChain()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
use Particle\Validator\Output\Subject;
12
use Particle\Validator\Value\Container;
13
14
/**
15
 * The Rule class is the abstract parent of all rules of Particle and defines their common behaviour.
16
 *
17
 * @package Particle\Validator
18
 */
19
abstract class Rule
20
{
21
    /**
22
     * Contains an array of all values to be validated.
23
     *
24
     * @var array
25
     */
26
    protected $values;
27
28
    /**
29
     * Contains an array of messages to be returned on validation errors.
30
     *
31
     * @var array
32
     */
33
    protected $messageTemplates = [];
34
35
    /**
36
     * Contains a reference to the MessageStack to append errors to.
37
     *
38
     * @var MessageStack
39
     */
40
    protected $messageStack;
41
42
    /**
43
     * The key we have to validate the value of.
44
     *
45
     * @var string
46
     */
47
    protected $key;
48
49
    /**
50
     * The name may be used in validation error messages.
51
     *
52
     * @var string
53
     */
54
    protected $name;
55
56
    /**
57
     * This method should validate, possibly log errors, and return the result as a boolean.
58
     *
59
     * @param mixed $value
60
     * @return bool
61
     */
62
    abstract public function validate($value);
63
64
    /**
65
     * This indicates whether or not the rule can and should break the chain it's in.
66
     *
67
     * @return bool
68
     */
69 205
    public function shouldBreakChain()
70
    {
71 205
        return false;
72
    }
73
74
    /**
75
     * This indicates whether or not the rule should break the chain it's in on validation failure.
76
     *
77
     * @return bool
78
     */
79 95
    public function shouldBreakChainOnError()
80
    {
81 95
        return false;
82
    }
83
84
    /**
85
     * Registers the message stack to append errors to.
86
     *
87
     * @param MessageStack $messageStack
88
     * @return $this
89
     */
90 259
    public function setMessageStack(MessageStack $messageStack)
91
    {
92 259
        $this->messageStack = $messageStack;
93 259
        return $this;
94
    }
95
96
    /**
97
     * Sets the default parameters for each validation rule (key and name).
98
     *
99
     * @param string $key
100
     * @param string $name
101
     * @return $this
102
     */
103 261
    public function setParameters($key, $name)
104
    {
105 261
        $this->key = $key;
106 261
        $this->name = $name;
107 261
        return $this;
108
    }
109
110
    /**
111
     * Determines whether or not the value of $key is valid in the array $values and returns the result as a bool.
112
     *
113
     * @param string $key
114
     * @param Container $input
115
     * @return bool
116
     */
117 234
    public function isValid($key, Container $input)
118
    {
119 234
        return $this->validate($input->get($key));
120
    }
121
122
    /**
123
     * Attach a representation of this rule to the Output\Subject $subject.
124
     *
125
     * @internal
126
     * @param Subject $subject
127
     * @param MessageStack $messageStack
128
     */
129 2
    public function output(Subject $subject, MessageStack $messageStack)
130
    {
131 2
        $this->setParameters($subject->getKey(), $subject->getName());
132
133 2
        $outputRule = new Output\Rule(
134 2
            $this->getShortName(),
135 2
            $this->getMessageTemplates($messageStack),
136 2
            $this->getMessageParameters()
137 2
        );
138
139 2
        $subject->addRule($outputRule);
140 2
    }
141
142
    /**
143
     * Appends the error for reason $reason to the MessageStack.
144
     *
145
     * @param string $reason
146
     * @return bool
147
     */
148 124
    protected function error($reason)
149
    {
150 124
        $this->messageStack->append(
151 124
            new Failure(
152 124
                $this->key,
153 124
                $reason,
154 124
                $this->getMessage($reason),
155 124
                $this->getMessageParameters()
156 124
            )
157 124
        );
158
159 124
        return false;
160
    }
161
162
    /**
163
     * Return an array of all parameters that might be replaced in the validation error messages.
164
     *
165
     * @return array
166
     */
167 126
    protected function getMessageParameters()
168
    {
169 126
        $name = isset($this->name) ? $this->name : str_replace('_', ' ', $this->key);
170
171
        return [
172 126
            'key' => $this->key,
173 126
            'name' => $name,
174 126
        ];
175
    }
176
177
    /**
178
     * Returns an error message for the reason $reason, or an empty string if it doesn't exist.
179
     *
180
     * @param mixed $reason
181
     * @return string
182
     */
183 124
    protected function getMessage($reason)
184
    {
185 124
        $messageTemplate = '';
186 124
        if (array_key_exists($reason, $this->messageTemplates)) {
187 124
            $messageTemplate = $this->messageTemplates[$reason];
188 124
        }
189
190 124
        return $messageTemplate;
191
    }
192
193
    /**
194
     * Returns the name of this class, without the namespace.
195
     *
196
     * @return string
197
     */
198 2
    protected function getShortName()
199
    {
200 2
        return substr(get_class($this), strrpos(get_class($this), '\\') + 1);
201
    }
202
203
    /**
204
     * Get an array of Message Templates to be returned in output.
205
     *
206
     * @param MessageStack $messageStack
207
     * @return array
208
     */
209 2
    protected function getMessageTemplates(MessageStack $messageStack)
210
    {
211 2
        $messages = $this->messageTemplates;
212 2
        foreach ($messages as $reason => $message) {
213 2
            $overwrite = $messageStack->getOverwrite($reason, $this->key);
214
215 2
            if (is_string($overwrite)) {
216 1
                $messages[$reason] = $overwrite;
217 1
            }
218 2
        }
219
220 2
        return $messages;
221
    }
222
}
223