JString   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 300
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 96.88%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 1
dl 0
loc 300
ccs 62
cts 64
cp 0.9688
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 4 1
A equals() 0 4 1
A equalsIgnoreCase() 0 4 1
A length() 0 4 1
A concat() 0 8 2
A contains() 0 4 1
A endsWith() 0 14 2
A startsWith() 0 9 2
A charAt() 0 10 3
A indexOf() 0 9 2
A lastIndexOf() 0 9 4
A isEmpty() 0 4 1
A split() 0 4 1
A toLowerCase() 0 4 1
A toUpperCase() 0 4 1
A trim() 0 4 1
A substring() 0 8 2
A replace() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace LazyEight\BasicTypes;
4
5
use LazyEight\BasicTypes\Exceptions\IndexOutOfBoundsException;
6
7
/**
8
 * Class JString
9
 * @package LazyEight\BasicTypes
10
 * @author Victor Ribeiro <[email protected]>
11
 */
12
class JString
13
{
14
    /*
15
     * @var string
16
     */
17
    private $value;
18
19
    /**
20
     * @param string $str
21
     */
22 11
    public function __construct(string $str)
23
    {
24 11
        $this->value = (string) $str;
25 11
    }
26
27
    /**
28
     * @return string
29
     */
30 1
    public function getValue() : string
31
    {
32 1
        return $this->value;
33
    }
34
35
    /**
36
     * Compares this string to the specified object. The result is true if and only if the
37
     * argument is not null and is a string that represents the same sequence of
38
     * characters as this object.
39
     *
40
     * @param string $stringToCompare
41
     * @return bool
42
     */
43 2
    public function equals(string $stringToCompare) : bool
44
    {
45 2
        return $this->value === $stringToCompare;
46
    }
47
48
    /**
49
     * Compares this string to another string, ignoring case considerations.
50
     * Two strings are considered equal ignoring case if they are of the same length
51
     * and corresponding characters in the two strings are equal ignoring case.
52
     *
53
     * Two characters c1 and c2 are considered the same ignoring case if at least one of the
54
     * following is true:
55
     * The two characters are the same (as compared by the == operator)
56
     * Applying the method JString.toUpperCase(char) to each character produces the same result
57
     * Applying the method JString.toLowerCase(char) to each character produces the same result
58
     * Returns:
59
     * true if the argument is not null and it represents an equivalent JString ignoring case;
60
     * false otherwise
61
     *
62
     * @param string $anotherString
63
     * @return bool
64
     */
65 3
    public function equalsIgnoreCase($anotherString) : bool
66
    {
67 3
        return $this->toLowerCase() == (new JString($anotherString))->toLowerCase();
68
    }
69
70
    /**
71
     * Returns the length of the sequence of characters represented by this object.
72
     *
73
     * @return int
74
     */
75 2
    public function length() : int
76
    {
77 2
        return mb_strlen($this->value);
78
    }
79
80
    /**
81
     * Concatenates the specified string to the end of this string.
82
     * If the length of the argument string is 0, then a string is returned.
83
     * Otherwise, a new string is created, representing a character sequence that
84
     * is the concatenation of the character sequence represented by this JString object and
85
     * the character sequence represented by the argument string.
86
     * returns:
87
     * A string that represents the concatenation of this object's characters followed by the
88
     * string argument characters.
89
     *
90
     * @param string $str
91
     * @return string
92
     */
93 1
    public function concat($str)
94
    {
95 1
        if (mb_strlen($str) == 0) {
96
            return $str;
97
        }
98
99 1
        return $this->value.$str;
100
    }
101
102
    /**
103
     * Returns true if and only if this string contains the specified sequence of char values.
104
     * false otherwise
105
     *
106
     * @param string $str
107
     * @throws \InvalidArgumentException
108
     * @return bool
109
     */
110 2
    public function contains(string $str) : bool
111
    {
112 2
        return (bool) mb_strpos($this->value, $str);
113
    }
114
115
    /**
116
     * Returns true if the string ends with $substring, false otherwise. By
117
     * default, the comparison is case-sensitive, but can be made insensitive
118
     * by setting $caseSensitive to false.
119
     *
120
     * @param string $suffix
121
     * @param bool $caseSensitive
122
     * @return bool
123
     */
124 1
    public function endsWith($suffix, $caseSensitive = true)
125
    {
126 1
        $substringLength = mb_strlen($suffix);
127 1
        $strLength = $this->length();
128 1
        $endOfStr = mb_substr($this->value, $strLength - $substringLength, $substringLength);
129 1
        $substring = $suffix;
130
131 1
        if (!$caseSensitive) {
132 1
            $substring = mb_strtolower($suffix);
133 1
            $endOfStr = mb_strtolower($endOfStr);
134
        }
135
136 1
        return $substring == $endOfStr;
137
    }
138
139
    /**
140
     * Tests if the substring of this string beginning at the specified index starts
141
     * with the specified prefix.
142
     * Returns:
143
     * true if the character sequence represented by the argument is a prefix of the substring of
144
     * this object starting at index toOffset; false otherwise. The result is false if toOffset
145
     * is negative or greater than the length of this JString Object
146
     *
147
     * @param string $prefix
148
     * @param int $toOffset
149
     * @return bool
150
     */
151 1
    public function startsWith($prefix, $toOffset = 0)
152
    {
153 1
        $strToSearch = mb_substr($this->value, $toOffset, mb_strlen($prefix));
154 1
        if (false !== mb_strpos($strToSearch, $prefix)) {
155 1
            return true;
156
        }
157
158 1
        return false;
159
    }
160
161
    /**
162
     * Returns the char value at the specified index. An index ranges from 0 to length() - 1.
163
     * The first char value of the sequence is at index 0, the next at index 1, and so on,
164
     * as for array indexing. If the char value specified by the index is a surrogate,
165
     * the surrogate value is returned.
166
     *
167
     * @param int $index
168
     * @throws IndexOutOfBoundsException
169
     * @return string
170
     */
171 1
    public function charAt($index)
172
    {
173 1
        if ((int) $index < 0 || (int) $index > $this->length()) {
174 1
            throw new IndexOutOfBoundsException(
175 1
                'The index argument is negative or not less than the length of this string.'
176
            );
177
        }
178
179 1
        return mb_substr($this->value, (int) $index, 1);
180
    }
181
182
    /**
183
     * Returns the index within this string of the first occurrence of the specified character.
184
     * If the argument fromIndex is specified the search will start at the index that was informed
185
     *
186
     * @param string $char
187
     * @param int $fromIndex
188
     * @return int positive with the position of desired part of the string, -1 if the string not found
189
     */
190 1
    public function indexOf($char, $fromIndex = 0)
191
    {
192 1
        $position = mb_strpos($this->value, $char, (int) $fromIndex);
193 1
        if (false === $position) {
194
            return -1;
195
        }
196
197 1
        return $position;
198
    }
199
200
    /**
201
     * Returns the index within this string of the last occurrence of the specified character.
202
     * If you set the fromIndex argument the method will searching backward starting at the specified index.
203
     *
204
     * @param string $char
205
     * @param int $fromIndex
206
     * @return int
207
     */
208 1
    public function lastIndexOf($char, $fromIndex = -1)
209
    {
210 1
        if ($fromIndex > 0) {
211 1
            $valueForSearch = $this->substring(0, $fromIndex);
212 1
            return ($position = mb_strripos($valueForSearch, $char)) === false ? -1 : $position;
213
        }
214
215 1
        return ($position = mb_strripos($this->value, $char, (int) $fromIndex)) === false ? -1 : $position;
216
    }
217
218
    /**
219
     * Returns true if, and only if, length() is 0.
220
     *
221
     * @return bool
222
     */
223 1
    public function isEmpty()
224
    {
225 1
        return 0 === $this->length();
226
    }
227
228
    /**
229
     * Splits this string around matches of the given regular expression.
230
     *
231
     * @param string $char
232
     * @return array
233
     */
234 1
    public function split($char)
235
    {
236 1
        return mb_split($char, $this->value);
237
    }
238
239
    /**
240
     * Converts all of the characters in this JString to lower case using the rules
241
     * of the default locale.
242
     *
243
     * @return string
244
     */
245 1
    public function toLowerCase()
246
    {
247 1
        return mb_strtolower($this->value);
248
    }
249
250
    /**
251
     * Converts all of the characters in this JString to upper case using the rules
252
     * of the default locale.
253
     *
254
     * @return string
255
     */
256 1
    public function toUpperCase()
257
    {
258 1
        return mb_strtoupper($this->value);
259
    }
260
261
    /**
262
     * Returns a copy of the string, with leading and trailing whitespace omitted.
263
     *
264
     * @return string
265
     */
266 1
    public function trim()
267
    {
268 1
        return trim($this->value);
269
    }
270
271
    /**
272
     * Returns a new string that is a substring of this string. The substring begins at
273
     * the specified beginIndex and extends to the character at index endIndex - 1.
274
     * Thus the length of the substring is endIndex-beginIndex.
275
     *
276
     * @param int $beginIndex
277
     * @param int $endIndex
278
     * @return string
279
     */
280 1
    public function substring($beginIndex, $endIndex = -1)
281
    {
282 1
        if ($endIndex < 0) {
283 1
            $endIndex = $this->length();
284
        }
285
286 1
        return mb_substr($this->value, $beginIndex, ($endIndex - $beginIndex) + 1);
287
    }
288
289
    /**
290
     * Returns a new string resulting from replacing all occurrences of
291
     * oldChar in this string with newChar.
292
     *
293
     * @param string $oldChar
294
     * @param string $newChar
295
     * @return string
296
     */
297 1
    public function replace($oldChar, $newChar)
298
    {
299 1
        return str_replace($oldChar, $newChar, $this->value);
300
    }
301
302
    /**
303
     * The string value (which is already a string!) is itself returned.
304
     *
305
     * @return string
306
     */
307 1
    public function __toString()
308
    {
309 1
        return $this->value;
310
    }
311
}
312