Files
panel/app/Casts/NullableEncrypter.php
2023-11-06 04:20:25 +00:00

31 lines
821 B
PHP

<?php
namespace Convoy\Casts;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
class NullableEncrypter implements CastsAttributes
{
/**
* Cast the given value.
*
* @param array<string, mixed> $attributes
*/
public function get(Model $model, string $key, mixed $value, array $attributes): ?string
{
return !empty($value) ? app(Encrypter::class)->decrypt($value) : null;
}
/**
* Prepare the given value for storage.
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): ?string
{
return !empty($value) ? app(Encrypter::class)->encrypt($value) : null;
}
}