Completed
Push — 2.x ( ef919b...c1100b )
by Paul
02:31
created

ValidateSpec::getDefaultMessage()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 8.8571
cc 5
eloc 11
nc 6
nop 0
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
     *
31
     * Validate the field matches this rule (blank not allowed).
32
     *
33
     * @param string $rule The rule name.
34
     *
35
     * @param ...$args Arguments for the rule.
36
     *
37
     * @return self
38
     *
39
     */
40 9
    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...
41
    {
42 9
        $this->allow_blank = false;
43 9
        $this->reverse = false;
44 9
        return $this->init(func_get_args());
45
    }
46
47
    /**
48
     *
49
     * Validate the field is blank.
50
     *
51
     * @return self
52
     *
53
     */
54 1
    public function isBlank()
55
    {
56 1
        $this->allow_blank = true;
57 1
        $this->reverse = false;
58 1
        return $this->init(array());
59
    }
60
61
    /**
62
     *
63
     * Validate the field matches this rule (blank allowed).
64
     *
65
     * @param string $rule The rule name.
66
     *
67
     * @param ...$args Arguments for the rule.
68
     *
69
     * @return self
70
     *
71
     */
72 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...
73
    {
74 1
        $this->allow_blank = true;
75 1
        $this->reverse = false;
76 1
        return $this->init(func_get_args());
77
    }
78
79
    /**
80
     *
81
     * Validate the field does not match this rule (blank not allowed).
82
     *
83
     * @param string $rule The rule name.
84
     *
85
     * @param ...$args Arguments for the rule.
86
     *
87
     * @return self
88
     *
89
     */
90 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...
91
    {
92 2
        $this->allow_blank = false;
93 2
        $this->reverse = true;
94 2
        return $this->init(func_get_args());
95
    }
96
97
    /**
98
     *
99
     * Validate the field is not blank.
100
     *
101
     * @return self
102
     *
103
     */
104 1
    public function isNotBlank()
105
    {
106 1
        $this->allow_blank = false;
107 1
        $this->reverse = true;
108 1
        return $this->init(array());
109
    }
110
111
    /**
112
     *
113
     * Validate the field does not match this rule (blank allowed).
114
     *
115
     * @param string $rule The rule name.
116
     *
117
     * @param ...$args Arguments for the rule.
118
     *
119
     * @return self
120
     *
121
     */
122 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...
123
    {
124 1
        $this->allow_blank = true;
125 1
        $this->reverse = true;
126 1
        return $this->init(func_get_args());
127
    }
128
129
    /**
130
     *
131
     * Returns the default failure message for this rule specification.
132
     *
133
     * @return string
134
     *
135
     */
136 12
    protected function getDefaultMessage()
137
    {
138 12
        $message = $this->field . ' should';
139
140 12
        if (! $this->rule) {
141
            return $message
142 2
                . (($this->reverse) ? ' not' : '')
143 2
                . ' have been blank';
144
        }
145
146 11
        if ($this->allow_blank) {
147 2
            $message .= ' have been blank or';
148 2
        }
149
150 11
        if ($this->reverse) {
151 2
            $message .= ' not';
152 2
        }
153
154 11
        return "{$message} have validated as " . parent::getDefaultMessage();
155
    }
156
157
    /**
158
     *
159
     * Check if the subject field passes the rule specification.
160
     *
161
     * @param mixed $subject The filter subject.
162
     *
163
     * @return bool
164
     *
165
     */
166 12
    protected function applyRule($subject)
167
    {
168 12
        if (! $this->rule) {
169 2
            return false;
170
        }
171
172 11
        if (! isset($subject->{$this->field})) {
173 2
            return false;
174
        }
175
176 10
        if ($this->reverse) {
177 2
            return ! parent::applyRule($subject);
178
        }
179
180 8
        return parent::applyRule($subject);
181
    }
182
}
183