<?php

namespace App\Casts;

use App\Enums\UserRole;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class RoleCast implements CastsAttributes
{
    public function get($model, string $key, $value, array $attributes)
    {
        return UserRole::from($value);
    }

    public function set($model, string $key, $value, array $attributes)
    {
        return $value instanceof UserRole ? $value->value : $value;
    }
}
