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