Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | 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\Rfc4122;
16
 
17
use Ramsey\Uuid\Builder\UuidBuilderInterface;
18
use Ramsey\Uuid\Codec\CodecInterface;
19
use Ramsey\Uuid\Converter\NumberConverterInterface;
20
use Ramsey\Uuid\Converter\Time\UnixTimeConverter;
21
use Ramsey\Uuid\Converter\TimeConverterInterface;
22
use Ramsey\Uuid\Exception\UnableToBuildUuidException;
23
use Ramsey\Uuid\Exception\UnsupportedOperationException;
24
use Ramsey\Uuid\Math\BrickMathCalculator;
25
use Ramsey\Uuid\Rfc4122\UuidInterface as Rfc4122UuidInterface;
26
use Ramsey\Uuid\Uuid;
27
use Ramsey\Uuid\UuidInterface;
28
use Throwable;
29
 
30
/**
31
 * UuidBuilder builds instances of RFC 4122 UUIDs
32
 *
33
 * @psalm-immutable
34
 */
35
class UuidBuilder implements UuidBuilderInterface
36
{
37
    private TimeConverterInterface $unixTimeConverter;
38
 
39
    /**
40
     * Constructs the DefaultUuidBuilder
41
     *
42
     * @param NumberConverterInterface $numberConverter The number converter to
43
     *     use when constructing the Uuid
44
     * @param TimeConverterInterface $timeConverter The time converter to use
45
     *     for converting Gregorian time extracted from version 1, 2, and 6
46
     *     UUIDs to Unix timestamps
47
     * @param TimeConverterInterface|null $unixTimeConverter The time converter
48
     *     to use for converter Unix Epoch time extracted from version 7 UUIDs
49
     *     to Unix timestamps
50
     */
51
    public function __construct(
52
        private NumberConverterInterface $numberConverter,
53
        private TimeConverterInterface $timeConverter,
54
        ?TimeConverterInterface $unixTimeConverter = null
55
    ) {
56
        $this->unixTimeConverter = $unixTimeConverter ?? new UnixTimeConverter(new BrickMathCalculator());
57
    }
58
 
59
    /**
60
     * Builds and returns a Uuid
61
     *
62
     * @param CodecInterface $codec The codec to use for building this Uuid instance
63
     * @param string $bytes The byte string from which to construct a UUID
64
     *
65
     * @return Rfc4122UuidInterface UuidBuilder returns instances of Rfc4122UuidInterface
66
     *
67
     * @psalm-pure
68
     */
69
    public function build(CodecInterface $codec, string $bytes): UuidInterface
70
    {
71
        try {
72
            /** @var Fields $fields */
73
            $fields = $this->buildFields($bytes);
74
 
75
            if ($fields->isNil()) {
76
                return new NilUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
77
            }
78
 
79
            if ($fields->isMax()) {
80
                return new MaxUuid($fields, $this->numberConverter, $codec, $this->timeConverter);
81
            }
82
 
83
            switch ($fields->getVersion()) {
84
                case Uuid::UUID_TYPE_TIME:
85
                    return new UuidV1($fields, $this->numberConverter, $codec, $this->timeConverter);
86
                case Uuid::UUID_TYPE_DCE_SECURITY:
87
                    return new UuidV2($fields, $this->numberConverter, $codec, $this->timeConverter);
88
                case Uuid::UUID_TYPE_HASH_MD5:
89
                    return new UuidV3($fields, $this->numberConverter, $codec, $this->timeConverter);
90
                case Uuid::UUID_TYPE_RANDOM:
91
                    return new UuidV4($fields, $this->numberConverter, $codec, $this->timeConverter);
92
                case Uuid::UUID_TYPE_HASH_SHA1:
93
                    return new UuidV5($fields, $this->numberConverter, $codec, $this->timeConverter);
94
                case Uuid::UUID_TYPE_REORDERED_TIME:
95
                    return new UuidV6($fields, $this->numberConverter, $codec, $this->timeConverter);
96
                case Uuid::UUID_TYPE_UNIX_TIME:
97
                    return new UuidV7($fields, $this->numberConverter, $codec, $this->unixTimeConverter);
98
                case Uuid::UUID_TYPE_CUSTOM:
99
                    return new UuidV8($fields, $this->numberConverter, $codec, $this->timeConverter);
100
            }
101
 
102
            throw new UnsupportedOperationException(
103
                'The UUID version in the given fields is not supported '
104
                . 'by this UUID builder'
105
            );
106
        } catch (Throwable $e) {
107
            throw new UnableToBuildUuidException($e->getMessage(), (int) $e->getCode(), $e);
108
        }
109
    }
110
 
111
    /**
112
     * Proxy method to allow injecting a mock, for testing
113
     */
114
    protected function buildFields(string $bytes): FieldsInterface
115
    {
116
        return new Fields($bytes);
117
    }
118
}