| 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\Codec; |
| 16 |
|
| 17 |
use Ramsey\Uuid\Builder\UuidBuilderInterface; |
| 18 |
use Ramsey\Uuid\Exception\InvalidArgumentException; |
| 19 |
use Ramsey\Uuid\Exception\InvalidUuidStringException; |
| 20 |
use Ramsey\Uuid\Uuid; |
| 21 |
use Ramsey\Uuid\UuidInterface; |
| 22 |
|
| 23 |
use function bin2hex; |
| 24 |
use function hex2bin; |
| 25 |
use function implode; |
| 26 |
use function sprintf; |
| 27 |
use function str_replace; |
| 28 |
use function strlen; |
| 29 |
use function substr; |
| 30 |
|
| 31 |
/** |
| 32 |
* StringCodec encodes and decodes RFC 9562 (formerly RFC 4122) UUIDs |
| 33 |
* |
| 34 |
* @immutable |
| 35 |
*/ |
| 36 |
class StringCodec implements CodecInterface |
| 37 |
{ |
| 38 |
/** |
| 39 |
* Constructs a StringCodec |
| 40 |
* |
| 41 |
* @param UuidBuilderInterface $builder The builder to use when encoding UUIDs |
| 42 |
*/ |
| 43 |
public function __construct(private UuidBuilderInterface $builder) |
| 44 |
{ |
| 45 |
} |
| 46 |
|
| 47 |
public function encode(UuidInterface $uuid): string |
| 48 |
{ |
| 49 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 50 |
$hex = bin2hex($uuid->getFields()->getBytes()); |
| 51 |
|
| 52 |
/** @var non-empty-string */ |
| 53 |
return sprintf( |
| 54 |
'%08s-%04s-%04s-%04s-%012s', |
| 55 |
substr($hex, 0, 8), |
| 56 |
substr($hex, 8, 4), |
| 57 |
substr($hex, 12, 4), |
| 58 |
substr($hex, 16, 4), |
| 59 |
substr($hex, 20), |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* @return non-empty-string |
| 65 |
*/ |
| 66 |
public function encodeBinary(UuidInterface $uuid): string |
| 67 |
{ |
| 68 |
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */ |
| 69 |
return $uuid->getFields()->getBytes(); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @throws InvalidUuidStringException |
| 74 |
* |
| 75 |
* @inheritDoc |
| 76 |
*/ |
| 77 |
public function decode(string $encodedUuid): UuidInterface |
| 78 |
{ |
| 79 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 80 |
return $this->builder->build($this, $this->getBytes($encodedUuid)); |
| 81 |
} |
| 82 |
|
| 83 |
public function decodeBytes(string $bytes): UuidInterface |
| 84 |
{ |
| 85 |
if (strlen($bytes) !== 16) { |
| 86 |
throw new InvalidArgumentException('$bytes string should contain 16 characters.'); |
| 87 |
} |
| 88 |
|
| 89 |
return $this->builder->build($this, $bytes); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the UUID builder |
| 94 |
*/ |
| 95 |
protected function getBuilder(): UuidBuilderInterface |
| 96 |
{ |
| 97 |
return $this->builder; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Returns a byte string of the UUID |
| 102 |
*/ |
| 103 |
protected function getBytes(string $encodedUuid): string |
| 104 |
{ |
| 105 |
$parsedUuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}', '-'], '', $encodedUuid); |
| 106 |
|
| 107 |
$components = [ |
| 108 |
substr($parsedUuid, 0, 8), |
| 109 |
substr($parsedUuid, 8, 4), |
| 110 |
substr($parsedUuid, 12, 4), |
| 111 |
substr($parsedUuid, 16, 4), |
| 112 |
substr($parsedUuid, 20), |
| 113 |
]; |
| 114 |
|
| 115 |
if (!Uuid::isValid(implode('-', $components))) { |
| 116 |
throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid); |
| 117 |
} |
| 118 |
|
| 119 |
return (string) hex2bin($parsedUuid); |
| 120 |
} |
| 121 |
} |
| 122 |
|