Completed
Push — 2.x ( ac33b7...f97207 )
by Paul
9s
created

ValidateSpec::__invoke()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
crap 5
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Filter\Spec;
10
11
/**
12
 *
13
 * A "validate" rule specification.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
class ValidateSpec extends Spec
19
{
20
    /**
21
     *
22
     * Reverse the rule, so that a "pass" is treated as a "fail".
23
     *
24
     * @var bool
25
     *
26
     */
27
    protected $reverse = false;
28
29
    /**
30
     * Skip checking whether field value is blank
31
     *
32
     * @var boolean
33
     */
34
    protected $skip_blank = false;
35
36
    /**
37
     *
38
     * Applies the rule specification to a subject.
39
     *
40
     * @param mixed $subject The filter subject.
41
     *
42
     * @return bool True on success, false on failure.
43
     *
44
     */
45 14
    public function __invoke($subject)
46
    {
47 14
        if (! $this->skip_blank && $this->subjectFieldIsBlank($subject)) {
48 8
            return $this->allow_blank;
49
        }
50
51 12
        if (! $this->rule) {
52 2
            return $this->reverse;
53
        }
54
55 10
        if ($this->reverse) {
56 2
            return ! parent::__invoke($subject);
57
        }
58
59 8
        return parent::__invoke($subject);
60
    }
61
62
    /**
63
     *
64
     * Validate the field matches this rule (blank not allowed).
65
     *
66
     * @param string $rule The rule name.
67
     *
68
     * @param ...$args Arguments for the rule.
69
     *
70
     * @return self
71
     *
72
     */
73 10
    public function is($rule)
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75 10
        $this->allow_blank = false;
76 10
        $this->reverse = false;
77 10
        return $this->init(func_get_args());
78
    }
79
80
    /**
81
     *
82
     * Validate the field is blank.
83
     *
84
     * @return self
85
     *
86
     */
87 1
    public function isBlank()
88
    {
89 1
        $this->allow_blank = true;
90 1
        $this->reverse = false;
91 1
        return $this->init(array());
92
    }
93
94
    /**
95
     *
96
     * Validate the field matches this rule (blank allowed).
97
     *
98
     * @param string $rule The rule name.
99
     *
100
     * @param ...$args Arguments for the rule.
101
     *
102
     * @return self
103
     *
104
     */
105 1
    public function isBlankOr($rule)
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107 1
        $this->allow_blank = true;
108 1
        $this->reverse = false;
109 1
        return $this->init(func_get_args());
110
    }
111
112
    /**
113
     *
114
     * Validate the field does not match this rule (blank not allowed).
115
     *
116
     * @param string $rule The rule name.
117
     *
118
     * @param ...$args Arguments for the rule.
119
     *
120
     * @return self
121
     *
122
     */
123 2
    public function isNot($rule)
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
    {
125 2
        $this->allow_blank = false;
126 2
        $this->reverse = true;
127 2
        return $this->init(func_get_args());
128
    }
129
130
    /**
131
     *
132
     * Validate the field is not blank.
133
     *
134
     * @return self
135
     *
136
     */
137 2
    public function isNotBlank()
138
    {
139 2
        $this->allow_blank = false;
140 2
        $this->reverse = true;
141 2
        return $this->init(array());
142
    }
143
144
    /**
145
     *
146
     * Validate the field does not match this rule (blank allowed).
147
     *
148
     * @param string $rule The rule name.
149
     *
150
     * @param ...$args Arguments for the rule.
151
     *
152
     * @return self
153
     *
154
     */
155 1
    public function isBlankOrNot($rule)
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
156
    {
157 1
        $this->allow_blank = true;
158 1
        $this->reverse = true;
159 1
        return $this->init(func_get_args());
160
    }
161
162
    /**
163
     *
164
     * Returns the default failure message for this rule specification.
165
     *
166
     * @return string
167
     *
168
     */
169 13
    protected function getDefaultMessage()
170
    {
171 13
        $message = $this->field . ' should';
172
173 13
        if (! $this->rule) {
174
            return $message
175 3
                . (($this->reverse) ? ' not' : '')
176 3
                . ' have been blank';
177
        }
178
179 11
        if ($this->allow_blank) {
180 2
            $message .= ' have been blank or';
181
        }
182
183 11
        if ($this->reverse) {
184 2
            $message .= ' not';
185
        }
186
187 11
        return "{$message} have validated as " . parent::getDefaultMessage();
188
    }
189
190
    /**
191
     * Skip checking whether field is blank
192
     *
193
     * @return self
194
     */
195 1
    public function skipBlankRule()
196
    {
197 1
        $this->skip_blank = true;
198 1
        return $this;
199
    }
200
}
201