Debian Codegraph @

lib/3rdParty/composer/spomky-labs/base64url/src/Base64Url.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * The MIT License (MIT)
7 *
8 * Copyright (c) 2014-2020 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 use function base64_decode;
17 use function base64_encode;
18 use InvalidArgumentException;
19 use function rtrim;
20 use function strtr;
21
22 /**
23 * Encode and decode data into Base64 Url Safe.
24 */
25 final class Base64Url
26 {
27 /**
28 * @param string $data The data to encode
29 * @param bool $usePadding If true, the "=" padding at end of the encoded value are kept, else it is removed
30 *
31 * @return string The data encoded
32 */
33 public static function encode(string $data, bool $usePadding = false): string
34 {
35 $encoded = strtr(base64_encode($data), '+/', '-_');
36
37 return true === $usePadding ? $encoded : rtrim($encoded, '=');
38 }
39
40 /**
41 * @param string $data The data to decode
42 *
43 * @throws InvalidArgumentException
44 *
45 * @return string The data decoded
46 */
47 public static function decode(string $data): string
48 {
49 $decoded = base64_decode(strtr($data, '-_', '+/'), true);
50 if (false === $decoded) {
51 throw new InvalidArgumentException('Invalid data provided');
52 }
53
54 return $decoded;
55 }
56 }