| 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 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Builder\UuidBuilderInterface; |
| 16 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Codec\CodecInterface; |
| 17 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 18 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\TimeConverterInterface; |
| 19 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\UnableToBuildUuidException; |
| 20 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\UnsupportedOperationException; |
| 21 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Nonstandard\UuidV6; |
| 22 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\UuidInterface as Rfc4122UuidInterface; |
| 23 |
use Dudlewebs\WPMCS\Ramsey\Uuid\UuidInterface; |
| 24 |
use Throwable; |
| 25 |
/** |
| 26 |
* UuidBuilder builds instances of RFC 4122 UUIDs |
| 27 |
* |
| 28 |
* @psalm-immutable |
| 29 |
*/ |
| 30 |
class UuidBuilder implements UuidBuilderInterface |
| 31 |
{ |
| 32 |
/** |
| 33 |
* @var NumberConverterInterface |
| 34 |
*/ |
| 35 |
private $numberConverter; |
| 36 |
/** |
| 37 |
* @var TimeConverterInterface |
| 38 |
*/ |
| 39 |
private $timeConverter; |
| 40 |
/** |
| 41 |
* Constructs the DefaultUuidBuilder |
| 42 |
* |
| 43 |
* @param NumberConverterInterface $numberConverter The number converter to |
| 44 |
* use when constructing the 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(NumberConverterInterface $numberConverter, TimeConverterInterface $timeConverter) |
| 49 |
{ |
| 50 |
$this->numberConverter = $numberConverter; |
| 51 |
$this->timeConverter = $timeConverter; |
| 52 |
} |
| 53 |
/** |
| 54 |
* Builds and returns a Uuid |
| 55 |
* |
| 56 |
* @param CodecInterface $codec The codec to use for building this Uuid instance |
| 57 |
* @param string $bytes The byte string from which to construct a UUID |
| 58 |
* |
| 59 |
* @return Rfc4122UuidInterface UuidBuilder returns instances of Rfc4122UuidInterface |
| 60 |
* |
| 61 |
* @psalm-pure |
| 62 |
*/ |
| 63 |
public function build(CodecInterface $codec, string $bytes): UuidInterface |
| 64 |
{ |
| 65 |
try { |
| 66 |
$fields = $this->buildFields($bytes); |
| 67 |
if ($fields->isNil()) { |
| 68 |
return new NilUuid($fields, $this->numberConverter, $codec, $this->timeConverter); |
| 69 |
} |
| 70 |
switch ($fields->getVersion()) { |
| 71 |
case 1: |
| 72 |
return new UuidV1($fields, $this->numberConverter, $codec, $this->timeConverter); |
| 73 |
case 2: |
| 74 |
return new UuidV2($fields, $this->numberConverter, $codec, $this->timeConverter); |
| 75 |
case 3: |
| 76 |
return new UuidV3($fields, $this->numberConverter, $codec, $this->timeConverter); |
| 77 |
case 4: |
| 78 |
return new UuidV4($fields, $this->numberConverter, $codec, $this->timeConverter); |
| 79 |
case 5: |
| 80 |
return new UuidV5($fields, $this->numberConverter, $codec, $this->timeConverter); |
| 81 |
case 6: |
| 82 |
return new UuidV6($fields, $this->numberConverter, $codec, $this->timeConverter); |
| 83 |
} |
| 84 |
throw new UnsupportedOperationException('The UUID version in the given fields is not supported ' . 'by this UUID builder'); |
| 85 |
} catch (Throwable $e) { |
| 86 |
throw new UnableToBuildUuidException($e->getMessage(), (int) $e->getCode(), $e); |
| 87 |
} |
| 88 |
} |
| 89 |
/** |
| 90 |
* Proxy method to allow injecting a mock, for testing |
| 91 |
*/ |
| 92 |
protected function buildFields(string $bytes): FieldsInterface |
| 93 |
{ |
| 94 |
return new Fields($bytes); |
| 95 |
} |
| 96 |
} |
| 97 |
|