|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Fuel\Validation |
|
4
|
|
|
* @version 2.0 |
|
5
|
|
|
* @author Fuel Development Team |
|
6
|
|
|
* @license MIT License |
|
7
|
|
|
* @copyright 2010 - 2013 Fuel Development Team |
|
8
|
|
|
* @link http://fuelphp.com |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Fuel\Validation; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Tests the AbstractRule class |
|
15
|
|
|
* |
|
16
|
|
|
* @package Fuel\Validation |
|
17
|
|
|
* @author Fuel Development Team |
|
18
|
|
|
* @since 2.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractRule implements RuleInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Contains the rule failure message |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $message; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Contains any parameters needed by validation rules |
|
32
|
|
|
* |
|
33
|
|
|
* @var mixed |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $params; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Set to true to always run the rule when validating, regardless of if the data exists. |
|
39
|
|
|
* |
|
40
|
|
|
* @var bool |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $alwaysRun = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Creates a new validation rule |
|
46
|
|
|
* |
|
47
|
|
|
* @param mixed $params |
|
48
|
|
|
* @param string $message |
|
49
|
|
|
* |
|
50
|
|
|
* @since 2.0 |
|
51
|
|
|
*/ |
|
52
|
310 |
|
public function __construct($params = null, $message = null) |
|
53
|
|
|
{ |
|
54
|
310 |
|
$this->setParameter($params); |
|
55
|
|
|
|
|
56
|
310 |
|
if ($message) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
1 |
|
$this->setMessage($message); |
|
59
|
|
|
} |
|
60
|
310 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Gets the failure message for this rule |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
* |
|
67
|
|
|
* @since 2.0 |
|
68
|
|
|
*/ |
|
69
|
33 |
|
public function getMessage() |
|
70
|
|
|
{ |
|
71
|
33 |
|
return $this->message; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Should return a list of tokens that can be inserted into this rule's error message. |
|
76
|
|
|
* For example this might be an upper bound for MaxValue or the regex passed to the Regex rule. |
|
77
|
|
|
* Values should always have a string key so they can be easily identified. |
|
78
|
|
|
* |
|
79
|
|
|
* @return string[] |
|
80
|
|
|
*/ |
|
81
|
7 |
|
public function getMessageParameters() |
|
82
|
|
|
{ |
|
83
|
7 |
|
return array(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sets the failure message for this rule |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $message |
|
90
|
|
|
* |
|
91
|
|
|
* @return $this |
|
92
|
|
|
* |
|
93
|
|
|
* @since 2.0 |
|
94
|
|
|
*/ |
|
95
|
5 |
|
public function setMessage($message) |
|
96
|
|
|
{ |
|
97
|
5 |
|
$this->message = $message; |
|
98
|
|
|
|
|
99
|
5 |
|
return $this; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Sets the parameter for this validation rule. |
|
104
|
|
|
* See each Rule's documentation for what this should be. |
|
105
|
|
|
* |
|
106
|
|
|
* @param mixed $params |
|
107
|
|
|
* |
|
108
|
|
|
* @return $this |
|
109
|
|
|
* |
|
110
|
|
|
* @since 2.0 |
|
111
|
|
|
*/ |
|
112
|
310 |
|
public function setParameter($params) |
|
113
|
|
|
{ |
|
114
|
310 |
|
$this->params = $params; |
|
115
|
|
|
|
|
116
|
310 |
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns the value of the set parameter. |
|
121
|
|
|
* See each Rule's documentation for what the parameter does. |
|
122
|
|
|
* |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
* |
|
125
|
|
|
* @since 2.0 |
|
126
|
|
|
*/ |
|
127
|
183 |
|
public function getParameter() |
|
128
|
|
|
{ |
|
129
|
183 |
|
return $this->params; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
5 |
|
public function canAlwaysRun() |
|
133
|
|
|
{ |
|
134
|
5 |
|
return $this->alwaysRun; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: