Skip to content

Commit 3d39604

Browse files
committed
uuid methods
1 parent 7ba0c22 commit 3d39604

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"doctrine/dbal": "~2.6",
7676
"filp/whoops": "^2.1.4",
7777
"mockery/mockery": "~1.0",
78+
"moontoast/math": "^1.1",
7879
"orchestra/testbench-core": "3.6.*",
7980
"pda/pheanstalk": "~3.0",
8081
"phpunit/phpunit": "~6.0",

src/Illuminate/Support/Str.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Illuminate\Support;
44

5+
use Ramsey\Uuid\Uuid;
6+
use Ramsey\Uuid\UuidFactory;
57
use Illuminate\Support\Traits\Macroable;
8+
use Ramsey\Uuid\Generator\CombGenerator;
9+
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
610

711
class Str
812
{
@@ -517,6 +521,37 @@ public static function ucfirst($string)
517521
return static::upper(static::substr($string, 0, 1)).static::substr($string, 1);
518522
}
519523

524+
/**
525+
* Generate a UUID (version 4).
526+
*
527+
* @return \Ramsey\Uuid\Uuid
528+
*/
529+
public static function uuid()
530+
{
531+
return Uuid::uuid4();
532+
}
533+
534+
/**
535+
* Generate a time-ordered UUID (version 4).
536+
*
537+
* @return \Ramsey\Uuid\Uuid
538+
*/
539+
public static function orderedUuid()
540+
{
541+
$factory = new UuidFactory;
542+
543+
$factory->setRandomGenerator(new CombGenerator(
544+
$factory->getRandomGenerator(),
545+
$factory->getNumberConverter()
546+
));
547+
548+
$factory->setCodec(new TimestampFirstCombCodec(
549+
$factory->getUuidBuilder()
550+
));
551+
552+
return $factory->uuid4();
553+
}
554+
520555
/**
521556
* Returns the replacements for the ascii method.
522557
*

tests/Support/SupportStrTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Support;
44

5+
use Ramsey\Uuid\Uuid;
56
use Illuminate\Support\Str;
67
use PHPUnit\Framework\TestCase;
78

@@ -285,6 +286,12 @@ public function testUcfirst()
285286
$this->assertEquals('Мама', Str::ucfirst('мама'));
286287
$this->assertEquals('Мама мыла раму', Str::ucfirst('мама мыла раму'));
287288
}
289+
290+
public function testUuid()
291+
{
292+
$this->assertInstanceOf(Uuid::class, Str::uuid());
293+
$this->assertInstanceOf(Uuid::class, Str::orderedUuid());
294+
}
288295
}
289296

290297
class StringableObjectStub

0 commit comments

Comments
 (0)