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\Guid;
16
 
17
use Ramsey\Uuid\Builder\UuidBuilderInterface;
18
use Ramsey\Uuid\Codec\CodecInterface;
19
use Ramsey\Uuid\Converter\NumberConverterInterface;
20
use Ramsey\Uuid\Converter\TimeConverterInterface;
21
use Ramsey\Uuid\Exception\UnableToBuildUuidException;
22
use Ramsey\Uuid\UuidInterface;
23
use Throwable;
24
 
25
/**
26
 * GuidBuilder builds instances of Guid
27
 *
28
 * @see Guid
29
 *
30
 * @psalm-immutable
31
 */
32
class GuidBuilder implements UuidBuilderInterface
33
{
34
    /**
35
     * @param NumberConverterInterface $numberConverter The number converter to
36
     *     use when constructing the Guid
37
     * @param TimeConverterInterface $timeConverter The time converter to use
38
     *     for converting timestamps extracted from a UUID to Unix timestamps
39
     */
40
    public function __construct(
41
        private NumberConverterInterface $numberConverter,
42
        private TimeConverterInterface $timeConverter
43
    ) {
44
    }
45
 
46
    /**
47
     * Builds and returns a Guid
48
     *
49
     * @param CodecInterface $codec The codec to use for building this Guid instance
50
     * @param string $bytes The byte string from which to construct a UUID
51
     *
52
     * @return Guid The GuidBuilder returns an instance of Ramsey\Uuid\Guid\Guid
53
     *
54
     * @psalm-pure
55
     */
56
    public function build(CodecInterface $codec, string $bytes): UuidInterface
57
    {
58
        try {
59
            return new Guid(
60
                $this->buildFields($bytes),
61
                $this->numberConverter,
62
                $codec,
63
                $this->timeConverter
64
            );
65
        } catch (Throwable $e) {
66
            throw new UnableToBuildUuidException($e->getMessage(), (int) $e->getCode(), $e);
67
        }
68
    }
69
 
70
    /**
71
     * Proxy method to allow injecting a mock, for testing
72
     */
73
    protected function buildFields(string $bytes): Fields
74
    {
75
        return new Fields($bytes);
76
    }
77
}