googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
uuid
/
src
/
Nonstandard
/
UuidBuilder.php
googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
uuid
/
src
/
Nonstandard
Last commit date
Fields.php
3 years ago
Uuid.php
3 years ago
UuidBuilder.php
3 years ago
UuidV6.php
3 years ago
UuidBuilder.php
89 lines
| 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 | * @var NumberConverterInterface |
| 34 | */ |
| 35 | private $numberConverter; |
| 36 | |
| 37 | /** |
| 38 | * @var TimeConverterInterface |
| 39 | */ |
| 40 | private $timeConverter; |
| 41 | |
| 42 | /** |
| 43 | * @param NumberConverterInterface $numberConverter The number converter to |
| 44 | * use when constructing the Nonstandard\Uuid |
| 45 | * @param TimeConverterInterface $timeConverter The time converter to use |
| 46 | * for converting timestamps extracted from a UUID to Unix timestamps |
| 47 | */ |
| 48 | public function __construct( |
| 49 | NumberConverterInterface $numberConverter, |
| 50 | TimeConverterInterface $timeConverter |
| 51 | ) { |
| 52 | $this->numberConverter = $numberConverter; |
| 53 | $this->timeConverter = $timeConverter; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Builds and returns a Nonstandard\Uuid |
| 58 | * |
| 59 | * @param CodecInterface $codec The codec to use for building this instance |
| 60 | * @param string $bytes The byte string from which to construct a UUID |
| 61 | * |
| 62 | * @return Uuid The Nonstandard\UuidBuilder returns an instance of |
| 63 | * Nonstandard\Uuid |
| 64 | * |
| 65 | * @psalm-pure |
| 66 | */ |
| 67 | public function build(CodecInterface $codec, string $bytes): UuidInterface |
| 68 | { |
| 69 | try { |
| 70 | return new Uuid( |
| 71 | $this->buildFields($bytes), |
| 72 | $this->numberConverter, |
| 73 | $codec, |
| 74 | $this->timeConverter |
| 75 | ); |
| 76 | } catch (Throwable $e) { |
| 77 | throw new UnableToBuildUuidException($e->getMessage(), (int) $e->getCode(), $e); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Proxy method to allow injecting a mock, for testing |
| 83 | */ |
| 84 | protected function buildFields(string $bytes): Fields |
| 85 | { |
| 86 | return new Fields($bytes); |
| 87 | } |
| 88 | } |
| 89 |