-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a91979b
commit 5618f24
Showing
4 changed files
with
72 additions
and
66 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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Longman\LaravelLodash\Eloquent\Casts; | ||
|
||
use Illuminate\Contracts\Database\Eloquent\CastsAttributes; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Longman\LaravelLodash\Support\Uuid; | ||
|
||
class BinaryUuid implements CastsAttributes | ||
{ | ||
public function get(Model $model, string $key, mixed $value, array $attributes): ?string | ||
{ | ||
if (blank($value)) { | ||
return null; | ||
} | ||
|
||
return Uuid::toString($value); | ||
} | ||
|
||
public function set(Model $model, string $key, mixed $value, array $attributes): ?array | ||
{ | ||
if (blank($value)) { | ||
return null; | ||
} | ||
|
||
return [ | ||
$key => Uuid::toBinary($value), | ||
]; | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Longman\LaravelLodash\Support; | ||
|
||
use Ramsey\Uuid\Exception\InvalidArgumentException; | ||
use Ramsey\Uuid\Uuid as UuidFactory; | ||
|
||
use function strtolower; | ||
|
||
class Uuid | ||
{ | ||
public static function isBinary(string $value): bool | ||
{ | ||
try { | ||
UuidFactory::fromBytes($value); | ||
} catch (InvalidArgumentException) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public static function toString(string $uuid): string | ||
{ | ||
$uuid = UuidFactory::fromBytes($uuid); | ||
|
||
return $uuid->toString(); | ||
} | ||
|
||
public static function toBinary(string $uuid): string | ||
{ | ||
$uuid = UuidFactory::fromString(strtolower($uuid)); | ||
|
||
return $uuid->getBytes(); | ||
} | ||
} |