| 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 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid\Codec; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Builder\UuidBuilderInterface; |
| 16 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidArgumentException; |
| 17 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidUuidStringException; |
| 18 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\FieldsInterface; |
| 19 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Uuid; |
| 20 |
use Dudlewebs\WPMCS\Ramsey\Uuid\UuidInterface; |
| 21 |
use function hex2bin; |
| 22 |
use function implode; |
| 23 |
use function str_replace; |
| 24 |
use function strlen; |
| 25 |
use function substr; |
| 26 |
/** |
| 27 |
* StringCodec encodes and decodes RFC 4122 UUIDs |
| 28 |
* |
| 29 |
* @link http://tools.ietf.org/html/rfc4122 |
| 30 |
* |
| 31 |
* @psalm-immutable |
| 32 |
*/ |
| 33 |
class StringCodec implements CodecInterface |
| 34 |
{ |
| 35 |
/** |
| 36 |
* @var UuidBuilderInterface |
| 37 |
*/ |
| 38 |
private $builder; |
| 39 |
/** |
| 40 |
* Constructs a StringCodec |
| 41 |
* |
| 42 |
* @param UuidBuilderInterface $builder The builder to use when encoding UUIDs |
| 43 |
*/ |
| 44 |
public function __construct(UuidBuilderInterface $builder) |
| 45 |
{ |
| 46 |
$this->builder = $builder; |
| 47 |
} |
| 48 |
public function encode(UuidInterface $uuid): string |
| 49 |
{ |
| 50 |
/** @var FieldsInterface $fields */ |
| 51 |
$fields = $uuid->getFields(); |
| 52 |
return $fields->getTimeLow()->toString() . '-' . $fields->getTimeMid()->toString() . '-' . $fields->getTimeHiAndVersion()->toString() . '-' . $fields->getClockSeqHiAndReserved()->toString() . $fields->getClockSeqLow()->toString() . '-' . $fields->getNode()->toString(); |
| 53 |
} |
| 54 |
/** |
| 55 |
* @psalm-return non-empty-string |
| 56 |
* @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty |
| 57 |
* @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty |
| 58 |
*/ |
| 59 |
public function encodeBinary(UuidInterface $uuid): string |
| 60 |
{ |
| 61 |
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */ |
| 62 |
return $uuid->getFields()->getBytes(); |
| 63 |
} |
| 64 |
/** |
| 65 |
* @throws InvalidUuidStringException |
| 66 |
* |
| 67 |
* @inheritDoc |
| 68 |
*/ |
| 69 |
public function decode(string $encodedUuid): UuidInterface |
| 70 |
{ |
| 71 |
return $this->builder->build($this, $this->getBytes($encodedUuid)); |
| 72 |
} |
| 73 |
public function decodeBytes(string $bytes): UuidInterface |
| 74 |
{ |
| 75 |
if (strlen($bytes) !== 16) { |
| 76 |
throw new InvalidArgumentException('$bytes string should contain 16 characters.'); |
| 77 |
} |
| 78 |
return $this->builder->build($this, $bytes); |
| 79 |
} |
| 80 |
/** |
| 81 |
* Returns the UUID builder |
| 82 |
*/ |
| 83 |
protected function getBuilder(): UuidBuilderInterface |
| 84 |
{ |
| 85 |
return $this->builder; |
| 86 |
} |
| 87 |
/** |
| 88 |
* Returns a byte string of the UUID |
| 89 |
*/ |
| 90 |
protected function getBytes(string $encodedUuid): string |
| 91 |
{ |
| 92 |
$parsedUuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'], '', $encodedUuid); |
| 93 |
$components = [substr($parsedUuid, 0, 8), substr($parsedUuid, 8, 4), substr($parsedUuid, 12, 4), substr($parsedUuid, 16, 4), substr($parsedUuid, 20)]; |
| 94 |
if (!Uuid::isValid(implode('-', $components))) { |
| 95 |
throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid); |
| 96 |
} |
| 97 |
return (string) hex2bin($parsedUuid); |
| 98 |
} |
| 99 |
} |
| 100 |
|