Skip to content
Snippets Groups Projects
Commit 436f54d1 authored by Ján Forgáč's avatar Ján Forgáč
Browse files

UuidConverter: binary MySqlEndian added

parent 46a3c3ee
Branches
Tags v6.0.2
No related merge requests found
Pipeline #65144 failed
......@@ -11,6 +11,10 @@ use Ramsey;
class UuidConverter
{
const BINARY = 'binary';
const HEX_STRING = 'string';
const UNHEX_HEX_STRING = 'unhex';
public static function binaryToHex($value): string
{
$uuid = Ramsey\Uuid\Uuid::fromBytes($value);
......@@ -23,22 +27,22 @@ class UuidConverter
return $uuid->toString();
}
public static function binaryToMySqlEndian($value, bool $unHexAsString = true)
public static function binaryToMySqlEndian($value, string $returnType = self::UNHEX_HEX_STRING)
{
$string = Ramsey\Uuid\Uuid::fromBytes($value)->toString();
$parts = explode('-', $string);
$endian = $parts[2] . $parts[1] . $parts[0] . $parts[3] . $parts[4];
return $unHexAsString ? "UNHEX('$endian')" : $endian;
return self::returnEndian($endian, $returnType);
}
public static function stringOrHexToMySqlEndian($value, bool $unHexAsString = true)
public static function stringOrHexToMySqlEndian($value, string $returnType = self::UNHEX_HEX_STRING)
{
$string = Ramsey\Uuid\Uuid::fromString($value)->toString();
$parts = explode('-', $string);
$endian = $parts[2] . $parts[1] . $parts[0] . $parts[3] . $parts[4];
return $unHexAsString ? "UNHEX('$endian')" : $endian;
return self::returnEndian($endian, $returnType);
}
public static function hexToBinary($value)
......@@ -53,4 +57,13 @@ class UuidConverter
return $uuid->getBytes();
}
private static function returnEndian(string $endian, string $returnType)
{
return match ($returnType) {
self::UNHEX_HEX_STRING => "UNHEX('$endian')",
self::BINARY => hex2bin($endian),
self::HEX_STRING => $endian,
};
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment