RobDWaller / ReallySimpleJWT
1
<?php
2

3
namespace ReallySimpleJWT\Helper;
4

5
trait Base64
6
{
7
    /**
8
     * Convert a base64 string to a base64 Url string.
9
     */
10 16
    public function toBase64Url(string $base64): string
11
    {
12 16
        return str_replace(['+', '/', '='], ['-', '_', ''], $base64);
13
    }
14

15
    /**
16
     * Convert a base64 URL string to a base64 string.
17
     */
18 16
    public function toBase64(string $urlString): string
19
    {
20 16
        return str_replace(['-', '_'], ['+', '/'], $urlString);
21
    }
22

23
    /**
24
     * Add padding to base64 strings which require it. Some base64 URL strings
25
     * which are decoded will have missing padding which is represented by the
26
     * equals sign.
27
     */
28 16
    public function addPadding(string $base64String): string
29
    {
30 16
        if (strlen($base64String) % 4 !== 0) {
31 16
            return $this->addPadding($base64String . '=');
32
        }
33

34 16
        return $base64String;
35
    }
36
}

Read our documentation on viewing source code .

Loading