diff --git a/src/Provider/Dce/SystemDceSecurityProvider.php b/src/Provider/Dce/SystemDceSecurityProvider.php index 21a9fab..1a1f4cf 100644 --- a/src/Provider/Dce/SystemDceSecurityProvider.php +++ b/src/Provider/Dce/SystemDceSecurityProvider.php @@ -193,7 +193,7 @@ class SystemDceSecurityProvider implements DceSecurityProviderInterface * Since Windows does not have the same concept as an effective POSIX GID * for the running script, we will get the local group memberships for the * user running the script. Then, we will get the SID (security identifier) - * for the first group. that appears in that list. Finally, we will return + * for the first group that appears in that list. Finally, we will return * the RID (relative identifier) for the group and use that as the GID. * * @link https://www.windows-commandline.com/list-of-user-groups-command-line/ List of user groups command line diff --git a/src/Rfc4122/UuidV2.php b/src/Rfc4122/UuidV2.php index 6164d65..7a3e8dc 100644 --- a/src/Rfc4122/UuidV2.php +++ b/src/Rfc4122/UuidV2.php @@ -19,6 +19,7 @@ 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\Type\Integer as IntegerObject; use Ramsey\Uuid\Uuid; /** @@ -61,4 +62,36 @@ final class UuidV2 extends Uuid implements UuidInterface parent::__construct($fields, $numberConverter, $codec, $timeConverter); } + + /** + * Returns the local domain used to create this version 2 UUID + */ + public function getLocalDomain(): int + { + /** @var Rfc4122FieldsInterface $fields */ + $fields = $this->getFields(); + + return (int) hexdec($fields->getClockSeqLow()->toString()); + } + + /** + * Returns the string name of the local domain + */ + public function getLocalDomainName(): string + { + return Uuid::DCE_DOMAIN_NAMES[$this->getLocalDomain()]; + } + + /** + * Returns the local identifier for the domain used to create this version 2 UUID + */ + public function getLocalIdentifier(): IntegerObject + { + /** @var Rfc4122FieldsInterface $fields */ + $fields = $this->getFields(); + + return new IntegerObject( + $this->numberConverter->fromHex($fields->getTimeLow()->toString()) + ); + } } diff --git a/src/Uuid.php b/src/Uuid.php index 68b4ff6..0a34612 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -188,6 +188,17 @@ class Uuid implements UuidInterface */ public const DCE_DOMAIN_ORG = 2; + /** + * DCE Security domain string names + * + * @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, ยง11.5.1.1 + */ + public const DCE_DOMAIN_NAMES = [ + self::DCE_DOMAIN_PERSON => 'person', + self::DCE_DOMAIN_GROUP => 'group', + self::DCE_DOMAIN_ORG => 'org', + ]; + /** * @var UuidFactoryInterface|null */ diff --git a/tests/Rfc4122/UuidV2Test.php b/tests/Rfc4122/UuidV2Test.php index 9a68d2b..5b9ca46 100644 --- a/tests/Rfc4122/UuidV2Test.php +++ b/tests/Rfc4122/UuidV2Test.php @@ -12,6 +12,8 @@ use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidV2; use Ramsey\Uuid\Test\TestCase; +use Ramsey\Uuid\Type\Integer; +use Ramsey\Uuid\Uuid; class UuidV2Test extends TestCase { @@ -54,4 +56,67 @@ class UuidV2Test extends TestCase ['version' => 9], ]; } + + /** + * @dataProvider provideLocalDomainAndIdentifierForTests + */ + public function testGetLocalDomainAndIdentifier( + int $domain, + Integer $identifier, + int $expectedDomain, + string $expectedDomainName, + string $expectedIdentifier + ): void { + /** @var UuidV2 $uuid */ + $uuid = Uuid::uuid2($domain, $identifier); + + $this->assertSame($expectedDomain, $uuid->getLocalDomain()); + $this->assertSame($expectedDomainName, $uuid->getLocalDomainName()); + $this->assertInstanceOf(Integer::class, $uuid->getLocalIdentifier()); + $this->assertSame($expectedIdentifier, $uuid->getLocalIdentifier()->toString()); + } + + /** + * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification + */ + public function provideLocalDomainAndIdentifierForTests(): array + { + return [ + [ + 'domain' => Uuid::DCE_DOMAIN_PERSON, + 'identifier' => new Integer('12345678'), + 'expectedDomain' => 0, + 'expectedDomainName' => 'person', + 'expectedIdentifier' => '12345678', + ], + [ + 'domain' => Uuid::DCE_DOMAIN_GROUP, + 'identifier' => new Integer('87654321'), + 'expectedDomain' => 1, + 'expectedDomainName' => 'group', + 'expectedIdentifier' => '87654321', + ], + [ + 'domain' => Uuid::DCE_DOMAIN_ORG, + 'identifier' => new Integer('1'), + 'expectedDomain' => 2, + 'expectedDomainName' => 'org', + 'expectedIdentifier' => '1', + ], + [ + 'domain' => Uuid::DCE_DOMAIN_PERSON, + 'identifier' => new Integer('0'), + 'expectedDomain' => 0, + 'expectedDomainName' => 'person', + 'expectedIdentifier' => '0', + ], + [ + 'domain' => Uuid::DCE_DOMAIN_PERSON, + 'identifier' => new Integer('4294967295'), + 'expectedDomain' => 0, + 'expectedDomainName' => 'person', + 'expectedIdentifier' => '4294967295', + ], + ]; + } }