SafeBase64Parser::decode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MiladRahimi\Jwt\Base64;
6
7
class SafeBase64Parser implements Base64Parser
8
{
9
    /**
10
     * @inheritdoc
11
     */
12
    public function encode(string $data): string
13
    {
14
        return str_replace('=', '', strtr(base64_encode($data), '+/', '-_'));
15
    }
16
17
    /**
18
     * @inheritdoc
19
     */
20
    public function decode(string $data): string
21
    {
22
        if ($remainder = strlen($data) % 4) {
23
            $paddingLength = 4 - $remainder;
24
            $data .= str_repeat('=', $paddingLength);
25
        }
26
27
        return base64_decode(strtr($data, '-_', '+/'));
28
    }
29
}
30