[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Form/Filter/ -> TelFilter.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\Filter;
  11  
  12  use Joomla\CMS\Form\Form;
  13  use Joomla\CMS\Form\FormFilterInterface;
  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 Filter class for phone numbers
  22   *
  23   * @since  4.0.0
  24   */
  25  class TelFilter implements FormFilterInterface
  26  {
  27      /**
  28       * Method to filter a field value.
  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  mixed   The filtered value.
  39       *
  40       * @since   4.0.0
  41       */
  42      public function filter(\SimpleXMLElement $element, $value, $group = null, Registry $input = null, Form $form = null)
  43      {
  44          $value = trim($value);
  45  
  46          // Does it match the NANP pattern?
  47          if (preg_match('/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/', $value) == 1) {
  48              $number = (string) preg_replace('/[^\d]/', '', $value);
  49  
  50              if (substr($number, 0, 1) === '1') {
  51                  $number = substr($number, 1);
  52              }
  53  
  54              if (substr($number, 0, 2) === '+1') {
  55                  $number = substr($number, 2);
  56              }
  57  
  58              $result = '1.' . $number;
  59          } elseif (preg_match('/^\+(?:[0-9] ?){6,14}[0-9]$/', $value) == 1) {
  60              // If not, does it match ITU-T?
  61              $countrycode = substr($value, 0, strpos($value, ' '));
  62              $countrycode = (string) preg_replace('/[^\d]/', '', $countrycode);
  63              $number = strstr($value, ' ');
  64              $number = (string) preg_replace('/[^\d]/', '', $number);
  65              $result = $countrycode . '.' . $number;
  66          } elseif (preg_match('/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/', $value) == 1) {
  67              // If not, does it match EPP?
  68              if (strstr($value, 'x')) {
  69                  $xpos = strpos($value, 'x');
  70                  $value = substr($value, 0, $xpos);
  71              }
  72  
  73              $result = str_replace('+', '', $value);
  74          } elseif (preg_match('/[0-9]{1,3}\.[0-9]{4,14}$/', $value) == 1) {
  75              // Maybe it is already ccc.nnnnnnn?
  76              $result = $value;
  77          } else {
  78              // If not, can we make it a string of digits?
  79              $value = (string) preg_replace('/[^\d]/', '', $value);
  80  
  81              if ($value != null && \strlen($value) <= 15) {
  82                  $length = \strlen($value);
  83  
  84                  // If it is fewer than 13 digits assume it is a local number
  85                  if ($length <= 12) {
  86                      $result = '.' . $value;
  87                  } else {
  88                      // If it has 13 or more digits let's make a country code.
  89                      $cclen = $length - 12;
  90                      $result = substr($value, 0, $cclen) . '.' . substr($value, $cclen);
  91                  }
  92              } else {
  93                  // If not let's not save anything.
  94                  $result = '';
  95              }
  96          }
  97  
  98          return $result;
  99      }
 100  }


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