обработка uuid

This commit is contained in:
Toy Rik 2026-02-21 18:26:45 +03:00
parent 324e57e5f5
commit daa4a197c8
9 changed files with 168 additions and 6 deletions

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Domain\Shared\Casts;
use App\Domain\Shared\ValueObjects\Uuid;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\UuidInterface;
use Spatie\LaravelData\Casts\Cast;
use Spatie\LaravelData\Support\Creation\CreationContext;
use Spatie\LaravelData\Support\DataProperty;
class UuidCast implements CastsAttributes, Cast
{
public function get(Model $model, string $key, mixed $value, array $attributes): ?Uuid
{
if (is_null($value)) {
return null;
}
return Uuid::fromString($value);
}
public function set(Model $model, string $key, mixed $value, array $attributes)
{
if (is_null($value)) {
return [$key => null];
}
if ($value instanceof UuidInterface) {
return [$key => $value->toString()];
}
return [$key => Uuid::fromString((string) $value)->toString()];
}
public function cast(DataProperty $property, mixed $value, array $properties, CreationContext $context): mixed
{
return Uuid::fromString((string) $value);
}
}

View File

@ -43,4 +43,3 @@ abstract class BaseRepository extends Model
return $model->newQuery();
}
}

View File

@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Domain\Shared\ValueObjects;
use DomainException;
use Ramsey\Uuid\Uuid as RamseyUuid;
use Ramsey\Uuid\UuidInterface;
final readonly class Uuid
{
private function __construct(
private UuidInterface $value
) {
}
public static function generate(): self
{
return new self(RamseyUuid::uuid4());
}
public static function fromString(string $value): self
{
if (!RamseyUuid::isValid($value)) {
throw new DomainException(sprintf('Недопустимая строка UUID: %s', $value));
}
return new self(RamseyUuid::fromString($value));
}
public function toString(): string
{
return $this->value->toString();
}
public function equals(self $other): bool
{
return $this->value->equals($other);
}
public function __toString(): string
{
return $this->toString();
}
public static function validate(string|array $uuid): bool
{
$uuids = is_array($uuid) ? $uuid : [$uuid];
foreach ($uuids as $u) {
if (!RamseyUuid::isValid($u)) {
return false;
}
}
return true;
}
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Domain\User\Actions;
use App\Domain\User\Data\ShowRequest;
use App\Domain\User\Data\ShowResponseData;
use App\Domain\User\Repositories\UserRepository;
use App\Models\User;
@ -16,8 +17,9 @@ class ShowAction
}
public function execute(ShowRequest $request): User
public function execute(ShowRequest $request)
{
return $this->userRepository->whereUuid($request->user_uuid)->firstOrFail();
$result = $this->userRepository->whereUuid($request->user_uuid)->firstOrFail();
return ShowResponseData::fromModel($result);
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace App\Domain\User\Data;
use App\Domain\User\Data\ValueObjects\RoleData;
use App\Models\User;
use Spatie\LaravelData\Data;
class ShowResponseData extends Data
{
public function __construct(
public readonly string $uuid,
public readonly string $name,
public readonly RoleData $role,
) {
}
public static function fromModel(User $model): self
{
return new self(
uuid: $model->uuid->toString(),
name: $model->name,
role: new RoleData(
uuid: $model->role->uuid->toString(),
name: $model->role->name,
)
);
}
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
namespace App\Domain\User\Data\ValueObjects;
use Spatie\LaravelData\Data;
class RoleData extends Data
{
public function __construct(
public readonly string $uuid,
public readonly string $name,
) {
}
}

View File

@ -4,8 +4,11 @@ declare(strict_types=1);
namespace App\Models;
use App\Domain\Shared\Casts\UuidCast;
use App\Domain\Shared\ValueObjects\Uuid;
/**
* @property string $uuid
* @property-read Uuid $uuid
* @property string $name
* @property string $description
* @property Carbon $created_at
@ -19,6 +22,12 @@ class Role extends BaseModel
'description',
];
protected $casts = [
'uuid' => UuidCast::class,
'created_at' => 'datetime',
'updated_at' => 'datetime',
];
public function users()
{
return $this->hasMany(User::class);

View File

@ -6,6 +6,9 @@ namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Domain\Shared\Casts\UuidCast;
use App\Domain\Shared\ValueObjects\Uuid;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -13,7 +16,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
/**
* @property string $uuid
* @property Uuid $uuid
* @property string $name
* @property string $email
* @property string $password
@ -59,6 +62,7 @@ class User extends Authenticatable
protected function casts(): array
{
return [
'uuid' => UuidCast::class,
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

View File

@ -1,7 +1,7 @@
@extends ('layouts.app')
@section ('content')
<h1>{{ $data }}</h1>
<h1>{{ dd($data) }}</h1>
<x-common.page-breadcrumb pageTitle="User Profile" />
<div class="rounded-2xl border border-gray-200 bg-white p-5 dark:border-gray-800 dark:bg-white/[0.03] lg:p-6">