mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-14 15:56:48 +03:00
Validate fields passed to specific UUID version classes
This commit is contained in:
@@ -16,7 +16,12 @@ namespace Ramsey\Uuid\Rfc4122;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeInterface;
|
||||
use Ramsey\Uuid\Codec\CodecInterface;
|
||||
use Ramsey\Uuid\Converter\NumberConverterInterface;
|
||||
use Ramsey\Uuid\Converter\TimeConverterInterface;
|
||||
use Ramsey\Uuid\Exception\DateTimeException;
|
||||
use Ramsey\Uuid\Exception\InvalidArgumentException;
|
||||
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Throwable;
|
||||
|
||||
@@ -28,6 +33,33 @@ use Throwable;
|
||||
*/
|
||||
final class UuidV1 extends Uuid implements UuidInterface
|
||||
{
|
||||
/**
|
||||
* Creates a version 1 (time-based) 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_TIME) {
|
||||
throw new InvalidArgumentException(
|
||||
'Fields used to create a UuidV1 must represent a '
|
||||
. 'version 1 (time-based) UUID'
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a DateTimeInterface object representing the timestamp associated
|
||||
* with the UUID
|
||||
|
||||
@@ -14,6 +14,11 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -30,4 +35,30 @@ use Ramsey\Uuid\Uuid;
|
||||
*/
|
||||
final class UuidV2 extends Uuid implements UuidInterface
|
||||
{
|
||||
/**
|
||||
* Creates a version 2 (DCE Security) 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_IDENTIFIER) {
|
||||
throw new InvalidArgumentException(
|
||||
'Fields used to create a UuidV2 must represent a '
|
||||
. 'version 2 (DCE Security) UUID'
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,11 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -24,4 +29,30 @@ use Ramsey\Uuid\Uuid;
|
||||
*/
|
||||
final class UuidV3 extends Uuid implements UuidInterface
|
||||
{
|
||||
/**
|
||||
* Creates a version 3 (name-based, MD5-hashed) 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_HASH_MD5) {
|
||||
throw new InvalidArgumentException(
|
||||
'Fields used to create a UuidV3 must represent a '
|
||||
. 'version 3 (name-based, MD5-hashed) UUID'
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,11 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -24,4 +29,30 @@ use Ramsey\Uuid\Uuid;
|
||||
*/
|
||||
final class UuidV4 extends Uuid implements UuidInterface
|
||||
{
|
||||
/**
|
||||
* Creates a version 4 (random) 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_RANDOM) {
|
||||
throw new InvalidArgumentException(
|
||||
'Fields used to create a UuidV4 must represent a '
|
||||
. 'version 4 (random) UUID'
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,11 @@ 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;
|
||||
|
||||
/**
|
||||
@@ -24,4 +29,30 @@ use Ramsey\Uuid\Uuid;
|
||||
*/
|
||||
final class UuidV5 extends Uuid implements UuidInterface
|
||||
{
|
||||
/**
|
||||
* Creates a version 5 (name-based, SHA1-hashed) 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_HASH_SHA1) {
|
||||
throw new InvalidArgumentException(
|
||||
'Fields used to create a UuidV5 must represent a '
|
||||
. 'version 5 (named-based, SHA1-hashed) UUID'
|
||||
);
|
||||
}
|
||||
|
||||
parent::__construct($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Test\Rfc4122;
|
||||
|
||||
use Mockery;
|
||||
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;
|
||||
use Ramsey\Uuid\Rfc4122\UuidV1;
|
||||
use Ramsey\Uuid\Test\TestCase;
|
||||
|
||||
class UuidV1Test extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideTestVersions
|
||||
*/
|
||||
public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void
|
||||
{
|
||||
$fields = Mockery::mock(FieldsInterface::class, [
|
||||
'getVersion' => $version,
|
||||
]);
|
||||
|
||||
$numberConverter = Mockery::mock(NumberConverterInterface::class);
|
||||
$codec = Mockery::mock(CodecInterface::class);
|
||||
$timeConverter = Mockery::mock(TimeConverterInterface::class);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Fields used to create a UuidV1 must represent a '
|
||||
. 'version 1 (time-based) UUID'
|
||||
);
|
||||
|
||||
new UuidV1($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
|
||||
*/
|
||||
public function provideTestVersions(): array
|
||||
{
|
||||
return [
|
||||
['version' => 0],
|
||||
['version' => 2],
|
||||
['version' => 3],
|
||||
['version' => 4],
|
||||
['version' => 5],
|
||||
['version' => 6],
|
||||
['version' => 7],
|
||||
['version' => 8],
|
||||
['version' => 9],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Test\Rfc4122;
|
||||
|
||||
use Mockery;
|
||||
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;
|
||||
use Ramsey\Uuid\Rfc4122\UuidV2;
|
||||
use Ramsey\Uuid\Test\TestCase;
|
||||
|
||||
class UuidV2Test extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideTestVersions
|
||||
*/
|
||||
public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void
|
||||
{
|
||||
$fields = Mockery::mock(FieldsInterface::class, [
|
||||
'getVersion' => $version,
|
||||
]);
|
||||
|
||||
$numberConverter = Mockery::mock(NumberConverterInterface::class);
|
||||
$codec = Mockery::mock(CodecInterface::class);
|
||||
$timeConverter = Mockery::mock(TimeConverterInterface::class);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Fields used to create a UuidV2 must represent a '
|
||||
. 'version 2 (DCE Security) UUID'
|
||||
);
|
||||
|
||||
new UuidV2($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
|
||||
*/
|
||||
public function provideTestVersions(): array
|
||||
{
|
||||
return [
|
||||
['version' => 0],
|
||||
['version' => 1],
|
||||
['version' => 3],
|
||||
['version' => 4],
|
||||
['version' => 5],
|
||||
['version' => 6],
|
||||
['version' => 7],
|
||||
['version' => 8],
|
||||
['version' => 9],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Test\Rfc4122;
|
||||
|
||||
use Mockery;
|
||||
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;
|
||||
use Ramsey\Uuid\Rfc4122\UuidV3;
|
||||
use Ramsey\Uuid\Test\TestCase;
|
||||
|
||||
class UuidV3Test extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideTestVersions
|
||||
*/
|
||||
public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void
|
||||
{
|
||||
$fields = Mockery::mock(FieldsInterface::class, [
|
||||
'getVersion' => $version,
|
||||
]);
|
||||
|
||||
$numberConverter = Mockery::mock(NumberConverterInterface::class);
|
||||
$codec = Mockery::mock(CodecInterface::class);
|
||||
$timeConverter = Mockery::mock(TimeConverterInterface::class);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Fields used to create a UuidV3 must represent a '
|
||||
. 'version 3 (name-based, MD5-hashed) UUID'
|
||||
);
|
||||
|
||||
new UuidV3($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
|
||||
*/
|
||||
public function provideTestVersions(): array
|
||||
{
|
||||
return [
|
||||
['version' => 0],
|
||||
['version' => 1],
|
||||
['version' => 2],
|
||||
['version' => 4],
|
||||
['version' => 5],
|
||||
['version' => 6],
|
||||
['version' => 7],
|
||||
['version' => 8],
|
||||
['version' => 9],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Test\Rfc4122;
|
||||
|
||||
use Mockery;
|
||||
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;
|
||||
use Ramsey\Uuid\Rfc4122\UuidV4;
|
||||
use Ramsey\Uuid\Test\TestCase;
|
||||
|
||||
class UuidV4Test extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideTestVersions
|
||||
*/
|
||||
public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void
|
||||
{
|
||||
$fields = Mockery::mock(FieldsInterface::class, [
|
||||
'getVersion' => $version,
|
||||
]);
|
||||
|
||||
$numberConverter = Mockery::mock(NumberConverterInterface::class);
|
||||
$codec = Mockery::mock(CodecInterface::class);
|
||||
$timeConverter = Mockery::mock(TimeConverterInterface::class);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Fields used to create a UuidV4 must represent a '
|
||||
. 'version 4 (random) UUID'
|
||||
);
|
||||
|
||||
new UuidV4($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
|
||||
*/
|
||||
public function provideTestVersions(): array
|
||||
{
|
||||
return [
|
||||
['version' => 0],
|
||||
['version' => 1],
|
||||
['version' => 2],
|
||||
['version' => 3],
|
||||
['version' => 5],
|
||||
['version' => 6],
|
||||
['version' => 7],
|
||||
['version' => 8],
|
||||
['version' => 9],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Test\Rfc4122;
|
||||
|
||||
use Mockery;
|
||||
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;
|
||||
use Ramsey\Uuid\Rfc4122\UuidV5;
|
||||
use Ramsey\Uuid\Test\TestCase;
|
||||
|
||||
class UuidV5Test extends TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideTestVersions
|
||||
*/
|
||||
public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void
|
||||
{
|
||||
$fields = Mockery::mock(FieldsInterface::class, [
|
||||
'getVersion' => $version,
|
||||
]);
|
||||
|
||||
$numberConverter = Mockery::mock(NumberConverterInterface::class);
|
||||
$codec = Mockery::mock(CodecInterface::class);
|
||||
$timeConverter = Mockery::mock(TimeConverterInterface::class);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->expectExceptionMessage(
|
||||
'Fields used to create a UuidV5 must represent a '
|
||||
. 'version 5 (named-based, SHA1-hashed) UUID'
|
||||
);
|
||||
|
||||
new UuidV5($fields, $numberConverter, $codec, $timeConverter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification
|
||||
*/
|
||||
public function provideTestVersions(): array
|
||||
{
|
||||
return [
|
||||
['version' => 0],
|
||||
['version' => 1],
|
||||
['version' => 2],
|
||||
['version' => 3],
|
||||
['version' => 4],
|
||||
['version' => 6],
|
||||
['version' => 7],
|
||||
['version' => 8],
|
||||
['version' => 9],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user