From f6ab5500db3b59401eaddd069dab1455841227a6 Mon Sep 17 00:00:00 2001 From: prolic Date: Mon, 6 Apr 2020 22:20:07 -0400 Subject: [PATCH] allow static guid calls --- src/Guid/Guid.php | 31 +++++++++++++++++++++++++++++++ src/Uuid.php | 22 +++++++++++----------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/src/Guid/Guid.php b/src/Guid/Guid.php index 08a00695..918b1277 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; use Ramsey\Uuid\UuidInterface; /** @@ -51,6 +54,11 @@ */ final class Guid extends Uuid implements UuidInterface { + /** + * @var UuidFactoryInterface|null + */ + private static $factory = null; + public function __construct( Fields $fields, NumberConverterInterface $numberConverter, @@ -59,4 +67,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 0f86cdd6..fe635158 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -385,7 +385,7 @@ public static function setFactory(UuidFactoryInterface $factory): void */ public static function fromBytes(string $bytes): UuidInterface { - return self::getFactory()->fromBytes($bytes); + return static::getFactory()->fromBytes($bytes); } /** @@ -401,7 +401,7 @@ public static function fromBytes(string $bytes): UuidInterface */ public static function fromString(string $uuid): UuidInterface { - return self::getFactory()->fromString($uuid); + return static::getFactory()->fromString($uuid); } /** @@ -422,7 +422,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); } /** @@ -438,7 +438,7 @@ public static function fromDateTime( */ public static function fromInteger(string $integer): UuidInterface { - return self::getFactory()->fromInteger($integer); + return static::getFactory()->fromInteger($integer); } /** @@ -453,7 +453,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); } /** @@ -472,7 +472,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); } /** @@ -501,7 +501,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); } /** @@ -519,7 +519,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); } /** @@ -530,7 +530,7 @@ public static function uuid3($ns, string $name): UuidInterface */ public static function uuid4(): UuidInterface { - return self::getFactory()->uuid4(); + return static::getFactory()->uuid4(); } /** @@ -548,7 +548,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); } /** @@ -568,6 +568,6 @@ public static function uuid6( ?Hexadecimal $node = null, ?int $clockSeq = null ): UuidInterface { - return self::getFactory()->uuid6($node, $clockSeq); + return static::getFactory()->uuid6($node, $clockSeq); } }