[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/ -> FormRule.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   *
   9   * Remove phpcs exception with deprecated constant JCOMPAT_UNICODE_PROPERTIES
  10   * @phpcs:disable PSR1.Files.SideEffects
  11   */
  12  
  13  namespace Joomla\CMS\Form;
  14  
  15  \defined('JPATH_PLATFORM') or die;
  16  
  17  use Joomla\Registry\Registry;
  18  
  19  // Detect if we have full UTF-8 and unicode PCRE support.
  20  if (!\defined('JCOMPAT_UNICODE_PROPERTIES')) {
  21      /**
  22       * Flag indicating UTF-8 and PCRE support is present
  23       *
  24       * @var    boolean
  25       * @since  1.6
  26       *
  27       * @deprecated 5.0 Will be removed without replacement (Also remove phpcs exception)
  28       */
  29      \define('JCOMPAT_UNICODE_PROPERTIES', (bool) @preg_match('/\pL/u', 'a'));
  30  }
  31  
  32  /**
  33   * Form Rule class for the Joomla Platform.
  34   *
  35   * @since  1.6
  36   */
  37  class FormRule
  38  {
  39      /**
  40       * The regular expression to use in testing a form field value.
  41       *
  42       * @var    string
  43       * @since  1.6
  44       */
  45      protected $regex;
  46  
  47      /**
  48       * The regular expression modifiers to use when testing a form field value.
  49       *
  50       * @var    string
  51       * @since  1.6
  52       */
  53      protected $modifiers = '';
  54  
  55      /**
  56       * Method to test the value.
  57       *
  58       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  59       * @param   mixed              $value    The form field value to validate.
  60       * @param   string             $group    The field name group control value. This acts as as an array container for the field.
  61       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  62       *                                       full field name would end up being "bar[foo]".
  63       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  64       * @param   Form               $form     The form object for which the field is being tested.
  65       *
  66       * @return  boolean  True if the value is valid, false otherwise.
  67       *
  68       * @since   1.6
  69       * @throws  \UnexpectedValueException if rule is invalid.
  70       */
  71      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  72      {
  73          // Check for a valid regex.
  74          if (empty($this->regex)) {
  75              throw new \UnexpectedValueException(sprintf('%s has invalid regex.', \get_class($this)));
  76          }
  77  
  78          // Detect if we have full UTF-8 and unicode PCRE support.
  79          static $unicodePropertiesSupport = null;
  80  
  81          if ($unicodePropertiesSupport === null) {
  82              $unicodePropertiesSupport = (bool) @\preg_match('/\pL/u', 'a');
  83          }
  84  
  85          // Add unicode property support if available.
  86          if ($unicodePropertiesSupport) {
  87              $this->modifiers = (strpos($this->modifiers, 'u') !== false) ? $this->modifiers : $this->modifiers . 'u';
  88          }
  89  
  90          // Test the value against the regular expression.
  91          if (preg_match(\chr(1) . $this->regex . \chr(1) . $this->modifiers, $value)) {
  92              return true;
  93          }
  94  
  95          return false;
  96      }
  97  }


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