File: Valid.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (241 lines) | stat: -rw-r--r-- 5,297 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
/**
 * Copyright (c) STMicroelectronics, 2007. All Rights Reserved.
 *
 * Originally written by Manuel VACELET, 2007.
 * Copyright 2014, Franck Villaume - TrivialDev
 *
 * This file is a part of Fusionforge.
 *
 * Fusionforge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Fusionforge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Codendi. If not, see <http://www.gnu.org/licenses/>.
 */

require_once 'common/valid/Rule.class.php';

/**
 * @package Codendi
 */
class Valid {
	/**
	 * @access private
	 */
	var $errors;

	/**
	 * @access private
	 */
	var $key;

	/**
	 * @access private
	 */
	var $rules;

	/**
	 * @access private
	 */
	var $isRequired;

	/**
	 * @access private
	 */
	var $useFeedback;

	/**
	 * @access private
	 */
	var $globalErrorMessage;

	/**
	 * @access private
	 */
	var $isValid;

	/**
	 * Constructor
	 */
	function Valid($key = null) {
		$this->key = $key;
		$this->errors = array();
		$this->rules = array();
		$this->isRequired = false;
		$this->useFeedback = true;
		$this->globalErrorMessage = null;
		$this->isValid;
	}

	/**
	 * Return the variable name on which rules must applies.
	 *
	 * @access private
	 */
	function getKey() {
		return $this->key;
	}

	/**
	 * Add a new rule in this validation.
	 *
	 * ou can add a custom error message that will bypass the default one that
	 * comes with the rule.
	 * @param Rule   $rule    Reference on rule.
	 * @param string|bool $message Error message.
	 */
	function addRule(&$rule, $message=false) {
		$this->rules[] =& $rule;
		$this->errors[] = $message;
	}

	/**
	 * The value is required.
	 *
	 * All rules must succeed (as usual). Empty / null values are forbidden
	 * (raise an error). And all failure generate an error (instead of a
	 * warning).
	 */
	function required() {
		$this->isRequired = true;
	}

	/**
	 * Turn feedback off.
	 */
	function disableFeedback() {
		$this->useFeedback = false;
	}

	/**
	 * Set a global error message that will replace all other messages.
	 *
	 * Note: If no error, no message raised. The message is raised with either
	 * 'warning' or 'error' level according to required();
	 * @param	String	Error message
	 */
	function setErrorMessage($msg) {
		$this->globalErrorMessage = $msg;
	}

	/**
	 * Return true if given value is empty
	 *
	 * @access	private
	 * @param	mixed	$value	Value to test
	 * @return	boolean
	 */
	function isValueEmpty($value) {
		return ($value === '' || $value === false || $value === null);
	}

	/**
	 * Append feedback in the global Response object.
	 * @access private
	 */
	function addFeedback($level, $error) {
		global $feedback, $error_msg, $warning_msg;
		switch ($level) {
			case 'error': {
				$error_msg .= $level._(': ').$error;
				break;
			}
			case 'warning': {
				$warning_msg .= $level._(': ').$error;
				break;
			}
			case 'feedback': {
				$feedback .= $level._(': ').$error;
				break;
			}
		}
	}

	/**
	 * Generate error message according to settings.
	 *
	 * Takes in account user requirement 'required' and
	 * 'disableFeedback'. Empty error messages are discarded.
	 * @access private
	 */
	function populateFeedback() {
		if($this->useFeedback) {
			$level = 'warning';
			if($this->isRequired) {
				$level = 'error';
			}
			if($this->globalErrorMessage !== null && !$this->isValid) {
				$this->addFeedback($level, $this->globalErrorMessage);
			} else {
				foreach($this->errors as $error) {
					if($error != '') {
						$this->addFeedback($level, $error);
					}
				}
			}
		}
	}

	/**
	 * Prepare error message on Rule:isValid result.
	 *
	 * If the test succeeded, the error message is cleared (either custom or
	 * built-in messages).
	 * @access private
	 * @param int	$i	Index of the Rule that was applied.
	 * @param bool	$result	Result of the test.
	 */
	function errorMessage($i, $result) {
		if($result === true) {
			$this->errors[$i] = '';
		} else {
			if($this->errors[$i] === false) {
				$this->errors[$i] = $this->rules[$i]->getErrorMessage($this->key);
			}
		}
	}

	/**
	 * Apply each rule on the given value and prepare feedback.
	 *
	 * @access	private
	 * @param	mixed	$value	Value to test.
	 */
	function checkEachRules($value) {
		$isValid = true;
		$rCtr = count($this->rules);
		for($i = 0; $i < $rCtr; $i++) {
			$valid = $this->rules[$i]->isValid($value);
			$this->errorMessage($i, $valid);
			$isValid = $isValid && $valid;
		}
		if($isValid && $this->isRequired && $this->isValueEmpty($value)) {
			$this->isValid = false;
		} else {
			$this->isValid = $isValid;
		}
		$this->populateFeedback();
	}

	/**
	 * Run validation on given value.
	 *
	 * @param	mixed	$value	Value to test.
	 * @return	bool
	 */
	function validate($value) {
		if($this->isRequired || (!$this->isRequired && !$this->isValueEmpty($value))) {
			$this->checkEachRules($value);
			return $this->isValid;
		}
		return true;
	}
}