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

Base64::JSONUrlSafeEncode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 2
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
}