summaryrefslogtreecommitdiff
path: root/lib/prado/framework/Web/UI/WebControls/TRangeValidator.php
blob: a6668f4ccf457f23e8dde6750f7c5739f81d205b (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
 * TRangeValidator class file
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link https://github.com/pradosoft/prado
 * @copyright Copyright &copy; 2005-2015 The PRADO Group
 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
 * @package System.Web.UI.WebControls
 */

/**
 * Using TBaseValidator class
 */
Prado::using('System.Web.UI.WebControls.TBaseValidator');

/**
 * TRangeValidator class
 *
 * TRangeValidator tests whether an input value is within a specified range.
 *
 * TRangeValidator uses three key properties to perform its validation.
 * The {@link setMinValue MinValue} and {@link setMaxValue MaxValue}
 * properties specify the minimum and maximum values of the valid range.
 * The {@link setDataType DataType} property is used to specify the
 * data type of the value and the minimum and maximum range values.
 * These values are converted to this data type before the validation
 * operation is performed. The following value types are supported:
 * - <b>Integer</b> A 32-bit signed integer data type.
 * - <b>Float</b> A double-precision floating point number data type.
 * - <b>Date</b> A date data type. The date format can be specified by
 *   setting {@link setDateFormat DateFormat} property, which must be recognizable
 *   by {@link TSimpleDateFormatter}. If the property is not set,
 *   the GNU date syntax is assumed.
 * - <b>String</b> A string data type.
 * - <b>StringLength</b> check for string length.
 *
 * If {@link setStrictComparison StrictComparison} is true, then the ranges
 * are compared as strictly less than the max value and/or strictly greater than the min value.
 *
 * The TRangeValidator allows a special DataType "StringLength" that
 * can be used to verify minimum and maximum string length. The
 * {@link setCharset Charset} property can be used to force a particular
 * charset for comparison. Otherwise, the application charset is used and is
 * defaulted as UTF-8.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0
 */
class TRangeValidator extends TBaseValidator
{
	/**
	 * Gets the name of the javascript class responsible for performing validation for this control.
	 * This method overrides the parent implementation.
	 * @return string the javascript class name
	 */
	protected function getClientClassName()
	{
		return 'Prado.WebUI.TRangeValidator';
	}

	/**
	 * @return string the minimum value of the validation range.
	 */
	public function getMinValue()
	{
		return $this->getViewState('MinValue','');
	}

	/**
	 * Sets the minimum value of the validation range.
	 * @param string the minimum value
	 */
	public function setMinValue($value)
	{
		$this->setViewState('MinValue',TPropertyValue::ensureString($value),'');
	}

	/**
	 * @return string the maximum value of the validation range.
	 */
	public function getMaxValue()
	{
		return $this->getViewState('MaxValue','');
	}

	/**
	 * Sets the maximum value of the validation range.
	 * @param string the maximum value
	 */
	public function setMaxValue($value)
	{
		$this->setViewState('MaxValue',TPropertyValue::ensureString($value),'');
	}

	/**
	 * @param boolean true to perform strict comparison (i.e. strictly less than max and/or strictly greater than min).
	 */
	public function setStrictComparison($value)
	{
		$this->setViewState('StrictComparison', TPropertyValue::ensureBoolean($value),false);
	}

	/**
	 * @return boolean true to perform strict comparison.
	 */
	public function getStrictComparison()
	{
		return $this->getViewState('StrictComparison', false);
	}

	/**
	 * @return TRangeValidationDataType the data type that the values being compared are
	 * converted to before the comparison is made. Defaults to TRangeValidationDataType::String.
	 */
	public function getDataType()
	{
		return $this->getViewState('DataType',TRangeValidationDataType::String);
	}

	/**
	 * Sets the data type that the values being compared are converted to before the comparison is made.
	 * @param TRangeValidationDataType the data type
	 */
	public function setDataType($value)
	{
		$this->setViewState('DataType',TPropertyValue::ensureEnum($value,'TRangeValidationDataType'),TRangeValidationDataType::String);
	}

	/**
     * Sets the date format for a date validation
     * @param string the date format value
     */
	public function setDateFormat($value)
	{
		$this->setViewState('DateFormat', $value, '');
	}

	/**
	 * @return string the date validation date format if any
	 */
	public function getDateFormat()
	{
		return $this->getViewState('DateFormat', '');
	}

	/**
	 * @param string charset for string length comparison.
	 */
	public function setCharset($value)
	{
		$this->setViewState('Charset', $value, '');
	}

	/**
	 * @return string charset for string length comparison.
	 */
	public function getCharset()
	{
		return $this->getViewState('Charset', '');
	}

	/**
	 * This method overrides the parent's implementation.
	 * The validation succeeds if the input data is within the range.
	 * The validation always succeeds if the input data is empty.
	 * @return boolean whether the validation succeeds
	 */
	protected function evaluateIsValid()
	{
		$value=$this->getValidationValue($this->getValidationTarget());
		if($value==='')
			return true;

		switch($this->getDataType())
		{
			case TRangeValidationDataType::Integer:
				return $this->isValidInteger($value);
			case TRangeValidationDataType::Float:
				return $this->isValidFloat($value);
			case TRangeValidationDataType::Date:
				return $this->isValidDate($value);
			case TRangeValidationDataType::StringLength:
				return $this->isValidStringLength($value);
			default:
				return $this->isValidString($value);
		}
	}

	/**
	* Determine if the value is within the integer range.
	* @param string value to validate true
	* @return boolean true if within integer range.
	*/
	protected function isValidInteger($value)
	{
		$minValue=$this->getMinValue();
		$maxValue=$this->getMaxValue();

		$valid=preg_match('/^[-+]?[0-9]+$/',trim($value));
		$value=intval($value);
		if($minValue!=='')
			$valid=$valid && $this->isGreaterThan($value, intval($minValue));
		if($maxValue!=='')
			$valid=$valid && $this->isLessThan($value,intval($maxValue));
		return $valid;
	}

	protected function isLessThan($left,$right)
	{
		return $this->getStrictComparison() ? $left < $right : $left <= $right;
	}

	protected function isGreaterThan($left, $right)
	{
		return $this->getStrictComparison() ? $left > $right : $left >= $right;
	}

	/**
	 * Determine if the value is within the specified float range.
	 * @param string value to validate
	 * @return boolean true if within range.
	 */
	protected function isValidFloat($value)
	{
		$minValue=$this->getMinValue();
		$maxValue=$this->getMaxValue();

		$valid=preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
		$value=floatval($value);
		if($minValue!=='')
			$valid=$valid && $this->isGreaterThan($value,floatval($minValue));
		if($maxValue!=='')
			$valid=$valid && $this->isLessThan($value,floatval($maxValue));
		return $valid;
	}

	/**
	 * Determine if the date is within the specified range.
	 * Uses pradoParseDate and strtotime to get the date from string.
	 * @param string date as string to validate
	 * @return boolean true if within range.
	 */
	protected function isValidDate($value)
	{
		$minValue=$this->getMinValue();
		$maxValue=$this->getMaxValue();

		$valid=true;

		$dateFormat = $this->getDateFormat();
		if($dateFormat!=='')
		{
			$formatter=Prado::createComponent('System.Util.TSimpleDateFormatter', $dateFormat);
			$value = $formatter->parse($value);
			if($minValue!=='')
				$valid=$valid && $this->isGreaterThan($value,$formatter->parse($minValue));
			if($maxValue!=='')
				$valid=$valid && $this->isLessThan($value,$formatter->parse($maxValue));
			return $valid;
		}
		else
		{
			$value=strtotime($value);
			if($minValue!=='')
				$valid=$valid && $this->isGreaterThan($value,strtotime($minValue));
			if($maxValue!=='')
				$valid=$valid && $this->isLessThan($value,strtotime($maxValue));
			return $valid;
		}
	}

	/**
	 * Compare the string with a minimum and a maxiumum value.
	 * Uses strcmp for comparision.
	 * @param string value to compare with.
	 * @return boolean true if the string is within range.
	 */
	protected function isValidString($value)
	{
		$minValue=$this->getMinValue();
		$maxValue=$this->getMaxValue();

		$valid=true;
		if($minValue!=='')
			$valid=$valid && $this->isGreaterThan(strcmp($value,$minValue),0);
		if($maxValue!=='')
			$valid=$valid && $this->isLessThan(strcmp($value,$maxValue),0);
		return $valid;
	}

	/**
	 * @param string string for comparision
	 * @return boolean true if min and max string length are satisfied.
	 */
	protected function isValidStringLength($value)
	{
		$minValue=$this->getMinValue();
		$maxValue=$this->getMaxValue();

		$valid=true;
		$charset = $this->getCharset();
		if($charset==='')
		{
			$app= $this->getApplication()->getGlobalization();
			$charset = $app ? $app->getCharset() : null;
			if(!$charset)
				$charset = 'UTF-8';
		}

		$length = iconv_strlen($value, $charset);
		if($minValue!=='')
			$valid = $valid && $this->isGreaterThan($length,intval($minValue));
		if($maxValue!=='')
			$valid = $valid && $this->isLessThan($length,intval($maxValue));
		return $valid;
	}

	/**
	 * Returns an array of javascript validator options.
	 * @return array javascript validator options.
	 */
	protected function getClientScriptOptions()
	{
		$options=parent::getClientScriptOptions();
		$options['MinValue']=$this->getMinValue();
		$options['MaxValue']=$this->getMaxValue();
		$options['DataType']=$this->getDataType();
		$options['StrictComparison']=$this->getStrictComparison();
		if(($dateFormat=$this->getDateFormat())!=='')
			$options['DateFormat']=$dateFormat;
		return $options;
	}
}


/**
 * TRangeValidationDataType class.
 * TRangeValidationDataType defines the enumerable type for the possible data types that
 * a range validator can validate upon.
 *
 * The following enumerable values are defined:
 * - Integer
 * - Float
 * - Date
 * - String
 * - StringLength
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System.Web.UI.WebControls
 * @since 3.0.4
 */
class TRangeValidationDataType extends TValidationDataType
{
	const StringLength='StringLength';
}