Passed
Push — master ( afa2e3...eb32d2 )
by Siad
18:11
created

StringHelper::startsWith()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 1
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
/**
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 821
    public static function unqualify($qualifiedName, $separator = '.')
53
    {
54
        // if false, then will be 0
55 821
        $pos = strrpos($qualifiedName, $separator);
56 821
        if ($pos === false) {
57 4
            return $qualifiedName; // there is no '.' in the qualifed name
58
        }
59
60 821
        return substr($qualifiedName, $pos + 1); // start just after '.'
61
    }
62
63
    /**
64
     * @param bool|string $s
65
     *
66
     * @return boolean
67
     */
68 827
    public static function booleanValue($s)
69
    {
70 827
        if (is_bool($s)) {
71 820
            return $s; // it's already boolean (not a string)
72
        }
73
        // otherwise assume it's something like "true" or "t"
74 274
        $trimmed = strtolower(trim($s));
75
76 274
        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 644
    public static function isBoolean($s)
87
    {
88 644
        if (is_bool($s)) {
89
            return true; // it already is boolean
90
        }
91
92 644
        if ($s === "" || $s === null || !is_string($s)) {
93 27
            return false; // not a valid string for testing
94
        }
95
96 641
        $test = strtolower(trim($s));
97
98 641
        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 843
    public static function startsWith($check, $string)
110
    {
111 843
        if ($check === "" || $check === $string) {
112 10
            return true;
113
        }
114
115 840
        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 132
    public static function endsWith($check, $string)
127
    {
128 132
        if ($check === "" || $check === $string) {
129 4
            return true;
130
        }
131
132 130
        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 32
    public static function substring($string, $startpos, $endpos = -1)
146
    {
147 32
        $len = strlen($string);
148 32
        $endpos = (int) (($endpos === -1) ? $len - 1 : $endpos);
149 32
        if ($startpos > $len - 1 || $startpos < 0) {
150
            trigger_error("substring(), Startindex out of bounds must be 0<n<$len", E_USER_ERROR);
151
        }
152 32
        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 32
        if ($startpos === $endpos) {
156 1
            return (string) $string[$startpos];
157
        }
158
159 32
        $len = $endpos - $startpos;
160
161 32
        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 661
    public static function isSlotVar($value)
172
    {
173 661
        $value = trim($value);
174 661
        if ($value === "") {
175 29
            return false;
176
        }
177
178 658
        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