[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2011 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\Form\Form;
  13  use Joomla\CMS\Form\FormRule;
  14  use Joomla\Registry\Registry;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Form Rule class for the Joomla Platform
  22   *
  23   * @since  1.7.0
  24   */
  25  class TelRule extends FormRule
  26  {
  27      /**
  28       * Method to test the url for a valid parts.
  29       *
  30       * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
  31       * @param   mixed              $value    The form field value to validate.
  32       * @param   string             $group    The field name group control value. This acts as an array container for the field.
  33       *                                       For example if the field has name="foo" and the group value is set to "bar" then the
  34       *                                       full field name would end up being "bar[foo]".
  35       * @param   Registry           $input    An optional Registry object with the entire data set to validate against the entire form.
  36       * @param   Form               $form     The form object for which the field is being tested.
  37       *
  38       * @return  boolean  True if the value is valid, false otherwise.
  39       *
  40       * @since   1.7.0
  41       */
  42      public function test(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  43      {
  44          // If the field is empty and not required, the field is valid.
  45          $required = ((string) $element['required'] === 'true' || (string) $element['required'] === 'required');
  46  
  47          if (!$required && empty($value)) {
  48              return true;
  49          }
  50  
  51          /*
  52           * @link http://www.nanpa.com/
  53           * @link http://tools.ietf.org/html/rfc4933
  54           * @link http://www.itu.int/rec/T-REC-E.164/en
  55           *
  56           * Regex by Steve Levithan
  57           * @link http://blog.stevenlevithan.com/archives/validate-phone-number
  58           * @note that valid ITU-T and EPP must begin with +.
  59           */
  60          $regexarray = array(
  61              'NANP' => '/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/',
  62              'ITU-T' => '/^\+(?:[0-9] ?){6,14}[0-9]$/',
  63              'EPP' => '/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/',
  64          );
  65  
  66          if (isset($element['plan'])) {
  67              $plan = (string) $element['plan'];
  68  
  69              if ($plan === 'northamerica' || $plan === 'us') {
  70                  $plan = 'NANP';
  71              } elseif ($plan === 'International' || $plan === 'int' || $plan === 'missdn' || !$plan) {
  72                  $plan = 'ITU-T';
  73              } elseif ($plan === 'IETF') {
  74                  $plan = 'EPP';
  75              }
  76  
  77              $regex = $regexarray[$plan];
  78  
  79              // Test the value against the regular expression.
  80              if (preg_match($regex, $value) == false) {
  81                  return false;
  82              }
  83          } else {
  84              /*
  85               * If the rule is set but no plan is selected just check that there are between
  86               * 7 and 15 digits inclusive and no illegal characters (but common number separators
  87               * are allowed).
  88               */
  89              $cleanvalue = preg_replace('/[+. \-(\)]/', '', $value);
  90              $regex = '/^[0-9]{7,15}?$/';
  91  
  92              if (preg_match($regex, $cleanvalue) == true) {
  93                  return true;
  94              } else {
  95                  return false;
  96              }
  97          }
  98  
  99          return true;
 100      }
 101  }


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