MicroFrameWork/kernel/modules/post/service/PostService.php
2024-10-01 12:20:01 +03:00

41 lines
1.1 KiB
PHP

<?php
namespace kernel\modules\post\service;
use kernel\helpers\Debug;
use kernel\helpers\Slug;
use kernel\modules\post\models\Post;
use kernel\FormModel;
class PostService
{
public function create(FormModel $form_model): false|Post
{
$model = new Post();
$model->content = $form_model->getItem('content');
$model->user_id = $form_model->getItem('user_id');
$model->title = $form_model->getItem('title');
$model->slug = Slug::recursiveCreateSlug(Post::class, $this->createSlug($form_model));
if ($model->save()){
return $model;
}
return false;
}
public function createSlug(FormModel $form_model): string
{
return Slug::url_slug($form_model->getItem('title'), ['transliterate' => true, 'lowercase' => true]);
}
public function update(FormModel $form_model, Post $post): false|Post
{
$post->content = $form_model->getItem('content');
$post->user_id = $form_model->getItem('user_id');
if ($post->save()){
return $post;
}
return false;
}
}