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 JFormHelper::loadFieldClass('list');
13
14 /**
15 * Form Field class for the Joomla Platform.
16 * Provides a select list of integers with specified first, last and step values.
17 *
18 * @since 11.1
19 */
20 class JFormFieldInteger extends JFormFieldList
21 {
22 /**
23 * The form field type.
24 *
25 * @var string
26 * @since 11.1
27 */
28 protected $type = 'Integer';
29
30 /**
31 * Method to get the field options.
32 *
33 * @return array The field option objects.
34 *
35 * @since 11.1
36 */
37 protected function getOptions()
38 {
39 $options = array();
40
41 // Initialize some field attributes.
42 $first = (int) $this->element['first'];
43 $last = (int) $this->element['last'];
44 $step = (int) $this->element['step'];
45
46 // Sanity checks.
47 if ($step == 0)
48 {
49 // Step of 0 will create an endless loop.
50 return $options;
51 }
52 elseif ($first < $last && $step < 0)
53 {
54 // A negative step will never reach the last number.
55 return $options;
56 }
57 elseif ($first > $last && $step > 0)
58 {
59 // A position step will never reach the last number.
60 return $options;
61 }
62 elseif ($step < 0)
63 {
64 // Build the options array backwards.
65 for ($i = $first; $i >= $last; $i += $step)
66 {
67 $options[] = JHtml::_('select.option', $i);
68 }
69 }
70 else
71 {
72 // Build the options array.
73 for ($i = $first; $i <= $last; $i += $step)
74 {
75 $options[] = JHtml::_('select.option', $i);
76 }
77 }
78
79 // Merge any additional options in the XML definition.
80 $options = array_merge(parent::getOptions(), $options);
81
82 return $options;
83 }
84 }
85