Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow static guid calls #307

Open
wants to merge 4 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Guid/Guid.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
use Ramsey\Uuid\Codec\CodecInterface;
use Ramsey\Uuid\Converter\NumberConverterInterface;
use Ramsey\Uuid\Converter\TimeConverterInterface;
use Ramsey\Uuid\FeatureSet;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactory;
use Ramsey\Uuid\UuidFactoryInterface;

/**
* Guid represents a UUID with "native" (little-endian) byte order
Expand Down Expand Up @@ -50,6 +53,11 @@
*/
final class Guid extends Uuid
{
/**
* @var UuidFactoryInterface|null
*/
private static $factory = null;

public function __construct(
Fields $fields,
NumberConverterInterface $numberConverter,
Expand All @@ -58,4 +66,27 @@ public function __construct(
) {
parent::__construct($fields, $numberConverter, $codec, $timeConverter);
}

/**
* Returns the factory used to create UUIDs
*/
public static function getFactory(): UuidFactoryInterface
{
if (self::$factory === null) {
self::$factory = new UuidFactory(new FeatureSet(true));
}

return self::$factory;
}

/**
* Sets the factory used to create UUIDs
*
* @param UuidFactoryInterface $factory A factory that will be used by this
* class to create UUIDs
*/
public static function setFactory(UuidFactoryInterface $factory): void
{
throw new \Exception('not allowed');
}
}
22 changes: 11 additions & 11 deletions src/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public static function fromBytes(string $bytes): UuidInterface
);
}

return self::getFactory()->fromBytes($bytes);
return static::getFactory()->fromBytes($bytes);
}

/**
Expand Down Expand Up @@ -488,7 +488,7 @@ public static function fromString(string $uuid): UuidInterface
return new LazyUuidFromString($uuid);
}

return self::getFactory()->fromString($uuid);
return static::getFactory()->fromString($uuid);
}

/**
Expand All @@ -509,7 +509,7 @@ public static function fromDateTime(
?Hexadecimal $node = null,
?int $clockSeq = null
): UuidInterface {
return self::getFactory()->fromDateTime($dateTime, $node, $clockSeq);
return static::getFactory()->fromDateTime($dateTime, $node, $clockSeq);
}

/**
Expand All @@ -525,7 +525,7 @@ public static function fromDateTime(
*/
public static function fromInteger(string $integer): UuidInterface
{
return self::getFactory()->fromInteger($integer);
return static::getFactory()->fromInteger($integer);
}

/**
Expand All @@ -540,7 +540,7 @@ public static function fromInteger(string $integer): UuidInterface
*/
public static function isValid(string $uuid): bool
{
return self::getFactory()->getValidator()->validate($uuid);
return static::getFactory()->getValidator()->validate($uuid);
}

/**
Expand All @@ -559,7 +559,7 @@ public static function isValid(string $uuid): bool
*/
public static function uuid1($node = null, ?int $clockSeq = null): UuidInterface
{
return self::getFactory()->uuid1($node, $clockSeq);
return static::getFactory()->uuid1($node, $clockSeq);
}

/**
Expand Down Expand Up @@ -588,7 +588,7 @@ public static function uuid2(
?Hexadecimal $node = null,
?int $clockSeq = null
): UuidInterface {
return self::getFactory()->uuid2($localDomain, $localIdentifier, $node, $clockSeq);
return static::getFactory()->uuid2($localDomain, $localIdentifier, $node, $clockSeq);
}

/**
Expand All @@ -611,7 +611,7 @@ public static function uuid2(
*/
public static function uuid3($ns, string $name): UuidInterface
{
return self::getFactory()->uuid3($ns, $name);
return static::getFactory()->uuid3($ns, $name);
}

/**
Expand All @@ -622,7 +622,7 @@ public static function uuid3($ns, string $name): UuidInterface
*/
public static function uuid4(): UuidInterface
{
return self::getFactory()->uuid4();
return static::getFactory()->uuid4();
}

/**
Expand All @@ -645,7 +645,7 @@ public static function uuid4(): UuidInterface
*/
public static function uuid5($ns, string $name): UuidInterface
{
return self::getFactory()->uuid5($ns, $name);
return static::getFactory()->uuid5($ns, $name);
}

/**
Expand All @@ -665,6 +665,6 @@ public static function uuid6(
?Hexadecimal $node = null,
?int $clockSeq = null
): UuidInterface {
return self::getFactory()->uuid6($node, $clockSeq);
return static::getFactory()->uuid6($node, $clockSeq);
}
}