[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Field/ -> IntegerField.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Form\Field;
  11  
  12  use Joomla\CMS\HTML\HTMLHelper;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Form Field class for the Joomla Platform.
  20   * Provides a select list of integers with specified first, last and step values.
  21   *
  22   * @since  1.7.0
  23   */
  24  class IntegerField extends ListField
  25  {
  26      /**
  27       * The form field type.
  28       *
  29       * @var    string
  30       * @since  1.7.0
  31       */
  32      protected $type = 'Integer';
  33  
  34      /**
  35       * Method to get the field options.
  36       *
  37       * @return  array  The field option objects.
  38       *
  39       * @since   1.7.0
  40       */
  41      protected function getOptions()
  42      {
  43          $options = array();
  44  
  45          // Initialize some field attributes.
  46          $first = (int) $this->element['first'];
  47          $last = (int) $this->element['last'];
  48          $step = (int) $this->element['step'];
  49  
  50          // Sanity checks.
  51          if ($step == 0) {
  52              // Step of 0 will create an endless loop.
  53              return $options;
  54          } elseif ($first < $last && $step < 0) {
  55              // A negative step will never reach the last number.
  56              return $options;
  57          } elseif ($first > $last && $step > 0) {
  58              // A position step will never reach the last number.
  59              return $options;
  60          } elseif ($step < 0) {
  61              // Build the options array backwards.
  62              for ($i = $first; $i >= $last; $i += $step) {
  63                  $options[] = HTMLHelper::_('select.option', $i);
  64              }
  65          } else {
  66              // Build the options array.
  67              for ($i = $first; $i <= $last; $i += $step) {
  68                  $options[] = HTMLHelper::_('select.option', $i);
  69              }
  70          }
  71  
  72          // Merge any additional options in the XML definition.
  73          $options = array_merge(parent::getOptions(), $options);
  74  
  75          return $options;
  76      }
  77  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer