Debian Codegraph @

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