1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Form
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * Form Rule class for the Joomla Platform.
16 *
17 * @since 3.5
18 */
19 class JFormRuleNumber extends JFormRule
20 {
21 /**
22 * Method to test the range for a number value using min and max attributes.
23 *
24 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
25 * @param mixed $value The form field value to validate.
26 * @param string $group The field name group control value. This acts as an array container for the field.
27 * For example if the field has name="foo" and the group value is set to "bar" then the
28 * full field name would end up being "bar[foo]".
29 * @param Registry $input An optional Registry object with the entire data set to validate against the entire form.
30 * @param JForm $form The form object for which the field is being tested.
31 *
32 * @return boolean True if the value is valid, false otherwise.
33 *
34 * @since 3.5
35 */
36 public function test(SimpleXMLElement $element, $value, $group = null, Registry $input = null, JForm $form = null)
37 {
38 // Check if the field is required.
39 $required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
40
41 // If the value is empty and the field is not required return True.
42 if (($value === '' || $value === null) && ! $required)
43 {
44 return true;
45 }
46
47 $float_value = (float) $value;
48
49 if (isset($element['min']))
50 {
51 $min = (float) $element['min'];
52
53 if ($min > $float_value)
54 {
55 return false;
56 }
57 }
58
59 if (isset($element['max']))
60 {
61 $max = (float) $element['max'];
62
63 if ($max < $float_value)
64 {
65 return false;
66 }
67 }
68
69 return true;
70 }
71 }
72