Base64Url   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encode() 0 6 2
A decode() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Base64Url;
15
16
/**
17
 * Encode and decode data into Base64 Url Safe.
18
 */
19
final class Base64Url
20
{
21
    /**
22
     * @param string $data       The data to encode
23
     * @param bool   $usePadding If true, the "=" padding at end of the encoded value are kept, else it is removed
24
     *
25
     * @return string The data encoded
26
     */
27
    public static function encode(string $data, bool $usePadding = false): string
28
    {
29
        $encoded = \strtr(\base64_encode($data), '+/', '-_');
30
31
        return true === $usePadding ? $encoded : \rtrim($encoded, '=');
32
    }
33
34
    /**
35
     * @param string $data The data to decode
36
     *
37
     * @return string The data decoded
38
     */
39
    public static function decode(string $data): string
40
    {
41
        return \base64_decode(\strtr($data, '-_', '+/'), true);
42
    }
43
}
44