CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

StdLib.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: StdLib.php
4 #
5 # Part of the ScoutLib application support library
6 # Copyright 2016 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu
8 #
9
14 class StdLib
15 {
16
17 # ---- PUBLIC INTERFACE --------------------------------------------------
18
23 public static function GetMyCaller()
24 {
25 $Trace = version_compare(PHP_VERSION, "5.4.0", ">=")
26 ? debug_backtrace(FALSE, 2) : debug_backtrace(FALSE);
27 $Caller = basename($Trace[1]["file"]).":".$Trace[1]["line"];
28 return $Caller;
29 }
30
47 public static function CheckMyCaller($DesiredCaller, $ExceptionMsg = NULL)
48 {
49 # retrieve caller info
50 $Trace = version_compare(PHP_VERSION, "5.4.0", ">=")
51 ? debug_backtrace(FALSE, 3) : debug_backtrace(FALSE);
52 $FullFile = $Trace[1]["file"];
53 $File = basename($FullFile);
54 $Line = $Trace[1]["line"];
55 $Class = isset($Trace[2]["class"]) ? $Trace[2]["class"] : "";
56 $Function = isset($Trace[2]["function"]) ? $Trace[2]["function"] : "";
57
58 # if caller does not match desired caller
59 if (($DesiredCaller != $Class)
60 && ($DesiredCaller != $Class."::".$Function)
61 && ($DesiredCaller != $Class.$Function)
62 && ($DesiredCaller != $File)
63 && ($DesiredCaller != $File.":".$Line))
64 {
65 # if exception message supplied
66 if ($ExceptionMsg !== NULL)
67 {
68 # make any needed substitutions in exception message
69 $Msg = str_replace(
70 array(
71 "%FILE%",
72 "%LINE%",
73 "%FULLFILE%",
74 "%CLASS%",
75 "%FUNCTION%",
76 "%METHOD%"),
77 array(
78 $File,
79 $Line,
80 $FullFile,
81 $Class,
82 $Function,
83 $Class."::".$Function),
84 $ExceptionMsg);
85
86 # throw exception
87 throw new Exception($Msg);
88 }
89 else
90 {
91 # report to our caller that their caller was not the desired one
92 return FALSE;
93 }
94 }
95
96 # report to our caller that their caller was not the desired one
97 return TRUE;
98 }
99
105 public static function Pluralize($Word)
106 {
107 # return word unchanged if singular and plural are the same
108 if (in_array(strtolower($Word), self::$UncountableWords))
109 {
110 return $Word;
111 }
112
113 # check for irregular singular forms
114 foreach (self::$IrregularWords as $Pattern => $Result)
115 {
116 $Pattern = '/'.$Pattern.'$/i';
117 if (preg_match($Pattern, $Word))
118 {
119 return preg_replace($Pattern, $Result, $Word);
120 }
121 }
122
123 # check for matches using regular expressions
124 foreach (self::$PluralizePatterns as $Pattern => $Result)
125 {
126 if (preg_match($Pattern, $Word))
127 {
128 return preg_replace($Pattern, $Result, $Word);
129 }
130 }
131
132 # return word unchanged if we could not process it
133 return $Word;
134 }
135
141 public static function Singularize($Word)
142 {
143 # return word unchanged if singular and plural are the same
144 if (in_array(strtolower($Word), self::$UncountableWords))
145 {
146 return $Word;
147 }
148
149 # check for irregular plural forms
150 foreach (self::$IrregularWords as $Result => $Pattern)
151 {
152 $Pattern = '/'.$Pattern.'$/i';
153 if (preg_match($Pattern, $Word))
154 {
155 return preg_replace($Pattern, $Result, $Word);
156 }
157 }
158
159 # check for matches using regular expressions
160 foreach (self::$SingularizePatterns as $Pattern => $Result)
161 {
162 if (preg_match($Pattern, $Word))
163 {
164 return preg_replace($Pattern, $Result, $Word);
165 }
166 }
167
168 # return word unchanged if we could not process it
169 return $Word;
170 }
171
172
173 # ---- PRIVATE INTERFACE -------------------------------------------------
174
175 private static $PluralizePatterns = array(
176 '/(quiz)$/i' => "$1zes",
177 '/^(ox)$/i' => "$1en",
178 '/([m|l])ouse$/i' => "$1ice",
179 '/(matr|vert|ind)ix|ex$/i' => "$1ices",
180 '/(x|ch|ss|sh)$/i' => "$1es",
181 '/([^aeiouy]|qu)y$/i' => "$1ies",
182 '/(hive)$/i' => "$1s",
183 '/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
184 '/(shea|lea|loa|thie)f$/i' => "$1ves",
185 '/sis$/i' => "ses",
186 '/([ti])um$/i' => "$1a",
187 '/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
188 '/(bu)s$/i' => "$1ses",
189 '/(alias)$/i' => "$1es",
190 '/(octop)us$/i' => "$1i",
191 '/(ax|test)is$/i' => "$1es",
192 '/(us)$/i' => "$1es",
193 '/s$/i' => "s",
194 '/$/' => "s"
195 );
196 private static $SingularizePatterns = array(
197 '/(quiz)zes$/i' => "$1",
198 '/(matr)ices$/i' => "$1ix",
199 '/(vert|ind)ices$/i' => "$1ex",
200 '/^(ox)en$/i' => "$1",
201 '/(alias)es$/i' => "$1",
202 '/(octop|vir)i$/i' => "$1us",
203 '/(cris|ax|test)es$/i' => "$1is",
204 '/(shoe)s$/i' => "$1",
205 '/(o)es$/i' => "$1",
206 '/(bus)es$/i' => "$1",
207 '/([m|l])ice$/i' => "$1ouse",
208 '/(x|ch|ss|sh)es$/i' => "$1",
209 '/(m)ovies$/i' => "$1ovie",
210 '/(s)eries$/i' => "$1eries",
211 '/([^aeiouy]|qu)ies$/i' => "$1y",
212 '/([lr])ves$/i' => "$1f",
213 '/(tive)s$/i' => "$1",
214 '/(hive)s$/i' => "$1",
215 '/(li|wi|kni)ves$/i' => "$1fe",
216 '/(shea|loa|lea|thie)ves$/i'=> "$1f",
217 '/(^analy)ses$/i' => "$1sis",
218 '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => "$1$2sis",
219 '/([ti])a$/i' => "$1um",
220 '/(n)ews$/i' => "$1ews",
221 '/(h|bl)ouses$/i' => "$1ouse",
222 '/(corpse)s$/i' => "$1",
223 '/(us)es$/i' => "$1",
224 '/s$/i' => ""
225 );
226 private static $IrregularWords = array(
227 'move' => 'moves',
228 'foot' => 'feet',
229 'goose' => 'geese',
230 'sex' => 'sexes',
231 'child' => 'children',
232 'man' => 'men',
233 'tooth' => 'teeth',
234 'person' => 'people'
235 );
236 private static $UncountableWords = array(
237 'sheep',
238 'fish',
239 'deer',
240 'series',
241 'species',
242 'money',
243 'rice',
244 'information',
245 'equipment'
246 );
247 }
StdLib\CheckMyCaller
static CheckMyCaller($DesiredCaller, $ExceptionMsg=NULL)
Check the caller of the current function.
Definition: StdLib.php:47
StdLib\GetMyCaller
static GetMyCaller()
Get string with file and line number for call to current function.
Definition: StdLib.php:23
StdLib\Pluralize
static Pluralize($Word)
Pluralize an English word.
Definition: StdLib.php:105
StdLib
Standard utility library.
Definition: StdLib.php:14
PHP
PHP
Definition: RSSClient--Test.php:12
StdLib\Singularize
static Singularize($Word)
Singularize an English word.
Definition: StdLib.php:141