Skip to content

Commit

Permalink
[6.x] Add Str::isUuid() helper (#31148)
Browse files Browse the repository at this point in the history
* Add Str::isUuid() helper

* Style CI Fix

* Update Str.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
leonhh and taylorotwell committed Jan 16, 2020
1 parent f9c450a commit bfd17f0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ public static function is($pattern, $value)
return false;
}

/**
* Determine if a given string is a valid UUID.
*
* @param string $value
* @return bool
*/
public static function isUuid($value)
{
if (! is_string($value)) {
return false;
}

return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0;

This comment has been minimized.

Copy link
@danilopinotti

danilopinotti Jan 17, 2020

Contributor

The Lib (Ramsey\Uuid\Uuid) that we use to generate Uuid has a validator method:
Ramsey\Uuid\Uuid::isValid($uuid);

The REGEX used in lib is: '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$'

This comment has been minimized.

Copy link
@GrahamCampbell

GrahamCampbell Jan 17, 2020

Member

Their regex is exactly the same as ours, just uses slightly different syntax.

}

/**
* Convert a string to kebab case.
*
Expand Down
6 changes: 1 addition & 5 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1750,11 +1750,7 @@ public function validateUrl($attribute, $value)
*/
public function validateUuid($attribute, $value)
{
if (! is_string($value)) {
return false;
}

return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $value) > 0;
return Str::isUuid($value);
}

/**
Expand Down
48 changes: 48 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,22 @@ public function testIs()
$this->assertFalse(Str::is([], 'test'));
}

/**
* @dataProvider validUuidList
*/
public function testIsUuidWithValidUuid($uuid)
{
$this->assertTrue(Str::isUuid($uuid));
}

/**
* @dataProvider invalidUuidList
*/
public function testIsUuidWithInvalidUuid($uuid)
{
$this->assertFalse(Str::isUuid($uuid));
}

public function testKebab()
{
$this->assertSame('laravel-php-framework', Str::kebab('LaravelPhpFramework'));
Expand Down Expand Up @@ -388,6 +404,38 @@ public function testUuid()
$this->assertInstanceOf(UuidInterface::class, Str::uuid());
$this->assertInstanceOf(UuidInterface::class, Str::orderedUuid());
}

public function validUuidList()
{
return [
['a0a2a2d2-0b87-4a18-83f2-2529882be2de'],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
['00000000-0000-0000-0000-000000000000'],
['e60d3f48-95d7-4d8d-aad0-856f29a27da2'],
['ff6f8cb0-c57d-11e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-21e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-31e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-41e1-9b21-0800200c9a66'],
['ff6f8cb0-c57d-51e1-9b21-0800200c9a66'],
['FF6F8CB0-C57D-11E1-9B21-0800200C9A66'],
];
}

public function invalidUuidList()
{
return [
['not a valid uuid so we can test this'],
['zf6f8cb0-c57d-11e1-9b21-0800200c9a66'],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'.PHP_EOL],
['145a1e72-d11d-11e8-a8d5-f2801f1b9fd1 '],
[' 145a1e72-d11d-11e8-a8d5-f2801f1b9fd1'],
['145a1e72-d11d-11e8-a8d5-f2z01f1b9fd1'],
['3f6f8cb0-c57d-11e1-9b21-0800200c9a6'],
['af6f8cb-c57d-11e1-9b21-0800200c9a66'],
['af6f8cb0c57d11e19b210800200c9a66'],
['ff6f8cb0-c57da-51e1-9b21-0800200c9a66'],
];
}
}

class StringableObjectStub
Expand Down

0 comments on commit bfd17f0

Please sign in to comment.