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