[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Rule/ -> TimeRule.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2019 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\Rule;
  11  
  12  use Joomla\CMS\Factory;
  13  use Joomla\CMS\Form\Form;
  14  use Joomla\CMS\Form\FormRule;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\Registry\Registry;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('JPATH_PLATFORM') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Form Rule class for the Joomla Platform.
  24   *
  25   * @since  4.0.0
  26   */
  27  class TimeRule extends FormRule
  28  {
  29      /**
  30       * Method to test the range for a number value using min and max attributes.
  31       *
  32       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  33       * @param   mixed              $value    The form field value to validate.
  34       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  35       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  36       *                                       full field name would end up being "bar[foo]".
  37       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  38       * @param   Form               $form     The form object for which the field is being tested.
  39       *
  40       * @return  boolean  True if the value is valid, false otherwise.
  41       *
  42       * @since   4.0.0
  43       *
  44       * @throws \Exception
  45       */
  46      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null): bool
  47      {
  48          // Check if the field is required.
  49          $required = ((string) $element['required'] === 'true' || (string) $element['required'] === 'required');
  50  
  51          // If the value is empty and the field is not required return True.
  52          if (($value === '' || $value === null) && !$required) {
  53              return true;
  54          }
  55  
  56          $stringValue = (string) $value;
  57  
  58          // If the length of a field is smaller than 5 return error message
  59          if (strlen($stringValue) !== 5 && !isset($element['step'])) {
  60              Factory::getApplication()->enqueueMessage(
  61                  Text::_('JLIB_FORM_FIELD_INVALID_TIME_INPUT'),
  62                  'warning'
  63              );
  64  
  65              return false;
  66          }
  67  
  68          // If the third symbol isn't a ':' return error message
  69          if ($stringValue[2] !== ':') {
  70              Factory::getApplication()->enqueueMessage(
  71                  Text::_('JLIB_FORM_FIELD_INVALID_TIME_INPUT'),
  72                  'warning'
  73              );
  74  
  75              return false;
  76          }
  77  
  78          // If the are other symbols except of numbers and ':' return error message
  79          if (!preg_match('#^[0-9:]+$#', $stringValue)) {
  80              Factory::getApplication()->enqueueMessage(
  81                  Text::_('JLIB_FORM_FIELD_INVALID_TIME_INPUT'),
  82                  'warning'
  83              );
  84  
  85              return false;
  86          }
  87  
  88          // If min and max is set
  89          if (isset($element['min']) && isset($element['max'])) {
  90              $min = $element['min'][0] . $element['min'][1];
  91              $max = $element['max'][0] . $element['max'][1];
  92  
  93              // If the input is smaller than the set min return error message
  94              if (intval($min) > intval($stringValue[0] . $stringValue[1])) {
  95                  Factory::getApplication()->enqueueMessage(
  96                      Text::_('JLIB_FORM_FIELD_INVALID_MIN_TIME', $min),
  97                      'warning'
  98                  );
  99  
 100                  return false;
 101              }
 102  
 103              // If the input is greater than the set max return error message
 104              if (intval($max) < intval($stringValue[0] . $stringValue[1])) {
 105                  Factory::getApplication()->enqueueMessage(
 106                      Text::_('JLIB_FORM_FIELD_INVALID_MAX_TIME'),
 107                      'warning'
 108                  );
 109  
 110                  return false;
 111              }
 112  
 113              // If the hour input is equal to the set max but the minutes input is greater than zero return error message
 114              if (intval($max) === intval($stringValue[0] . $stringValue[1])) {
 115                  if (intval($element['min'][3] . $element['min'][4]) !== 0) {
 116                      Factory::getApplication()->enqueueMessage(
 117                          Text::_('JLIB_FORM_FIELD_INVALID_MAX_TIME'),
 118                          'warning'
 119                      );
 120  
 121                      return false;
 122                  }
 123              }
 124          }
 125  
 126          // If the first symbol is greater than 2 return error message
 127          if (intval($stringValue[0]) > 2) {
 128              Factory::getApplication()->enqueueMessage(
 129                  Text::_('JLIB_FORM_FIELD_INVALID_TIME_INPUT'),
 130                  'warning'
 131              );
 132  
 133              return false;
 134          }
 135  
 136          // If the first symbol is greater than 2 and the second symbol is greater than 3 return error message
 137          if (intval($stringValue[0]) === 2 && intval($stringValue[1]) > 3) {
 138              Factory::getApplication()->enqueueMessage(
 139                  Text::_('JLIB_FORM_FIELD_INVALID_TIME_INPUT'),
 140                  'warning'
 141              );
 142  
 143              return false;
 144          }
 145  
 146          // If the fourth symbol is greater than 5 return error message
 147          if (intval($stringValue[3]) > 5) {
 148              Factory::getApplication()->enqueueMessage(
 149                  Text::_('JLIB_FORM_FIELD_INVALID_TIME_INPUT'),
 150                  'warning'
 151              );
 152  
 153              return false;
 154          }
 155  
 156          // If the step is set return same error messages as above but taking into a count that there 8 and not 5 symbols
 157          if (isset($element['step'])) {
 158              if (
 159                  strlen($stringValue) !== 8
 160                  || intval($stringValue[5]) !== ':'
 161                  || intval($stringValue[6]) > 5
 162              ) {
 163                  Factory::getApplication()->enqueueMessage(
 164                      Text::_('JLIB_FORM_FIELD_INVALID_TIME_INPUT_SECONDS'),
 165                      'warning'
 166                  );
 167  
 168                  return false;
 169              }
 170          }
 171  
 172          return true;
 173      }
 174  }


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