Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * This file is part of the ramsey/uuid library
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
10
 * @license http://opensource.org/licenses/MIT MIT
11
 */
12
 
13
declare(strict_types=1);
14
 
15
namespace Ramsey\Uuid\Nonstandard;
16
 
17
use Ramsey\Uuid\Codec\CodecInterface;
18
use Ramsey\Uuid\Converter\NumberConverterInterface;
19
use Ramsey\Uuid\Converter\TimeConverterInterface;
20
use Ramsey\Uuid\Exception\InvalidArgumentException;
21
use Ramsey\Uuid\Lazy\LazyUuidFromString;
22
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
23
use Ramsey\Uuid\Rfc4122\TimeTrait;
24
use Ramsey\Uuid\Rfc4122\UuidInterface;
25
use Ramsey\Uuid\Rfc4122\UuidV1;
1464 lars 26
use Ramsey\Uuid\Uuid as BaseUuid;
148 lars 27
 
28
/**
29
 * Reordered time, or version 6, UUIDs include timestamp, clock sequence, and
30
 * node values that are combined into a 128-bit unsigned integer
31
 *
32
 * @deprecated Use {@see \Ramsey\Uuid\Rfc4122\UuidV6} instead.
33
 *
34
 * @link https://github.com/uuid6/uuid6-ietf-draft UUID version 6 IETF draft
35
 * @link http://gh.peabody.io/uuidv6/ "Version 6" UUIDs
36
 *
37
 * @psalm-immutable
38
 */
1464 lars 39
class UuidV6 extends BaseUuid implements UuidInterface
148 lars 40
{
41
    use TimeTrait;
42
 
43
    /**
44
     * Creates a version 6 (reordered time) UUID
45
     *
46
     * @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID
47
     * @param NumberConverterInterface $numberConverter The number converter to use
48
     *     for converting hex values to/from integers
49
     * @param CodecInterface $codec The codec to use when encoding or decoding
50
     *     UUID strings
51
     * @param TimeConverterInterface $timeConverter The time converter to use
52
     *     for converting timestamps extracted from a UUID to unix timestamps
53
     */
54
    public function __construct(
55
        Rfc4122FieldsInterface $fields,
56
        NumberConverterInterface $numberConverter,
57
        CodecInterface $codec,
58
        TimeConverterInterface $timeConverter
59
    ) {
60
        if ($fields->getVersion() !== Uuid::UUID_TYPE_REORDERED_TIME) {
61
            throw new InvalidArgumentException(
62
                'Fields used to create a UuidV6 must represent a '
63
                . 'version 6 (reordered time) UUID'
64
            );
65
        }
66
 
67
        parent::__construct($fields, $numberConverter, $codec, $timeConverter);
68
    }
69
 
70
    /**
71
     * Converts this UUID into an instance of a version 1 UUID
72
     */
73
    public function toUuidV1(): UuidV1
74
    {
75
        $hex = $this->getHex()->toString();
76
        $hex = substr($hex, 7, 5)
77
            . substr($hex, 13, 3)
78
            . substr($hex, 3, 4)
79
            . '1' . substr($hex, 0, 3)
80
            . substr($hex, 16);
81
 
82
        /** @var LazyUuidFromString $uuid */
83
        $uuid = Uuid::fromBytes((string) hex2bin($hex));
84
 
85
        return $uuid->toUuidV1();
86
    }
87
 
88
    /**
89
     * Converts a version 1 UUID into an instance of a version 6 UUID
90
     */
91
    public static function fromUuidV1(UuidV1 $uuidV1): \Ramsey\Uuid\Rfc4122\UuidV6
92
    {
93
        $hex = $uuidV1->getHex()->toString();
94
        $hex = substr($hex, 13, 3)
95
            . substr($hex, 8, 4)
96
            . substr($hex, 0, 5)
97
            . '6' . substr($hex, 5, 3)
98
            . substr($hex, 16);
99
 
100
        /** @var LazyUuidFromString $uuid */
101
        $uuid = Uuid::fromBytes((string) hex2bin($hex));
102
 
103
        return $uuid->toUuidV6();
104
    }
105
}