Passed
Pull Request — 3.x (#145)
by Hari
09:43
created

SanitizeSpec::useBlankField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 "sanitize" rule specification.
14
 *
15
 * @package Aura.Filter
16
 *
17
 */
18
class SanitizeSpec extends Spec
19
{
20
    /**
21
     *
22
     * If the field is blank, use this as the replacement value.
23
     *
24
     * @param mixed
25
     *
26
     */
27
    protected $blank_value;
28
29
    /**
30
     *
31
     * If the field is blank, use value from this field as the replacement value.
32
     *
33
     * @param string
34
     *
35
     */
36
    protected $blank_field;
37
38 6
    /**
39
     *
40 6
     * Applies the rule specification to a subject.
41 5
     *
42
     * @param mixed $subject The filter subject.
43
     *
44 3
     * @return bool True on success, false on failure.
45 1
     *
46
     */
47
    public function __invoke($subject)
48 2
    {
49 2
50 2
        if ($this->subjectFieldIsBlank($subject) && $this->blank_field) {
51
            $field = $this->field;
52
            $replace_with = $this->blank_field;
53
            $subject->$field = $subject->$replace_with;
54
        }
55
56
        if (! $this->subjectFieldIsBlank($subject)) {
57
            return parent::__invoke($subject);
58
        }
59
60
        if (! $this->allow_blank) {
61
            return false;
62
        }
63
64 5
        $field = $this->field;
65
        $subject->$field = $this->blank_value;
66 5
        return true;
67 5
    }
68
69
    /**
70
     *
71
     * Sanitize the field using this rule (blank not allowed).
72
     *
73
     * @param string $rule The rule name.
74
     *
75
     * @param ...$args Arguments for the rule.
0 ignored issues
show
Bug introduced by
The type Aura\Filter\Spec\Arguments was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
76
     *
77
     * @return self
78
     *
79
     */
80
    public function to($rule)
81 2
    {
82
        $this->allow_blank = false;
83 2
        return $this->init(func_get_args());
84 2
    }
85
86
    /**
87
     *
88
     * Sanitize the using this rule (blank allowed).
89
     *
90
     * @param string $rule The rule name.
91
     *
92
     * @param ...$args Arguments for the rule.
93
     *
94
     * @return self
95
     *
96 1
     */
97
    public function toBlankOr($rule)
98 1
    {
99 1
        $this->allow_blank = true;
100 1
        return $this->init(func_get_args());
101
    }
102
103
    /**
104
     *
105
     * Use this value for blank fields.
106
     *
107
     * @param mixed $blank_value Replace the blank field with this value.
108
     *
109
     * @return self
110 1
     *
111
     */
112 1
    public function useBlankValue($blank_value)
113 1
    {
114
        $this->allow_blank = true;
115
        $this->blank_value = $blank_value;
116
        return $this;
117
    }
118
119
    /**
120
     *
121
     * Use value from $field_name for blank field.
122
     *
123
     * @param string $field_name Replace the blank field with the value from field.
124
     *
125
     * @return self
126
     *
127
     */
128
    public function useBlankField($field_name)
129
    {
130
        $this->blank_field = $field_name;
131
        return $this;
132
    }
133
134
    /**
135
     *
136
     * Returns the default failure message for this rule specification.
137
     *
138
     * @return string
139
     *
140
     */
141
    protected function getDefaultMessage()
142
    {
143
        return $this->field . ' should have sanitized to '
144
             . parent::getDefaultMessage();
145
    }
146
}
147