From 3d39604bba72d45dab5b53951af42bbb21110cad Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 16 Jan 2018 14:09:42 -0600 Subject: [PATCH] uuid methods --- composer.json | 1 + src/Illuminate/Support/Str.php | 35 ++++++++++++++++++++++++++++++++ tests/Support/SupportStrTest.php | 7 +++++++ 3 files changed, 43 insertions(+) diff --git a/composer.json b/composer.json index 589dfba05aba..5e1d68b07608 100644 --- a/composer.json +++ b/composer.json @@ -75,6 +75,7 @@ "doctrine/dbal": "~2.6", "filp/whoops": "^2.1.4", "mockery/mockery": "~1.0", + "moontoast/math": "^1.1", "orchestra/testbench-core": "3.6.*", "pda/pheanstalk": "~3.0", "phpunit/phpunit": "~6.0", diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 6775f92df883..eb1d94e7d0de 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -2,7 +2,11 @@ namespace Illuminate\Support; +use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidFactory; use Illuminate\Support\Traits\Macroable; +use Ramsey\Uuid\Generator\CombGenerator; +use Ramsey\Uuid\Codec\TimestampFirstCombCodec; class Str { @@ -517,6 +521,37 @@ public static function ucfirst($string) return static::upper(static::substr($string, 0, 1)).static::substr($string, 1); } + /** + * Generate a UUID (version 4). + * + * @return \Ramsey\Uuid\Uuid + */ + public static function uuid() + { + return Uuid::uuid4(); + } + + /** + * Generate a time-ordered UUID (version 4). + * + * @return \Ramsey\Uuid\Uuid + */ + public static function orderedUuid() + { + $factory = new UuidFactory; + + $factory->setRandomGenerator(new CombGenerator( + $factory->getRandomGenerator(), + $factory->getNumberConverter() + )); + + $factory->setCodec(new TimestampFirstCombCodec( + $factory->getUuidBuilder() + )); + + return $factory->uuid4(); + } + /** * Returns the replacements for the ascii method. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index ead5077a5486..5288406d79b4 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -2,6 +2,7 @@ namespace Illuminate\Tests\Support; +use Ramsey\Uuid\Uuid; use Illuminate\Support\Str; use PHPUnit\Framework\TestCase; @@ -285,6 +286,12 @@ public function testUcfirst() $this->assertEquals('Мама', Str::ucfirst('мама')); $this->assertEquals('Мама мыла раму', Str::ucfirst('мама мыла раму')); } + + public function testUuid() + { + $this->assertInstanceOf(Uuid::class, Str::uuid()); + $this->assertInstanceOf(Uuid::class, Str::orderedUuid()); + } } class StringableObjectStub