Passed
Push — main ( 46fa35...3ac4cc )
by Michiel
06:19
created

StringHelper::slotVar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
namespace Phing\Util;
21
/**
22
 * String helper utility class.
23
 *
24
 * This class includes some Java-like functions for parsing strings,
25
 * as well as some functions for getting qualifiers / unqualifying phing-style
26
 * classpaths.  (e.g. "phing.util.StringHelper").
27
 *
28
 * @author Hans Lellelid <[email protected]>
29
 *
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
     * @param bool|string $s
45
     *
46
     * @return boolean
47
     */
48 848
    public static function booleanValue($s)
49
    {
50 848
        if (is_bool($s)) {
51 841
            return $s; // it's already boolean (not a string)
52
        }
53
        // otherwise assume it's something like "true" or "t"
54 275
        $trimmed = strtolower(trim($s));
55
56 275
        return (bool) in_array($trimmed, self::$TRUE_VALUES);
57
    }
58
59
    /**
60
     * tests if a string is a representative of a boolean
61
     *
62
     * @param bool|string $s
63
     *
64
     * @return bool
65
     */
66 668
    public static function isBoolean($s)
67
    {
68 668
        if (is_bool($s)) {
69
            return true; // it already is boolean
70
        }
71
72 668
        if ($s === "" || $s === null || !is_string($s)) {
73 24
            return false; // not a valid string for testing
74
        }
75
76 665
        $test = strtolower(trim($s));
77
78 665
        return in_array($test, array_merge(self::$FALSE_VALUES, self::$TRUE_VALUES), true);
79
    }
80
81
    /**
82
     * tests if a string starts with a given string
83
     *
84
     * @param $check
85
     * @param $string
86
     *
87
     * @return bool
88
     */
89 861
    public static function startsWith($check, $string)
90
    {
91 861
        if ($check === "" || $check === $string) {
92 8
            return true;
93
        }
94
95 858
        return strpos($string, $check) === 0;
96
    }
97
98
    /**
99
     * tests if a string ends with a given string
100
     *
101
     * @param $check
102
     * @param $string
103
     *
104
     * @return bool
105
     */
106 123
    public static function endsWith($check, $string)
107
    {
108 123
        if ($check === "" || $check === $string) {
109 4
            return true;
110
        }
111
112 121
        return strpos(strrev($string), strrev($check)) === 0;
113
    }
114
115
    /**
116
     * a natural way of getting a subtring, php's circular string buffer and strange
117
     * return values suck if you want to program strict as of C or friends
118
     *
119
     * @param string $string
120
     * @param int    $startpos
121
     * @param int    $endpos
122
     *
123
     * @return string
124
     */
125 33
    public static function substring($string, $startpos, $endpos = -1)
126
    {
127 33
        $len = strlen($string);
128 33
        $endpos = (int) (($endpos === -1) ? $len - 1 : $endpos);
129 33
        if ($startpos > $len - 1 || $startpos < 0) {
130
            trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR);
131
        }
132 33
        if ($endpos > $len - 1 || $endpos < $startpos) {
133
            trigger_error("substring(), Endindex out of bounds must be $startpos<n<" . ($len - 1), E_USER_ERROR);
134
        }
135 33
        if ($startpos === $endpos) {
136 1
            return (string) $string[$startpos];
137
        }
138
139 33
        $len = $endpos - $startpos;
140
141 33
        return substr($string, $startpos, $len + 1);
142
    }
143
144
    /**
145
     * Does the value correspond to a slot variable?
146
     *
147
     * @param string $value
148
     *
149
     * @return bool|int
150
     */
151 686
    public static function isSlotVar($value)
152
    {
153 686
        $value = trim($value);
154 686
        if ($value === "") {
155 28
            return false;
156
        }
157
158 683
        return preg_match('/^%\{([\w\.\-]+)\}$/', $value);
159
    }
160
161
    /**
162
     * Extracts the variable name for a slot var in the format %{task.current_file}
163
     *
164
     * @param string $var The var from build file.
165
     *
166
     * @return string Extracted name part.
167
     */
168
    public static function slotVar($var)
169
    {
170
        return trim($var, '%{} ');
171
    }
172
}
173