Framework  3.9
phone_number_type_renderer.inc
Go to the documentation of this file.
1 <?php
6 /**************************************************************
7 
8  Copyright (c) 2007-2010 Sonjara, Inc
9 
10  Permission is hereby granted, free of charge, to any person
11  obtaining a copy of this software and associated documentation
12  files (the "Software"), to deal in the Software without
13  restriction, including without limitation the rights to use,
14  copy, modify, merge, publish, distribute, sublicense, and/or sell
15  copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following
17  conditions:
18 
19  The above copyright notice and this permission notice shall be
20  included in all copies or substantial portions of the Software.
21 
22  Except as contained in this notice, the name(s) of the above
23  copyright holders shall not be used in advertising or otherwise
24  to promote the sale, use or other dealings in this Software
25  without prior written authorization.
26 
27  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
29  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
32  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  OTHER DEALINGS IN THE SOFTWARE.
35 
36 *****************************************************************/
37 
38 require_once realpath(dirname(__FILE__)."/abstract_type_renderer.inc");
39 
40 /*
41  * $parts is used by PhoneNumberFieldRenderer
42  *
43  * standard rendering is in the format
44  * (123) 456-7890 ext 12
45  */
47 {
48  static $parts = array(
49  "area_code" => 3,
50  "exchange" => 3,
51  "" => 4, // leave local digits as base field name for RequiredValidator test
52  "extension" => 6
53  );
54 
55 
57  {
58  }
59 
60  static function format($value, $template = "")
61  {
62  trace("PhoneNumberTypeRenderer function format value is $value", 4);
63  switch($template)
64  {
65 
66  case "standard":
67  return PhoneNumberTypeRenderer::standard($value);
68 
69  default:
70 
71  return PhoneNumberTypeRenderer::standard($value);
72  }
73 
74  return $value;
75  }
76 
77  static function standard($value)
78  {
79  trace("PhoneNumberTypeRenderer function standard value is $value", 4);
80  if (!$value) return "";
81 
82  $valueParts = PhoneNumberTypeRenderer::getValueParts($value);
83 
84  $phone = implode($valueParts, "-");
85 
86  $area_code = $valueParts["area_code"];
87  $ext = $valueParts["extension"];
88  $phone = preg_replace("/^$area_code-/", "($area_code) ", $phone);
89  if($ext)
90  $phone = preg_replace("/-$ext$/", " ext $ext", $phone);
91  else
92  $phone = preg_replace("/-*$/", "", $phone);
93 
94  return $phone;
95  }
96 
97  /*
98  * Given a string of just digits, break into
99  * a value for each phone parts area code,
100  * exchange, local, and extension).
101  */
102  static function getValueParts($value)
103  {
104  //AJG - Remove any non-numeric formatting before slicing up
105  $value = preg_replace("/\\D/", "", $value);
106 
108 
109  $valueParts = array();
110 
111  $idx = 0;
112  foreach($parts as $part => $limit)
113  {
114  $valueParts[$part] = substr($value, $idx, $limit);
115  $idx += $limit;
116  }
117 
118  return $valueParts;
119  }
120 }
121 
static format($value, $template="")
trace($msg, $lvl=3, $callStack=null)
Send output to the trace log.
Definition: functions.inc:1010