|
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
|
|
|
use function filter_var; |
|
24
|
|
|
use function is_bool; |
|
25
|
|
|
use function is_string; |
|
26
|
|
|
use function preg_match; |
|
27
|
|
|
use function strlen; |
|
28
|
|
|
use function strpos; |
|
29
|
|
|
use function strrev; |
|
30
|
|
|
use function trigger_error; |
|
31
|
|
|
use function trim; |
|
32
|
|
|
|
|
33
|
|
|
use const FILTER_NULL_ON_FAILURE; |
|
34
|
|
|
use const FILTER_VALIDATE_BOOLEAN; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* String helper utility class. |
|
38
|
|
|
* |
|
39
|
|
|
* This class includes some Java-like functions for parsing strings, |
|
40
|
|
|
* as well as some functions for getting qualifiers / unqualifying phing-style |
|
41
|
|
|
* classpaths. (e.g. "phing.util.StringHelper"). |
|
42
|
|
|
* |
|
43
|
|
|
* @author Hans Lellelid <[email protected]> |
|
44
|
|
|
*/ |
|
45
|
|
|
class StringHelper |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* Converts a string to a boolean according to Phing rules. |
|
49
|
|
|
* |
|
50
|
|
|
* This method has no type hints to avoid "type coercion". |
|
51
|
|
|
* |
|
52
|
|
|
* The following values are considered "true": |
|
53
|
|
|
* |
|
54
|
|
|
* - 'on' (string) |
|
55
|
|
|
* - 'true' (string) |
|
56
|
|
|
* - 'yes' (string) |
|
57
|
|
|
* - '1' (string) |
|
58
|
|
|
* - 1 (int) |
|
59
|
|
|
* - 1.0 (float) |
|
60
|
|
|
* - true (boolean) |
|
61
|
|
|
* |
|
62
|
|
|
* Everything else is "false". Also, string values are trimmed and case-insensitive. |
|
63
|
|
|
* |
|
64
|
|
|
* @param mixed $s Value to be converted |
|
65
|
|
|
* |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
41 |
|
public static function booleanValue($s) |
|
69
|
|
|
{ |
|
70
|
41 |
|
return filter_var($s, FILTER_VALIDATE_BOOLEAN); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* tests if a string is a representative of a boolean. |
|
75
|
|
|
* |
|
76
|
|
|
* This method has no type hints to avoid "type coercion". |
|
77
|
|
|
* |
|
78
|
|
|
* Rules: |
|
79
|
|
|
* |
|
80
|
|
|
* - Valid boolean values: true, false, 'true', 'false', 'on', 'off', 'yes', 'no', '1' and '0'. |
|
81
|
|
|
* - Anything else must not be considered boolean. |
|
82
|
|
|
* - This method is case-insensitive. |
|
83
|
|
|
* - Strings are trimmed. |
|
84
|
|
|
* |
|
85
|
|
|
* @param mixed $s The value to convert to a bool value |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
42 |
|
public static function isBoolean($s) |
|
90
|
|
|
{ |
|
91
|
42 |
|
if (is_bool($s)) { |
|
92
|
2 |
|
return true; // it already is boolean |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
40 |
|
if (!is_string($s) || '' === trim($s)) { |
|
96
|
11 |
|
return false; // not a valid string for testing |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
29 |
|
return null !== filter_var($s, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* tests if a string starts with a given string. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $check |
|
106
|
|
|
* @param string $string |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool |
|
109
|
|
|
*/ |
|
110
|
15 |
|
public static function startsWith($check, $string) |
|
111
|
|
|
{ |
|
112
|
15 |
|
if ('' === $check || $check === $string) { |
|
113
|
4 |
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
11 |
|
return 0 === strpos((string) $string, $check); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* tests if a string ends with a given string. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $check |
|
123
|
|
|
* @param string $string |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
12 |
|
public static function endsWith($check, $string) |
|
128
|
|
|
{ |
|
129
|
12 |
|
if ('' === $check || $check === $string) { |
|
130
|
4 |
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
8 |
|
return 0 === strpos(strrev($string), strrev($check)); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* a natural way of getting a subtring, php's circular string buffer and strange |
|
138
|
|
|
* return values suck if you want to program strict as of C or friends. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $string |
|
141
|
|
|
* @param int $startpos |
|
142
|
|
|
* @param int $endpos |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
16 |
|
public static function substring($string, $startpos, $endpos = -1) |
|
147
|
|
|
{ |
|
148
|
16 |
|
$len = strlen($string); |
|
149
|
16 |
|
$endpos = (int) ((-1 === $endpos) ? $len - 1 : $endpos); |
|
150
|
16 |
|
if ($startpos > $len - 1 || $startpos < 0) { |
|
151
|
3 |
|
trigger_error("substring(), Startindex out of bounds must be 0<n<{$len}", E_USER_ERROR); |
|
152
|
|
|
} |
|
153
|
13 |
|
if ($endpos > $len - 1 || $endpos < $startpos) { |
|
154
|
2 |
|
trigger_error("substring(), Endindex out of bounds must be {$startpos}<n<" . ($len - 1), E_USER_ERROR); |
|
155
|
|
|
} |
|
156
|
11 |
|
if ($startpos === $endpos) { |
|
157
|
4 |
|
return (string) $string[$startpos]; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
7 |
|
$len = $endpos - $startpos; |
|
161
|
|
|
|
|
162
|
7 |
|
return substr((string) $string, $startpos, $len + 1); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Does the value correspond to a slot variable? |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $value |
|
169
|
|
|
* |
|
170
|
|
|
* @return bool|int |
|
171
|
|
|
*/ |
|
172
|
15 |
|
public static function isSlotVar($value) |
|
173
|
|
|
{ |
|
174
|
15 |
|
$value = trim($value); |
|
175
|
15 |
|
if ('' === $value) { |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
15 |
|
return preg_match('/^%\{([\w\.\-]+)\}$/', $value); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Extracts the variable name for a slot var in the format %{task.current_file}. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $var the var from build file |
|
186
|
|
|
* |
|
187
|
|
|
* @return string extracted name part |
|
188
|
|
|
*/ |
|
189
|
7 |
|
public static function slotVar($var) |
|
190
|
|
|
{ |
|
191
|
7 |
|
return trim($var, '%{} '); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|