Debian Codegraph @

vendor/ramsey/uuid/src/Converter/Number/BigNumberConverter.php

raw
1 <?php
2 /**
3 * This file is part of the ramsey/uuid library
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9 * @license http://opensource.org/licenses/MIT MIT
10 * @link https://benramsey.com/projects/ramsey-uuid/ Documentation
11 * @link https://packagist.org/packages/ramsey/uuid Packagist
12 * @link https://github.com/ramsey/uuid GitHub
13 */
14
15 namespace Ramsey\Uuid\Converter\Number;
16
17 use Moontoast\Math\BigNumber;
18 use Ramsey\Uuid\Converter\NumberConverterInterface;
19
20 /**
21 * BigNumberConverter converts UUIDs from hexadecimal characters into
22 * moontoast/math `BigNumber` representations of integers and vice versa
23 */
24 class BigNumberConverter implements NumberConverterInterface
25 {
26 /**
27 * Converts a hexadecimal number into a `Moontoast\Math\BigNumber` representation
28 *
29 * @param string $hex The hexadecimal string representation to convert
30 * @return BigNumber
31 */
32 public function fromHex($hex)
33 {
34 $number = BigNumber::convertToBase10($hex, 16);
35
36 return new BigNumber($number);
37 }
38
39 /**
40 * Converts an integer or `Moontoast\Math\BigNumber` integer representation
41 * into a hexadecimal string representation
42 *
43 * @param int|string|BigNumber $integer An integer or `Moontoast\Math\BigNumber`
44 * @return string Hexadecimal string
45 */
46 public function toHex($integer)
47 {
48 if (!$integer instanceof BigNumber) {
49 $integer = new BigNumber($integer);
50 }
51
52 return BigNumber::convertFromBase10($integer, 16);
53 }
54 }