All checks were successful
Deploy Roxane to Preprod / deploy (push) Successful in 1m21s
95 lines
3.6 KiB
PHP
95 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
/**
|
|
* @property int $id
|
|
* @property string $identifier
|
|
* @property string $name
|
|
* @property string $subject
|
|
* @property string $body
|
|
* @property array<array-key, mixed>|null $variables
|
|
* @property bool $is_active
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property \Illuminate\Support\Carbon|null $deleted_at
|
|
* @method static \Database\Factories\NotificationTemplateFactory factory($count = null, $state = [])
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate onlyTrashed()
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereBody($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereDeletedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereIdentifier($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereIsActive($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereName($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereSubject($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereUpdatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate whereVariables($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate withTrashed(bool $withTrashed = true)
|
|
* @method static \Illuminate\Database\Eloquent\Builder<static>|NotificationTemplate withoutTrashed()
|
|
* @mixin \Eloquent
|
|
*/
|
|
class NotificationTemplate extends Model
|
|
{
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'identifier',
|
|
'name',
|
|
'subject',
|
|
'body',
|
|
'variables',
|
|
'is_active',
|
|
];
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'variables' => 'array',
|
|
'is_active' => 'boolean',
|
|
];
|
|
}
|
|
|
|
public static function getAttributeLabel(string $attribute): string
|
|
{
|
|
return __('notification_templates.fields.'.$attribute);
|
|
}
|
|
|
|
public static function findByIdentifier(string $identifier): ?self
|
|
{
|
|
return self::query()
|
|
->where('identifier', $identifier)
|
|
->where('is_active', true)
|
|
->first();
|
|
}
|
|
|
|
public function renderSubject(array $vars): string
|
|
{
|
|
return $this->replacePlaceholders($this->subject, $vars);
|
|
}
|
|
|
|
public function renderBody(array $vars): string
|
|
{
|
|
return $this->replacePlaceholders($this->body, $vars);
|
|
}
|
|
|
|
private function replacePlaceholders(string $text, array $vars): string
|
|
{
|
|
foreach ($vars as $key => $value) {
|
|
$text = str_replace('{'.$key.'}', (string) $value, $text);
|
|
}
|
|
|
|
return $text;
|
|
}
|
|
}
|