SafeBase64Parser::encode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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