Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
tel.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Form
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * Form Rule class for the Joomla Platform
14  *
15  * @package Joomla.Platform
16  * @subpackage Form
17  * @since 11.1
18  */
19 class JFormRuleTel extends JFormRule
20 {
21  /**
22  * Method to test the url for a valid parts.
23  *
24  * @param SimpleXMLElement $element The SimpleXMLElement object representing the <field /> tag for the form field object.
25  * @param mixed $value The form field value to validate.
26  * @param string $group The field name group control value. This acts as as an array container for the field.
27  * For example if the field has name="foo" and the group value is set to "bar" then the
28  * full field name would end up being "bar[foo]".
29  * @param JRegistry $input An optional JRegistry object with the entire data set to validate against the entire form.
30  * @param JForm $form The form object for which the field is being tested.
31  *
32  * @return boolean True if the value is valid, false otherwise.
33  *
34  * @since 11.1
35  */
36  public function test(SimpleXMLElement $element, $value, $group = null, JRegistry $input = null, JForm $form = null)
37  {
38  // If the field is empty and not required, the field is valid.
39  $required = ((string) $element['required'] == 'true' || (string) $element['required'] == 'required');
40  if (!$required && empty($value))
41  {
42  return true;
43  }
44 
45  /*
46  * @see http://www.nanpa.com/
47  * @see http://tools.ietf.org/html/rfc4933
48  * @see http://www.itu.int/rec/T-REC-E.164/en
49  *
50  * Regex by Steve Levithan
51  * @see http://blog.stevenlevithan.com/archives/validate-phone-number
52  * @note that valid ITU-T and EPP must begin with +.
53  */
54  $regexarray = array('NANP' => '/^(?:\+?1[-. ]?)?\(?([2-9][0-8][0-9])\)?[-. ]?([2-9][0-9]{2})[-. ]?([0-9]{4})$/',
55  'ITU-T' => '/^\+(?:[0-9] ?){6,14}[0-9]$/', 'EPP' => '/^\+[0-9]{1,3}\.[0-9]{4,14}(?:x.+)?$/');
56  if (isset($element['plan']))
57  {
58 
59  $plan = (string) $element['plan'];
60  if ($plan == 'northamerica' || $plan == 'us')
61  {
62  $plan = 'NANP';
63  }
64  elseif ($plan == 'International' || $plan == 'int' || $plan == 'missdn' || !$plan)
65  {
66  $plan = 'ITU-T';
67  }
68  elseif ($plan == 'IETF')
69  {
70  $plan = 'EPP';
71  }
72 
73  $regex = $regexarray[$plan];
74 
75  // Test the value against the regular expression.
76  if (preg_match($regex, $value) == false)
77  {
78 
79  return false;
80  }
81  }
82  else
83  {
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  if (preg_match($regex, $cleanvalue) == true)
92  {
93 
94  return true;
95  }
96  else
97  {
98 
99  return false;
100  }
101  }
102 
103  return true;
104  }
105 }