From 39bc7f24f87e0c9c5de5deb034492f667bd3f789 Mon Sep 17 00:00:00 2001 From: Antonio Carlos Ribeiro Date: Sat, 10 Feb 2018 14:08:58 -0200 Subject: [PATCH] Pair with Laravel 5.6 --- composer.json | 6 ++++-- src/Support/Str.php | 35 ++++++++++++++++++++++++++++++++ tests/Support/SupportStrTest.php | 7 +++++++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8dbd5d8..a80c8c2 100644 --- a/composer.json +++ b/composer.json @@ -29,13 +29,15 @@ "require": { "php": ">=7.0", "doctrine/inflector": "^1.2", - "symfony/var-dumper": "~3.3" + "symfony/var-dumper": "~3.3", + "ramsey/uuid": "^3.7" }, "require-dev": { "phpunit/phpunit" : ">=6.0", "squizlabs/php_codesniffer": "^2.3", - "mockery/mockery": "~1.0" + "mockery/mockery": "~1.0", + "moontoast/math": "^1.1" }, "autoload": { diff --git a/src/Support/Str.php b/src/Support/Str.php index 7381cfe..3778279 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -2,7 +2,11 @@ namespace IlluminateAgnostic\Str\Support; +use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\UuidFactory; use IlluminateAgnostic\Str\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 696a04e..d117e18 100644 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -2,6 +2,7 @@ namespace IlluminateAgnostic\Str\Tests\Support; +use Ramsey\Uuid\Uuid; use IlluminateAgnostic\Str\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