-
-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support version 8, custom UUIDs
- Loading branch information
Showing
22 changed files
with
332 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.. _reference.rfc4122.uuidv8: | ||
|
||
=============== | ||
Rfc4122\\UuidV8 | ||
=============== | ||
|
||
.. php:namespace:: Ramsey\Uuid\Rfc4122 | ||
.. php:class:: UuidV8 | ||
Implements :php:interface:`Ramsey\\Uuid\\Rfc4122\\UuidInterface`. | ||
|
||
UuidV8 represents a :ref:`version 8, custom UUID <rfc4122.version8>`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
.. _rfc4122.version8: | ||
|
||
================= | ||
Version 8: Custom | ||
================= | ||
|
||
.. note:: | ||
|
||
Version 8, custom UUIDs are a new format of UUID, proposed in an | ||
`Internet-Draft under review`_ at the IETF. While the draft is still going | ||
through the IETF process, the version 7 format is not expected to change | ||
in any way that breaks compatibility. | ||
|
||
Version 8 UUIDs allow applications to create custom UUIDs in an RFC-compatible | ||
way. The only requirement is the version and variant bits must be set according | ||
to the UUID specification. The bytes provided may contain any value according to | ||
your application's needs. Be aware, however, that other applications may not | ||
understand the semantics of the value. | ||
|
||
.. warning:: | ||
|
||
The bytes should be a 16-byte octet string, an open blob of data that you | ||
may fill with 128 bits of information. However, bits 48 through 51 will be | ||
replaced with the UUID version field, and bits 64 and 65 will be replaced | ||
with the UUID variant. You must not rely on these bits for your application | ||
needs. | ||
|
||
.. code-block:: php | ||
:caption: Generate a version 8, custom UUID | ||
:name: rfc4122.version8.example | ||
use Ramsey\Uuid\Uuid; | ||
$uuid = Uuid::uuid8("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff"); | ||
printf( | ||
"UUID: %s\nVersion: %d\n", | ||
$uuid->toString(), | ||
$uuid->getFields()->getVersion() | ||
); | ||
This will generate a version 8 UUID and print out its string representation. | ||
It will look something like this: | ||
|
||
.. code-block:: text | ||
UUID: 00112233-4455-8677-8899-aabbccddeeff | ||
Version: 8 | ||
.. _Internet-Draft under review: https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-00#section-5.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the ramsey/uuid library | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com> | ||
* @license http://opensource.org/licenses/MIT MIT | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ramsey\Uuid\Rfc4122; | ||
|
||
use Ramsey\Uuid\Codec\CodecInterface; | ||
use Ramsey\Uuid\Converter\NumberConverterInterface; | ||
use Ramsey\Uuid\Converter\TimeConverterInterface; | ||
use Ramsey\Uuid\Exception\InvalidArgumentException; | ||
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; | ||
use Ramsey\Uuid\Uuid; | ||
|
||
/** | ||
* Version 8, Custom UUIDs provide an RFC 4122 compatible format for | ||
* experimental or vendor-specific uses | ||
* | ||
* The only requirement for version 8 UUIDs is that the version and variant bits | ||
* must be set. Otherwise, implementations are free to set the other bits | ||
* according to their needs. As a result, the uniqueness of version 8 UUIDs is | ||
* implementation-specific and should not be assumed. | ||
* | ||
* @link https://datatracker.ietf.org/doc/html/draft-ietf-uuidrev-rfc4122bis-00#section-5.8 UUID Version 8 | ||
* | ||
* @psalm-immutable | ||
*/ | ||
final class UuidV8 extends Uuid implements UuidInterface | ||
{ | ||
/** | ||
* Creates a version 8 (custom) UUID | ||
* | ||
* @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID | ||
* @param NumberConverterInterface $numberConverter The number converter to use | ||
* for converting hex values to/from integers | ||
* @param CodecInterface $codec The codec to use when encoding or decoding | ||
* UUID strings | ||
* @param TimeConverterInterface $timeConverter The time converter to use | ||
* for converting timestamps extracted from a UUID to unix timestamps | ||
*/ | ||
public function __construct( | ||
Rfc4122FieldsInterface $fields, | ||
NumberConverterInterface $numberConverter, | ||
CodecInterface $codec, | ||
TimeConverterInterface $timeConverter | ||
) { | ||
if ($fields->getVersion() !== Uuid::UUID_TYPE_CUSTOM) { | ||
throw new InvalidArgumentException( | ||
'Fields used to create a UuidV8 must represent a ' | ||
. 'version 8 (custom) UUID' | ||
); | ||
} | ||
|
||
parent::__construct($fields, $numberConverter, $codec, $timeConverter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.