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 /**
13 * Form Field class for the Joomla Platform.
14 * Provides a one line text box with up-down handles to set a number in the field.
15 *
16 * @link http://www.w3.org/TR/html-markup/input.text.html#input.text
17 * @since 3.2
18 */
19 class JFormFieldNumber extends JFormField
20 {
21 /**
22 * The form field type.
23 *
24 * @var string
25 * @since 3.2
26 */
27 protected $type = 'Number';
28
29 /**
30 * The allowable maximum value of the field.
31 *
32 * @var float
33 * @since 3.2
34 */
35 protected $max = null;
36
37 /**
38 * The allowable minimum value of the field.
39 *
40 * @var float
41 * @since 3.2
42 */
43 protected $min = null;
44
45 /**
46 * The step by which value of the field increased or decreased.
47 *
48 * @var float
49 * @since 3.2
50 */
51 protected $step = 0;
52
53 /**
54 * Name of the layout being used to render the field
55 *
56 * @var string
57 * @since 3.7
58 */
59 protected $layout = 'joomla.form.field.number';
60
61 /**
62 * Method to get certain otherwise inaccessible properties from the form field object.
63 *
64 * @param string $name The property name for which to the the value.
65 *
66 * @return mixed The property value or null.
67 *
68 * @since 3.2
69 */
70 public function __get($name)
71 {
72 switch ($name)
73 {
74 case 'max':
75 case 'min':
76 case 'step':
77 return $this->$name;
78 }
79
80 return parent::__get($name);
81 }
82
83 /**
84 * Method to set certain otherwise inaccessible properties of the form field object.
85 *
86 * @param string $name The property name for which to the the value.
87 * @param mixed $value The value of the property.
88 *
89 * @return void
90 *
91 * @since 3.2
92 */
93 public function __set($name, $value)
94 {
95 switch ($name)
96 {
97 case 'step':
98 case 'min':
99 case 'max':
100 $this->$name = (float) $value;
101 break;
102
103 default:
104 parent::__set($name, $value);
105 }
106 }
107
108 /**
109 * Method to attach a JForm object to the field.
110 *
111 * @param SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
112 * @param mixed $value The form field value to validate.
113 * @param string $group The field name group control value. This acts as an array container for the field.
114 * For example if the field has name="foo" and the group value is set to "bar" then the
115 * full field name would end up being "bar[foo]".
116 *
117 * @return boolean True on success.
118 *
119 * @see JFormField::setup()
120 * @since 3.2
121 */
122 public function setup(SimpleXMLElement $element, $value, $group = null)
123 {
124 $return = parent::setup($element, $value, $group);
125
126 if ($return)
127 {
128 // It is better not to force any default limits if none is specified
129 $this->max = isset($this->element['max']) ? (float) $this->element['max'] : null;
130 $this->min = isset($this->element['min']) ? (float) $this->element['min'] : null;
131 $this->step = isset($this->element['step']) ? (float) $this->element['step'] : 1;
132 }
133
134 return $return;
135 }
136
137 /**
138 * Method to get the field input markup.
139 *
140 * @return string The field input markup.
141 *
142 * @since 3.2
143 */
144 protected function getInput()
145 {
146 // Trim the trailing line in the layout file
147 return rtrim($this->getRenderer($this->layout)->render($this->getLayoutData()), PHP_EOL);
148 }
149
150 /**
151 * Method to get the data to be passed to the layout for rendering.
152 *
153 * @return array
154 *
155 * @since 3.7
156 */
157 protected function getLayoutData()
158 {
159 $data = parent::getLayoutData();
160
161 // Initialize some field attributes.
162 $extraData = array(
163 'max' => $this->max,
164 'min' => $this->min,
165 'step' => $this->step,
166 'value' => $this->value,
167 );
168
169 return array_merge($data, $extraData);
170 }
171 }
172