|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the LGPL. For more information please see |
|
17
|
|
|
* <http://phing.info>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* String helper utility class. |
|
22
|
|
|
* |
|
23
|
|
|
* This class includes some Java-like functions for parsing strings, |
|
24
|
|
|
* as well as some functions for getting qualifiers / unqualifying phing-style |
|
25
|
|
|
* classpaths. (e.g. "phing.util.StringHelper"). |
|
26
|
|
|
* |
|
27
|
|
|
* @author Hans Lellelid <[email protected]> |
|
28
|
|
|
* |
|
29
|
|
|
* @package phing.system.util |
|
30
|
|
|
*/ |
|
31
|
|
|
class StringHelper |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $TRUE_VALUES = ["on", "true", "t", "yes", "1"]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private static $FALSE_VALUES = ["off", "false", "f", "no", "0"]; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Remove qualification to name. |
|
45
|
|
|
* E.g. eg.Cat -> Cat |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $qualifiedName |
|
48
|
|
|
* @param string $separator Character used to separate. |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
770 |
|
public static function unqualify($qualifiedName, $separator = '.') |
|
53
|
|
|
{ |
|
54
|
|
|
// if false, then will be 0 |
|
55
|
770 |
|
$pos = strrpos($qualifiedName, $separator); |
|
56
|
770 |
|
if ($pos === false) { |
|
57
|
2 |
|
return $qualifiedName; // there is no '.' in the qualifed name |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
770 |
|
return substr($qualifiedName, $pos + 1); // start just after '.' |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param bool|string $s |
|
65
|
|
|
* |
|
66
|
|
|
* @return boolean |
|
67
|
|
|
*/ |
|
68
|
776 |
|
public static function booleanValue($s) |
|
69
|
|
|
{ |
|
70
|
776 |
|
if (is_bool($s)) { |
|
71
|
769 |
|
return $s; // it's already boolean (not a string) |
|
72
|
|
|
} |
|
73
|
|
|
// otherwise assume it's something like "true" or "t" |
|
74
|
249 |
|
$trimmed = strtolower(trim($s)); |
|
75
|
|
|
|
|
76
|
249 |
|
return (bool) in_array($trimmed, self::$TRUE_VALUES); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* tests if a string is a representative of a boolean |
|
81
|
|
|
* |
|
82
|
|
|
* @param bool|string $s |
|
83
|
|
|
* |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
596 |
|
public static function isBoolean($s) |
|
87
|
|
|
{ |
|
88
|
596 |
|
if (is_bool($s)) { |
|
89
|
|
|
return true; // it already is boolean |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
596 |
|
if ($s === "" || $s === null || !is_string($s)) { |
|
93
|
27 |
|
return false; // not a valid string for testing |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
593 |
|
$test = strtolower(trim($s)); |
|
97
|
|
|
|
|
98
|
593 |
|
return in_array($test, array_merge(self::$FALSE_VALUES, self::$TRUE_VALUES), true); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* tests if a string starts with a given string |
|
103
|
|
|
* |
|
104
|
|
|
* @param $check |
|
105
|
|
|
* @param $string |
|
106
|
|
|
* |
|
107
|
|
|
* @return bool |
|
108
|
|
|
*/ |
|
109
|
792 |
|
public static function startsWith($check, $string) |
|
110
|
|
|
{ |
|
111
|
792 |
|
if ($check === "" || $check === $string) { |
|
112
|
9 |
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
789 |
|
return strpos($string, $check) === 0; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* tests if a string ends with a given string |
|
120
|
|
|
* |
|
121
|
|
|
* @param $check |
|
122
|
|
|
* @param $string |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
120 |
|
public static function endsWith($check, $string) |
|
127
|
|
|
{ |
|
128
|
120 |
|
if ($check === "" || $check === $string) { |
|
129
|
3 |
|
return true; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
118 |
|
return strpos(strrev($string), strrev($check)) === 0; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* a natural way of getting a subtring, php's circular string buffer and strange |
|
137
|
|
|
* return values suck if you want to program strict as of C or friends |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $string |
|
140
|
|
|
* @param int $startpos |
|
141
|
|
|
* @param int $endpos |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
30 |
|
public static function substring($string, $startpos, $endpos = -1) |
|
146
|
|
|
{ |
|
147
|
30 |
|
$len = strlen($string); |
|
148
|
30 |
|
$endpos = (int) (($endpos === -1) ? $len - 1 : $endpos); |
|
149
|
30 |
|
if ($startpos > $len - 1 || $startpos < 0) { |
|
150
|
|
|
trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR); |
|
151
|
|
|
} |
|
152
|
30 |
|
if ($endpos > $len - 1 || $endpos < $startpos) { |
|
153
|
|
|
trigger_error("substring(), Endindex out of bounds must be $startpos<n<" . ($len - 1), E_USER_ERROR); |
|
154
|
|
|
} |
|
155
|
30 |
|
if ($startpos === $endpos) { |
|
156
|
1 |
|
return (string) $string[$startpos]; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
30 |
|
$len = $endpos - $startpos; |
|
160
|
|
|
|
|
161
|
30 |
|
return substr($string, $startpos, $len + 1); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Does the value correspond to a slot variable? |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $value |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool|int |
|
170
|
|
|
*/ |
|
171
|
610 |
|
public static function isSlotVar($value) |
|
172
|
|
|
{ |
|
173
|
610 |
|
$value = trim($value); |
|
174
|
610 |
|
if ($value === "") { |
|
175
|
27 |
|
return false; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
607 |
|
return preg_match('/^%\{([\w\.\-]+)\}$/', $value); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Extracts the variable name for a slot var in the format %{task.current_file} |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $var The var from build file. |
|
185
|
|
|
* |
|
186
|
|
|
* @return string Extracted name part. |
|
187
|
|
|
*/ |
|
188
|
|
|
public static function slotVar($var) |
|
189
|
|
|
{ |
|
190
|
|
|
return trim($var, '%{} '); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|