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
|
|
|
* Applies the rule specification to a subject. |
32
|
|
|
* |
33
|
|
|
* @param mixed $subject The filter subject. |
34
|
|
|
* |
35
|
|
|
* @return bool True on success, false on failure. |
36
|
|
|
* |
37
|
|
|
*/ |
38
|
7 |
|
public function __invoke($subject) |
39
|
|
|
{ |
40
|
7 |
|
if (! $this->subjectFieldIsBlank($subject)) { |
41
|
6 |
|
return parent::__invoke($subject); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
if (! $this->allow_blank) { |
45
|
1 |
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
$field = $this->field; |
49
|
2 |
|
$subject->$field = $this->blank_value; |
50
|
2 |
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* Sanitize the field using this rule (blank not allowed). |
56
|
|
|
* |
57
|
|
|
* @param string $rule The rule name. |
58
|
|
|
* |
59
|
|
|
* @param ...$args Arguments for the rule. |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
6 |
|
public function to($rule) |
|
|
|
|
65
|
|
|
{ |
66
|
6 |
|
$this->allow_blank = false; |
67
|
6 |
|
return $this->init(func_get_args()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* Sanitize the using this rule (blank allowed). |
73
|
|
|
* |
74
|
|
|
* @param string $rule The rule name. |
75
|
|
|
* |
76
|
|
|
* @param ...$args Arguments for the rule. |
77
|
|
|
* |
78
|
|
|
* @return self |
79
|
|
|
* |
80
|
|
|
*/ |
81
|
2 |
|
public function toBlankOr($rule) |
|
|
|
|
82
|
|
|
{ |
83
|
2 |
|
$this->allow_blank = true; |
84
|
2 |
|
return $this->init(func_get_args()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
* Use this value for blank fields. |
90
|
|
|
* |
91
|
|
|
* @param mixed $blank_value Replace the blank field with this value. |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
* |
95
|
|
|
*/ |
96
|
2 |
|
public function useBlankValue($blank_value) |
97
|
|
|
{ |
98
|
2 |
|
$this->allow_blank = true; |
99
|
2 |
|
$this->blank_value = $blank_value; |
100
|
2 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* Returns the default failure message for this rule specification. |
106
|
|
|
* |
107
|
|
|
* @return string |
108
|
|
|
* |
109
|
|
|
*/ |
110
|
1 |
|
protected function getDefaultMessage() |
111
|
|
|
{ |
112
|
1 |
|
return $this->field . ' should have sanitized to ' |
113
|
1 |
|
. parent::getDefaultMessage(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.