Debian Codegraph @

lib/3rdParty/composer/spomky-labs/cbor-php/src/Utils.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace CBOR;
6
7 use function assert;
8 use Brick\Math\BigInteger;
9 use InvalidArgumentException;
10 use function is_string;
11
12 /**
13 * @internal
14 */
15 abstract class Utils
16 {
17 public static function binToInt(string $value): int
18 {
19 return self::binToBigInteger($value)->toInt();
20 }
21
22 public static function binToBigInteger(string $value): BigInteger
23 {
24 return self::hexToBigInteger(bin2hex($value));
25 }
26
27 public static function hexToInt(string $value): int
28 {
29 return self::hexToBigInteger($value)->toInt();
30 }
31
32 public static function hexToBigInteger(string $value): BigInteger
33 {
34 assert($value !== '', 'Value must not be empty');
35
36 return BigInteger::fromBase($value, 16);
37 }
38
39 public static function hexToString(string $value): string
40 {
41 $hex = bin2hex($value);
42 assert($hex !== '', 'Value must not be empty');
43
44 return BigInteger::fromBase($hex, 16)->toBase(10);
45 }
46
47 public static function decode(string $data): string
48 {
49 $decoded = base64_decode(strtr($data, '-_', '+/'), true);
50 if ($decoded === false) {
51 throw new InvalidArgumentException('Invalid data provided');
52 }
53
54 return $decoded;
55 }
56
57 /**
58 * @param mixed|null $data
59 */
60 public static function assertString($data, ?string $message = null): void
61 {
62 if (! is_string($data)) {
63 throw new InvalidArgumentException($message ?? '');
64 }
65 }
66 }