Debian Codegraph @

vendor/ramsey/uuid/src/Guid/GuidBuilder.php

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