| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* String helper utility class.
|
|
|
5 |
*
|
|
|
6 |
* This class includes some Java-like functions for parsing strings,
|
|
|
7 |
* as well as some functions for getting qualifiers / unqualifying phing-style
|
|
|
8 |
* classpaths. (e.g. "phing.util.StringHelper").
|
|
|
9 |
*
|
|
|
10 |
* @author Hans Lellelid <hans@xmpl.org>
|
|
|
11 |
* @package phing.system.util
|
|
|
12 |
*/
|
|
|
13 |
class StringHelper {
|
|
|
14 |
|
|
|
15 |
private static $TRUE_VALUES = array("on", "true", "t", "yes");
|
|
|
16 |
private static $FALSE_VALUES = array("off", "false", "f", "no");
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Replaces identifier tokens with corresponding text values in passed string.
|
|
|
20 |
*
|
|
|
21 |
* @params array $strings Array of strings to multiply. (If string is passed, will convert to array)
|
|
|
22 |
* @params array $tokens The tokens to search for.
|
|
|
23 |
* @params array $replacements The values with which to replace found tokens.
|
|
|
24 |
* @return string
|
|
|
25 |
*/
|
|
|
26 |
public static function multiply($strings, $tokens, $replacements) {
|
|
|
27 |
$strings = (array) $strings;
|
|
|
28 |
$results = array();
|
|
|
29 |
foreach ($strings as $string) {
|
|
|
30 |
$results[] = str_replace($tokens, $replacements, $string);
|
|
|
31 |
}
|
|
|
32 |
return $results;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Remove qualification to name.
|
|
|
37 |
* E.g. eg.Cat -> Cat
|
|
|
38 |
* @param string $qualifiedName
|
|
|
39 |
* @param string $separator Character used to separate.
|
|
|
40 |
*/
|
|
|
41 |
public static function unqualify($qualifiedName, $separator = '.') {
|
|
|
42 |
// if false, then will be 0
|
|
|
43 |
$pos = strrpos($qualifiedName, $separator);
|
|
|
44 |
if ($pos === false) {
|
|
|
45 |
return $qualifiedName; // there is no '.' in the qualifed name
|
|
|
46 |
} else {
|
|
|
47 |
return substr($qualifiedName, $pos + 1); // start just after '.'
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Converts a string to an indexed array of chars
|
|
|
53 |
* There's really no reason for this to be used in PHP, since strings
|
|
|
54 |
* are all accessible using the $string{0} notation.
|
|
|
55 |
* @param string $string
|
|
|
56 |
* @return array
|
|
|
57 |
* @deprecated
|
|
|
58 |
*/
|
|
|
59 |
public static function toCharArray($str) {
|
|
|
60 |
$ret=array();
|
|
|
61 |
$len=strlen($str);
|
|
|
62 |
for ($i=0; $i < $len; $i++) {
|
|
|
63 |
$ret[] = $str{$i};
|
|
|
64 |
}
|
|
|
65 |
return $ret;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Get the qualifier part of a qualified name.
|
|
|
70 |
* E.g. eg.Cat -> eg
|
|
|
71 |
* @return string
|
|
|
72 |
*/
|
|
|
73 |
public static function qualifier($qualifiedName, $seperator = '.') {
|
|
|
74 |
$pos = strrchr($qualifiedName, $seperator);
|
|
|
75 |
if ($pos === false) {
|
|
|
76 |
return '';
|
|
|
77 |
} else {
|
|
|
78 |
return substr($qualifiedName, 0, $pos);
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @param array $columns String[]
|
|
|
84 |
* @param string $prefix
|
|
|
85 |
* @return array String[]
|
|
|
86 |
*/
|
|
|
87 |
public static function prefix( $columns, $prefix) {
|
|
|
88 |
if ($prefix == null) return $columns;
|
|
|
89 |
$qualified = array();
|
|
|
90 |
foreach($columns as $key => $column) {
|
|
|
91 |
$qualified[$key] = $prefix . $column;
|
|
|
92 |
}
|
|
|
93 |
return $qualified;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
/**
|
|
|
97 |
*
|
|
|
98 |
* @return string
|
|
|
99 |
*/
|
|
|
100 |
public static function root($qualifiedName, $separator = '.') {
|
|
|
101 |
$loc = strpos($qualifiedName, $separator);
|
|
|
102 |
return ($loc === false) ? $qualifiedName : substr($qualifiedName, 0, $loc);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* @return int
|
|
|
107 |
*/
|
|
|
108 |
public static function hashCode($string) {
|
|
|
109 |
return crc32($string);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* @return boolean
|
|
|
114 |
*/
|
|
|
115 |
public static function booleanValue($s) {
|
|
|
116 |
if (is_bool($s)) {
|
|
|
117 |
return $s; // it's already boolean (not a string)
|
|
|
118 |
}
|
|
|
119 |
// otherwise assume it's something like "true" or "t"
|
|
|
120 |
$trimmed = strtolower(trim($s));
|
|
|
121 |
return (boolean) in_array($trimmed, self::$TRUE_VALUES);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
/** tests if a string is a representative of a boolean */
|
|
|
125 |
public static function isBoolean($s) {
|
|
|
126 |
|
|
|
127 |
if (is_bool($s)) {
|
|
|
128 |
return true; // it already is boolean
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
if ($s === "" || $s === null || !is_string($s)) {
|
|
|
132 |
return false; // not a valid string for testing
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
$test = trim(strtolower($s));
|
|
|
136 |
return (boolean) in_array($test, array_merge(self::$FALSE_VALUES, self::$TRUE_VALUES));
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
* Creates a key based on any number of passed params.
|
|
|
141 |
* @return string
|
|
|
142 |
*/
|
|
|
143 |
public static function key() {
|
|
|
144 |
$args = func_get_args();
|
|
|
145 |
return serialize($args);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
/** tests if a string starts with a given string */
|
|
|
149 |
public static function startsWith($check, $string) {
|
|
|
150 |
if ($check === "" || $check === $string) {
|
|
|
151 |
return true;
|
|
|
152 |
} else {
|
|
|
153 |
return (strpos($string, $check) === 0) ? true : false;
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
/** tests if a string ends with a given string */
|
|
|
158 |
public static function endsWith($check, $string) {
|
|
|
159 |
if ($check === "" || $check === $string) {
|
|
|
160 |
return true;
|
|
|
161 |
} else {
|
|
|
162 |
return (strpos(strrev($string), strrev($check)) === 0) ? true : false;
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* a natural way of getting a subtring, php's circular string buffer and strange
|
|
|
168 |
* return values suck if you want to program strict as of C or friends
|
|
|
169 |
*/
|
|
|
170 |
public static function substring($string, $startpos, $endpos = -1) {
|
|
|
171 |
$len = strlen($string);
|
|
|
172 |
$endpos = (int) (($endpos === -1) ? $len-1 : $endpos);
|
|
|
173 |
if ($startpos > $len-1 || $startpos < 0) {
|
|
|
174 |
trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR);
|
|
|
175 |
}
|
|
|
176 |
if ($endpos > $len-1 || $endpos < $startpos) {
|
|
|
177 |
trigger_error("substring(), Endindex out of bounds must be $startpos<n<".($len-1), E_USER_ERROR);
|
|
|
178 |
}
|
|
|
179 |
if ($startpos === $endpos) {
|
|
|
180 |
return (string) $string{$startpos};
|
|
|
181 |
} else {
|
|
|
182 |
$len = $endpos-$startpos;
|
|
|
183 |
}
|
|
|
184 |
return substr($string, $startpos, $len+1);
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* Does the value correspond to a slot variable?
|
|
|
189 |
* @param string $value
|
|
|
190 |
*/
|
|
|
191 |
public static function isSlotVar($value) {
|
|
|
192 |
$value = trim($value);
|
|
|
193 |
if ($value === "") return false;
|
|
|
194 |
return preg_match('/^%\{([\w\.\-]+)\}$/', $value);
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Extracts the variable name for a slot var in the format %{task.current_file}
|
|
|
199 |
* @param string $var The var from build file.
|
|
|
200 |
* @return string Extracted name part.
|
|
|
201 |
*/
|
|
|
202 |
public static function slotVar($var) {
|
|
|
203 |
return trim($var, '%{} ');
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
|