Completed
Pull Request — 3.x (#130)
by jake
02:10
created

SanitizeSpec   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 3
cbo 1
dl 0
loc 129
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 21 5
A to() 0 5 1
A toBlankOr() 0 5 1
A useBlankValue() 0 6 1
A useBlankField() 0 5 1
A getDefaultMessage() 0 5 1
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
    /**
39
     *
40
     * Applies the rule specification to a subject.
41
     *
42
     * @param mixed $subject The filter subject.
43
     *
44
     * @return bool True on success, false on failure.
45
     *
46
     */
47 7
    public function __invoke($subject)
48
    {
49
50 7
        if ($this->subjectFieldIsBlank($subject) && $this->blank_field) {
51 1
            $field = $this->field;
52 1
            $replace_with = $this->blank_field;
53 1
            $subject->$field = $subject->$replace_with;
54 1
        }
55
56 7
        if (! $this->subjectFieldIsBlank($subject)) {
57 6
            return parent::__invoke($subject);
58
        }
59
60 3
        if (! $this->allow_blank) {
61 1
            return false;
62
        }
63
64 2
        $field = $this->field;
65 2
        $subject->$field = $this->blank_value;
66 2
        return true;
67
    }
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.
76
     *
77
     * @return self
78
     *
79
     */
80 5
    public function to($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...
81
    {
82 5
        $this->allow_blank = false;
83 5
        return $this->init(func_get_args());
84
    }
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
     */
97 3
    public function toBlankOr($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...
98
    {
99 3
        $this->allow_blank = true;
100 3
        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
     *
111
     */
112 1
    public function useBlankValue($blank_value)
113
    {
114 1
        $this->allow_blank = true;
115 1
        $this->blank_value = $blank_value;
116 1
        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 1
    public function useBlankField($field_name)
129
    {
130 1
        $this->blank_field = $field_name;
131 1
        return $this;
132
    }
133
134
    /**
135
     *
136
     * Returns the default failure message for this rule specification.
137
     *
138
     * @return string
139
     *
140
     */
141 1
    protected function getDefaultMessage()
142
    {
143 1
        return $this->field . ' should have sanitized to '
144 1
             . parent::getDefaultMessage();
145
    }
146
}
147