Test Failed
Push — master ( da5155...bf86b6 )
by
unknown
03:42
created

StringHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 53
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A byteSubstr() 0 3 2
A base64UrlDecode() 0 3 1
A byteLength() 0 3 1
A base64UrlEncode() 0 3 1
1
<?php
2
3
namespace WebComplete\core\utils\helpers;
4
5
class StringHelper
6
{
7
    /**
8
     * Encodes string into "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648)
9
     *
10
     * > Note: Base 64 padding `=` may be at the end of the returned string.
11
     * > `=` is not transparent to URL encoding.
12
     *
13
     * @see https://tools.ietf.org/html/rfc4648#page-7
14
     * @param string $input the string to encode.
15
     * @return string encoded string.
16
     */
17
    public function base64UrlEncode($input): string
18
    {
19
        return \strtr(\base64_encode($input), '+/', '-_');
20
    }
21
22
    /**
23
     * Decodes "Base 64 Encoding with URL and Filename Safe Alphabet" (RFC 4648)
24
     *
25
     * @see https://tools.ietf.org/html/rfc4648#page-7
26
     * @param string $input encoded string.
27
     * @return string decoded string.
28
     */
29
    public function base64UrlDecode($input): string
30
    {
31
        return \base64_decode(\strtr($input, '-_', '+/'));
32
    }
33
34
    /**
35
     * Returns the number of bytes in the given string.
36
     * This method ensures the string is treated as a byte array by using `mb_strlen()`.
37
     * @param string $string the string being measured for length
38
     * @return int the number of bytes in the given string.
39
     */
40
    public function byteLength($string)
41
    {
42
        return \mb_strlen($string, '8bit');
43
    }
44
45
    /**
46
     * Returns the portion of string specified by the start and length parameters.
47
     * This method ensures the string is treated as a byte array by using `mb_substr()`.
48
     * @param string $string the input string. Must be one character or longer.
49
     * @param int $start the starting position
50
     * @param int $length the desired portion length. If not specified or `null`, there will be
51
     * no limit on length i.e. the output will be until the end of the string.
52
     * @return string the extracted part of string, or FALSE on failure or an empty string.
53
     * @see http://www.php.net/manual/en/function.substr.php
54
     */
55
    public function byteSubstr($string, $start, $length = null)
56
    {
57
        return \mb_substr($string, $start, $length === null ? \mb_strlen($string, '8bit') : $length, '8bit');
58
    }
59
}
60