Passed
Push — master ( 894585...1a72b2 )
by Fabian
02:50
created

Base64   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 10
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A UrlSafeEncode() 0 2 1
A UrlSafeDecode() 0 8 2
A JSONUrlSafeEncode() 0 2 1
1
<?php
2
3
namespace LE_ACME2\Utilities;
4
5
class Base64 {
6
7
    /**
8
     * Encodes a string input to a base64 encoded string which is URL safe.
9
     *
10
     * @param string	$input 	The input string to encode.
11
     * @return string	Returns a URL safe base64 encoded string.
12
     */
13
    public static function UrlSafeEncode(string $input) : string {
14
        return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
15
    }
16
17
    /**
18
     * Decodes a string that is URL safe base64 encoded.
19
     *
20
     * @param string	$input	The encoded input string to decode.
21
     * @return string	Returns the decoded input string.
22
     */
23
    public static function UrlSafeDecode(string $input) : string {
24
25
        $remainder = strlen($input) % 4;
26
        if ($remainder) {
27
            $padlen = 4 - $remainder;
28
            $input .= str_repeat('=', $padlen);
29
        }
30
        return base64_decode(strtr($input, '-_', '+/'));
31
    }
32
33
    public static function JSONUrlSafeEncode(array $input) : string {
34
        return self::UrlSafeEncode(json_encode($input, JSON_UNESCAPED_SLASHES));
35
    }
36
}