Debian Codegraph @

vendor/ramsey/uuid/src/Converter/Time/BigNumberTimeConverter.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\Time;
16
17 use Moontoast\Math\BigNumber;
18 use Ramsey\Uuid\Converter\TimeConverterInterface;
19
20 /**
21 * BigNumberTimeConverter uses the moontoast/math library's `BigNumber` to
22 * provide facilities for converting parts of time into representations that may
23 * be used in UUIDs
24 */
25 class BigNumberTimeConverter implements TimeConverterInterface
26 {
27 /**
28 * Uses the provided seconds and micro-seconds to calculate the time_low,
29 * time_mid, and time_high fields used by RFC 4122 version 1 UUIDs
30 *
31 * @param string $seconds
32 * @param string $microSeconds
33 * @return string[] An array containing `low`, `mid`, and `high` keys
34 * @link http://tools.ietf.org/html/rfc4122#section-4.2.2
35 */
36 public function calculateTime($seconds, $microSeconds)
37 {
38 $uuidTime = new BigNumber('0');
39
40 $sec = new BigNumber($seconds);
41 $sec->multiply('10000000');
42
43 $usec = new BigNumber($microSeconds);
44 $usec->multiply('10');
45
46 $uuidTime
47 ->add($sec)
48 ->add($usec)
49 ->add('122192928000000000');
50
51 $uuidTimeHex = sprintf('%016s', $uuidTime->convertToBase(16));
52
53 return [
54 'low' => substr($uuidTimeHex, 8),
55 'mid' => substr($uuidTimeHex, 4, 4),
56 'hi' => substr($uuidTimeHex, 0, 4),
57 ];
58 }
59 }