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