1 | <?php |
||
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() |
|
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() |
|
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) |
|
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) |
|
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() |
|
131 | |||
132 | 5 | public function canAlwaysRun() |
|
136 | |||
137 | } |
||
138 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: