1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Models/Traits/HasCustomMessages.php

50 lines
976 B
PHP
Raw Normal View History

2018-04-09 13:45:38 +02:00
<?php
namespace App\Models\Traits;
/**
* Class HasCustomMessages.
*/
trait HasCustomMessages
{
/**
* @param $value
*/
public function setCustomMessagesAttribute($data)
{
$fields = [];
if (! is_array($data)) {
$data = json_decode($data);
}
foreach ($data as $key => $value) {
if ($value) {
$fields[$key] = $value;
}
}
$this->attributes['custom_messages'] = count($fields) ? json_encode($fields) : null;
}
public function getCustomMessagesAttribute($value)
{
return json_decode($value ?: '{}');
}
public function customMessage($type)
{
$messages = $this->custom_messages;
if (! empty($messages->$type)) {
return $messages->$type;
}
if ($this->account) {
return $this->account->customMessage($type);
} else {
return '';
}
}
}