Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* UUID PK support * fix * styleci * move to dedicated class * fixes * fixes for mariadb * test fix * test fix * add psalm fix * mysql 5.7 fix * add tests for UuidHelper * styleci fix
- Loading branch information
Showing
7 changed files
with
315 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Helper; | ||
|
||
use Yiisoft\Db\Exception\InvalidArgumentException; | ||
|
||
use function bin2hex; | ||
use function hex2bin; | ||
use function preg_match; | ||
use function str_replace; | ||
|
||
final class UuidHelper | ||
{ | ||
public static function toUuid(string $blobString): string | ||
{ | ||
if (self::isValidUuid($blobString)) { | ||
return $blobString; | ||
} | ||
|
||
if (strlen($blobString) === 16) { | ||
$hex = bin2hex($blobString); | ||
} elseif (strlen($blobString) === 32 && self::isValidHexUuid($blobString)) { | ||
$hex = $blobString; | ||
} else { | ||
throw new InvalidArgumentException('Length of source data is should be 16 or 32 bytes.'); | ||
} | ||
|
||
return | ||
substr($hex, 0, 8) . '-' . | ||
substr($hex, 8, 4) . '-' . | ||
substr($hex, 12, 4) . '-' . | ||
substr($hex, 16, 4) . '-' . | ||
substr($hex, 20) | ||
; | ||
} | ||
|
||
public static function isValidUuid(string $uuidString): bool | ||
{ | ||
return (bool) preg_match('/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i', $uuidString); | ||
} | ||
|
||
public static function isValidHexUuid(string $uuidString): bool | ||
{ | ||
return (bool) preg_match('/^[0-9a-f]{32}$/i', $uuidString); | ||
} | ||
|
||
public static function uuidToBlob(string $uuidString): string | ||
{ | ||
if (!self::isValidUuid($uuidString)) { | ||
throw new InvalidArgumentException('Incorrect UUID.'); | ||
} | ||
|
||
return (string) hex2bin(str_replace('-', '', $uuidString)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Db\Tests\Db\Helper; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Db\Exception\InvalidArgumentException; | ||
use Yiisoft\Db\Helper\UuidHelper; | ||
|
||
/** | ||
* @group db | ||
*/ | ||
final class UuidHelperTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider successUuids | ||
*/ | ||
public function testConvert(string $uuid): void | ||
{ | ||
$blobUuid = UuidHelper::uuidToBlob($uuid); | ||
$this->assertEquals($uuid, UuidHelper::toUuid($blobUuid)); | ||
} | ||
|
||
/** | ||
* @dataProvider incorrectUuids | ||
*/ | ||
public function testToBlobIncorrectUuid(string $uuid): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Incorrect UUID.'); | ||
|
||
$blobUuid = UuidHelper::uuidToBlob($uuid); | ||
$this->assertEquals($uuid, UuidHelper::toUuid($blobUuid)); | ||
} | ||
|
||
/** | ||
* @dataProvider blobUuids | ||
*/ | ||
public function testToUuid($blobUuid, $expected): void | ||
{ | ||
$uuid = UuidHelper::toUuid($blobUuid); | ||
$this->assertEquals($expected, $uuid); | ||
} | ||
|
||
/** | ||
* @dataProvider incorrectBlobUuids | ||
*/ | ||
public function testToUuidFailed($blobUuid, $expected): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Length of source data is should be 16 or 32 bytes.'); | ||
|
||
$uuid = UuidHelper::toUuid($blobUuid); | ||
$this->assertEquals($expected, $uuid); | ||
} | ||
|
||
public function successUuids(): array | ||
{ | ||
return [ | ||
['738146be-87b1-49f2-9913-36142fb6fcbe'], | ||
]; | ||
} | ||
|
||
public function incorrectUuids(): array | ||
{ | ||
return [ | ||
['738146be-87b149f2-9913-36142fb6fcbe'], | ||
['738146be-87b1-K9f2-9913-36142fb6fcbe'], | ||
['738146be+87b1-K9f2-9913-36142fb6fcbe'], | ||
]; | ||
} | ||
|
||
public function blobUuids(): array | ||
{ | ||
return [ | ||
['738146be-87b1-49f2-9913-36142fb6fcbe', '738146be-87b1-49f2-9913-36142fb6fcbe'], | ||
['738146be87b149f2991336142fb6fcbe', '738146be-87b1-49f2-9913-36142fb6fcbe'], | ||
[hex2bin('738146be87b149f2991336142fb6fcbe'), '738146be-87b1-49f2-9913-36142fb6fcbe'], | ||
]; | ||
} | ||
|
||
public function incorrectBlobUuids(): array | ||
{ | ||
return [ | ||
['738146be-87b1-49f2-9913-36142fbfcbe', '738146be-87b1-49f2-9913-36142fb6fcbe'], | ||
['738146be87b149f2991336142fb6fcb', '738146be-87b1-49f2-9913-36142fb6fcbe'], | ||
[hex2bin('738146be87b149f291336142fb6fcb'), '738146be-87b1-49f2-9913-36142fb6fcbe'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.