55 lines
1.3 KiB
PHP
55 lines
1.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
|
||
|
|
class Template extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
public const TYPES = [
|
||
|
|
'twitter', 'instagram', 'facebook', 'reddit',
|
||
|
|
'tiktok', 'linkedin', 'youtube', 'blog', 'other',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'name',
|
||
|
|
'body',
|
||
|
|
'type',
|
||
|
|
'title',
|
||
|
|
'target',
|
||
|
|
'placeholders',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'placeholders' => 'array',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function applyToPost(array $data): array
|
||
|
|
{
|
||
|
|
$result = [
|
||
|
|
'title' => $this->title,
|
||
|
|
'body' => $this->body,
|
||
|
|
'type' => $this->type,
|
||
|
|
'target' => $this->target,
|
||
|
|
];
|
||
|
|
|
||
|
|
// Replace placeholders in all text fields
|
||
|
|
if ($this->placeholders && is_array($data)) {
|
||
|
|
foreach ($data as $key => $value) {
|
||
|
|
if (in_array($key, $this->placeholders)) {
|
||
|
|
foreach (['title', 'body', 'target'] as $field) {
|
||
|
|
if ($result[$field]) {
|
||
|
|
$result[$field] = str_replace("{{$key}}", $value, $result[$field]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return $result;
|
||
|
|
}
|
||
|
|
}
|