Framework  3.9
string_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 
41 {
42  function StringTypeRenderer()
43  {
44  }
45 
46  static function format($value, $template = "")
47  {
48  if (!$template) return $value;
49 
50  if ($template[0] == '/')
51  {
52  $clauses = array();
53  if (preg_match("/(\\/.*?\\/)(.*?)\\//", $template, $clauses))
54  {
55  return preg_replace($clauses[1], $clauses[2], $value);
56  }
57  }
58 
59  if ($template[0] == '|')
60  {
61  $clauses = array();
62  if (preg_match("/(|.*?|)(.*?)|/", $template, $clauses))
63  {
64  return preg_replace($clauses[1], $clauses[2], $value);
65  }
66  }
67 
68  switch($template)
69  {
70  case "codify":
71  return codify($value);
72 
73  case "prettify":
74  return prettify($value);
75 
76  case "upper":
77  return strtoupper($value);
78 
79  case "lower":
80  return strtolower($value);
81 
82  case "ucfirst":
83  return ucfirst($value);
84 
85  case "lcfirst":
86  return lcfirst($value);
87 
88  case "ucwords":
89  return ucwords(strtolower($value));
90 
91  case "stripHTML":
92  return stripHTML($value);
93 
94  case "jsSafe":
95  return jsSafe($value);
96 
97  case "html":
98  return formatAsHTML($value);
99 
100  case "htmlSafe":
101  return htmlsafe($value);
102 
103  case "xml":
104  return xmlEntities($value);
105 
106  case "xmlSafe":
107  return xmlEntities(stripHTML($value));
108 
109  case "url":
110  return urlencode($value);
111 
112  case "phone":
113  return StringTypeRenderer::phone($value);
114 
115  case "firstSentence":
116  return firstSentence($value);
117 
118  case "pluralize":
119  return pluralize($value);
120  default:
121 
122  if (is_numeric($template))
123  {
124  return ellipsis(stripHTML($value), $template, true);
125  }
126  else if (strpos($template, "|") !== false)
127  {
128  $options = explode("|", $template);
129  return $value ? $options[0] : $options[1];
130  }
131  }
132 
133  return $value;
134  }
135 
136  /*
137  * Given a phone number containing various
138  * separator characters between the numbers,
139  * clear out the separators and standardize
140  * the format as
141  * (xxx) xxx-xxxx
142  *
143  * tylerhall on 11/30/-1
144  * http://snipplr.com/view/25/format-phone-number/
145  */
146  static function phone($phone)
147  {
148  $phone = preg_replace("/[^0-9]/", "", $phone);
149 
150  if(strlen($phone) == 7)
151  return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
152  elseif(strlen($phone) == 10)
153  return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
154  else
155  return $phone;
156  }
157 }
158 
static format($value, $template="")
codify($name)
Takes a text string and converts it into a code-compliant format, suitable for use as a variable name...
Definition: functions.inc:1399
xmlEntities($string)
Function to provide html to XML entity renaming.
Definition: functions.inc:1903
pluralize($text, $count=0)
Takes a singular string and makes it plural.
Definition: functions.inc:1428
stripHTML($text)
Definition: functions.inc:847
firstSentence($text)
Returns the first sentence of the supplied text.
Definition: functions.inc:839
jsSafe($str, $escapeEntities=false)
Utility function to escape a string correctly for use in a Javascript client-side call.
Definition: functions.inc:434
formatAsHTML($text)
Takes a string and formats it for display as HTML, removing any HTML tags it contains,...
Definition: functions.inc:1456
ellipsis($txt, $max, $wholeWord=false)
Truncate the supplied text at the given maximum length.
Definition: functions.inc:779
prettify($name)
Takes a variable or field name and converts it into a human-readable version (assuming that the origi...
Definition: functions.inc:1413
htmlsafe($str)
Definition: functions.inc:451