File: /mnt/data/ghayatcom/ghayatcom-api/app/Traits/Encryptable.php
<?php
namespace App\Traits;
trait Encryptable
{
public function toArray()
{
$array = parent::toArray();
$encryptable = $this->encryptable ?? [];
foreach ($array as $key => $attribute) {
if (in_array($key, $encryptable) && $array[$key] != '') {
try {
$array[$key] = decrypt($array[$key]);
} catch (\Exception $e) {
return $value;
}
}
}
return $array;
}
public function getAttribute($key)
{
try {
$value = parent::getAttribute($key);
$encryptable = $this->encryptable ?? [];
if (in_array($key, $encryptable) && $value != '') {
$value = decrypt($value);
return $value;
}
return $value;
} catch (\Exception $e) {
return $value;
}
}
public function setAttribute($key, $value)
{
$encryptable = $this->encryptable ?? [];
if (in_array($key, $encryptable)) {
$value = encrypt($value);
}
return parent::setAttribute($key, $value);
}
}