Passed
Branch php-cs-fixer (b9836a)
by Fabio
15:02
created

TRangeValidator::getMaxValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * TRangeValidator class file
4
 *
5
 * @author Qiang Xue <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @copyright Copyright &copy; 2005-2016 The PRADO Group
8
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
9
 * @package Prado\Web\UI\WebControls
10
 */
11
12
namespace Prado\Web\UI\WebControls;
13
14
use Prado\Prado;
15
use Prado\TPropertyValue;
16
use Prado\Util\TSimpleDateFormatter;
17
18
/**
19
 * TRangeValidator class
20
 *
21
 * TRangeValidator tests whether an input value is within a specified range.
22
 *
23
 * TRangeValidator uses three key properties to perform its validation.
24
 * The {@link setMinValue MinValue} and {@link setMaxValue MaxValue}
25
 * properties specify the minimum and maximum values of the valid range.
26
 * The {@link setDataType DataType} property is used to specify the
27
 * data type of the value and the minimum and maximum range values.
28
 * These values are converted to this data type before the validation
29
 * operation is performed. The following value types are supported:
30
 * - <b>Integer</b> A 32-bit signed integer data type.
31
 * - <b>Float</b> A double-precision floating point number data type.
32
 * - <b>Date</b> A date data type. The date format can be specified by
33
 *   setting {@link setDateFormat DateFormat} property, which must be recognizable
34
 *   by {@link TSimpleDateFormatter}. If the property is not set,
35
 *   the GNU date syntax is assumed.
36
 * - <b>String</b> A string data type.
37
 * - <b>StringLength</b> check for string length.
38
 *
39
 * If {@link setStrictComparison StrictComparison} is true, then the ranges
40
 * are compared as strictly less than the max value and/or strictly greater than the min value.
41
 *
42
 * The TRangeValidator allows a special DataType "StringLength" that
43
 * can be used to verify minimum and maximum string length. The
44
 * {@link setCharset Charset} property can be used to force a particular
45
 * charset for comparison. Otherwise, the application charset is used and is
46
 * defaulted as UTF-8.
47
 *
48
 * @author Qiang Xue <[email protected]>
49
 * @package Prado\Web\UI\WebControls
50
 * @since 3.0
51
 */
52
class TRangeValidator extends TBaseValidator
53
{
54
	/**
55
	 * Gets the name of the javascript class responsible for performing validation for this control.
56
	 * This method overrides the parent implementation.
57
	 * @return string the javascript class name
58
	 */
59
	protected function getClientClassName()
60
	{
61
		return 'Prado.WebUI.TRangeValidator';
62
	}
63
64
	/**
65
	 * @return string the minimum value of the validation range.
66
	 */
67
	public function getMinValue()
68
	{
69
		return $this->getViewState('MinValue', '');
70
	}
71
72
	/**
73
	 * Sets the minimum value of the validation range.
74
	 * @param string the minimum value
75
	 */
76
	public function setMinValue($value)
77
	{
78
		$this->setViewState('MinValue', TPropertyValue::ensureString($value), '');
79
	}
80
81
	/**
82
	 * @return string the maximum value of the validation range.
83
	 */
84
	public function getMaxValue()
85
	{
86
		return $this->getViewState('MaxValue', '');
87
	}
88
89
	/**
90
	 * Sets the maximum value of the validation range.
91
	 * @param string the maximum value
92
	 */
93
	public function setMaxValue($value)
94
	{
95
		$this->setViewState('MaxValue', TPropertyValue::ensureString($value), '');
96
	}
97
98
	/**
99
	 * @param boolean true to perform strict comparison (i.e. strictly less than max and/or strictly greater than min).
100
	 */
101
	public function setStrictComparison($value)
102
	{
103
		$this->setViewState('StrictComparison', TPropertyValue::ensureBoolean($value), false);
104
	}
105
106
	/**
107
	 * @return boolean true to perform strict comparison.
108
	 */
109
	public function getStrictComparison()
110
	{
111
		return $this->getViewState('StrictComparison', false);
112
	}
113
114
	/**
115
	 * @return TRangeValidationDataType the data type that the values being compared are
116
	 * converted to before the comparison is made. Defaults to TRangeValidationDataType::String.
117
	 */
118
	public function getDataType()
119
	{
120
		return $this->getViewState('DataType', TRangeValidationDataType::String);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getViewSta...dationDataType::String) also could return the type string which is incompatible with the documented return type Prado\Web\UI\WebControls\TRangeValidationDataType.
Loading history...
121
	}
122
123
	/**
124
	 * Sets the data type that the values being compared are converted to before the comparison is made.
125
	 * @param TRangeValidationDataType the data type
126
	 */
127
	public function setDataType($value)
128
	{
129
		$this->setViewState('DataType', TPropertyValue::ensureEnum($value, 'Prado\\Web\\UI\\WebControls\\TRangeValidationDataType'), TRangeValidationDataType::String);
130
	}
131
132
	/**
133
	 * Sets the date format for a date validation
134
	 * @param string the date format value
135
	 */
136
	public function setDateFormat($value)
137
	{
138
		$this->setViewState('DateFormat', $value, '');
139
	}
140
141
	/**
142
	 * @return string the date validation date format if any
143
	 */
144
	public function getDateFormat()
145
	{
146
		return $this->getViewState('DateFormat', '');
147
	}
148
149
	/**
150
	 * @param string charset for string length comparison.
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\charset was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
151
	 */
152
	public function setCharset($value)
153
	{
154
		$this->setViewState('Charset', $value, '');
155
	}
156
157
	/**
158
	 * @return string charset for string length comparison.
159
	 */
160
	public function getCharset()
161
	{
162
		return $this->getViewState('Charset', '');
163
	}
164
165
	/**
166
	 * This method overrides the parent's implementation.
167
	 * The validation succeeds if the input data is within the range.
168
	 * The validation always succeeds if the input data is empty.
169
	 * @return boolean whether the validation succeeds
170
	 */
171
	protected function evaluateIsValid()
172
	{
173
		$value = $this->getValidationValue($this->getValidationTarget());
174
		if($value === '')
175
			return true;
176
177
		switch($this->getDataType())
178
		{
179
			case TRangeValidationDataType::Integer:
180
				return $this->isValidInteger($value);
181
			case TRangeValidationDataType::Float:
182
				return $this->isValidFloat($value);
183
			case TRangeValidationDataType::Date:
184
				return $this->isValidDate($value);
185
			case TRangeValidationDataType::StringLength:
186
				return $this->isValidStringLength($value);
187
			default:
188
				return $this->isValidString($value);
189
		}
190
	}
191
192
	/**
193
	 * Determine if the value is within the integer range.
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\value was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
194
	 * @param string value to validate true
195
	 * @return boolean true if within integer range.
196
	 */
197
	protected function isValidInteger($value)
198
	{
199
		$minValue = $this->getMinValue();
200
		$maxValue = $this->getMaxValue();
201
202
		$valid = preg_match('/^[-+]?[0-9]+$/', trim($value));
203
		$value = intval($value);
204
		if($minValue !== '')
205
			$valid = $valid && $this->isGreaterThan($value, intval($minValue));
206
		if($maxValue !== '')
207
			$valid = $valid && $this->isLessThan($value, intval($maxValue));
208
		return $valid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $valid also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
209
	}
210
211
	protected function isLessThan($left, $right)
212
	{
213
		return $this->getStrictComparison() ? $left < $right : $left <= $right;
214
	}
215
216
	protected function isGreaterThan($left, $right)
217
	{
218
		return $this->getStrictComparison() ? $left > $right : $left >= $right;
219
	}
220
221
	/**
222
	 * Determine if the value is within the specified float range.
223
	 * @param string value to validate
224
	 * @return boolean true if within range.
225
	 */
226
	protected function isValidFloat($value)
227
	{
228
		$minValue = $this->getMinValue();
229
		$maxValue = $this->getMaxValue();
230
231
		$valid = preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/', trim($value));
232
		$value = floatval($value);
233
		if($minValue !== '')
234
			$valid = $valid && $this->isGreaterThan($value, floatval($minValue));
235
		if($maxValue !== '')
236
			$valid = $valid && $this->isLessThan($value, floatval($maxValue));
237
		return $valid;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $valid also could return the type integer which is incompatible with the documented return type boolean.
Loading history...
238
	}
239
240
	/**
241
	 * Determine if the date is within the specified range.
0 ignored issues
show
Bug introduced by
The type Prado\Web\UI\WebControls\date was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
242
	 * Uses pradoParseDate and strtotime to get the date from string.
243
	 * @param string date as string to validate
244
	 * @return boolean true if within range.
245
	 */
246
	protected function isValidDate($value)
247
	{
248
		$minValue = $this->getMinValue();
249
		$maxValue = $this->getMaxValue();
250
251
		$valid = true;
252
253
		$dateFormat = $this->getDateFormat();
254
		if($dateFormat !== '')
255
		{
256
			$formatter = new TSimpleDateFormatter($dateFormat);
257
			$value = $formatter->parse($value);
258
			if($minValue !== '')
259
				$valid = $valid && $this->isGreaterThan($value, $formatter->parse($minValue));
260
			if($maxValue !== '')
261
				$valid = $valid && $this->isLessThan($value, $formatter->parse($maxValue));
262
			return $valid;
263
		}
264
		else
265
		{
266
			$value = strtotime($value);
267
			if($minValue !== '')
268
				$valid = $valid && $this->isGreaterThan($value, strtotime($minValue));
269
			if($maxValue !== '')
270
				$valid = $valid && $this->isLessThan($value, strtotime($maxValue));
271
			return $valid;
272
		}
273
	}
274
275
	/**
276
	 * Compare the string with a minimum and a maxiumum value.
277
	 * Uses strcmp for comparision.
278
	 * @param string value to compare with.
279
	 * @return boolean true if the string is within range.
280
	 */
281
	protected function isValidString($value)
282
	{
283
		$minValue = $this->getMinValue();
284
		$maxValue = $this->getMaxValue();
285
286
		$valid = true;
287
		if($minValue !== '')
288
			$valid = $valid && $this->isGreaterThan(strcmp($value, $minValue), 0);
289
		if($maxValue !== '')
290
			$valid = $valid && $this->isLessThan(strcmp($value, $maxValue), 0);
291
		return $valid;
292
	}
293
294
	/**
295
	 * @param string string for comparision
296
	 * @return boolean true if min and max string length are satisfied.
297
	 */
298
	protected function isValidStringLength($value)
299
	{
300
		$minValue = $this->getMinValue();
301
		$maxValue = $this->getMaxValue();
302
303
		$valid = true;
304
		$charset = $this->getCharset();
305
		if($charset === '')
306
		{
307
			$app = $this->getApplication()->getGlobalization();
308
			$charset = $app ? $app->getCharset() : null;
0 ignored issues
show
introduced by
The condition $app can never be true.
Loading history...
309
			if(!$charset)
310
				$charset = 'UTF-8';
311
		}
312
313
		$length = iconv_strlen($value, $charset);
314
		if($minValue !== '')
315
			$valid = $valid && $this->isGreaterThan($length, intval($minValue));
316
		if($maxValue !== '')
317
			$valid = $valid && $this->isLessThan($length, intval($maxValue));
318
		return $valid;
319
	}
320
321
	/**
322
	 * Returns an array of javascript validator options.
323
	 * @return array javascript validator options.
324
	 */
325
	protected function getClientScriptOptions()
326
	{
327
		$options = parent::getClientScriptOptions();
328
		$options['MinValue'] = $this->getMinValue();
329
		$options['MaxValue'] = $this->getMaxValue();
330
		$options['DataType'] = $this->getDataType();
331
		$options['StrictComparison'] = $this->getStrictComparison();
332
		if(($dateFormat = $this->getDateFormat()) !== '')
333
			$options['DateFormat'] = $dateFormat;
334
		return $options;
335
	}
336
}