diff --git a/src/Guid/Guid.php b/src/Guid/Guid.php index 0af02104..494bf7fe 100644 --- a/src/Guid/Guid.php +++ b/src/Guid/Guid.php @@ -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 @@ -50,6 +53,11 @@ */ final class Guid extends Uuid { + /** + * @var UuidFactoryInterface|null + */ + private static $factory = null; + public function __construct( Fields $fields, NumberConverterInterface $numberConverter, @@ -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'); + } } diff --git a/src/Uuid.php b/src/Uuid.php index 82c37d6b..83ba4f95 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -469,7 +469,7 @@ public static function fromBytes(string $bytes): UuidInterface ); } - return self::getFactory()->fromBytes($bytes); + return static::getFactory()->fromBytes($bytes); } /** @@ -491,7 +491,7 @@ public static function fromString(string $uuid): UuidInterface return new LazyUuidFromString($uuid); } - return self::getFactory()->fromString($uuid); + return static::getFactory()->fromString($uuid); } /** @@ -512,7 +512,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); } /** @@ -551,7 +551,7 @@ public static function fromHexadecimal(Hexadecimal $hex): UuidInterface */ public static function fromInteger(string $integer): UuidInterface { - return self::getFactory()->fromInteger($integer); + return static::getFactory()->fromInteger($integer); } /** @@ -565,7 +565,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); } /** @@ -584,7 +584,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); } /** @@ -613,7 +613,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); } /** @@ -628,7 +628,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); } /** @@ -639,7 +639,7 @@ public static function uuid3($ns, string $name): UuidInterface */ public static function uuid4(): UuidInterface { - return self::getFactory()->uuid4(); + return static::getFactory()->uuid4(); } /** @@ -654,7 +654,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); } /** @@ -674,7 +674,7 @@ public static function uuid6( ?Hexadecimal $node = null, ?int $clockSeq = null ): UuidInterface { - return self::getFactory()->uuid6($node, $clockSeq); + return static::getFactory()->uuid6($node, $clockSeq); } /**