From 5944dac0534322ff2fed1850aca05991e986d1d6 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Tue, 1 Oct 2024 16:00:14 +0300 Subject: [PATCH 1/2] Create slug --- kernel/helpers/Slug.php | 13 ++++++++++++- kernel/modules/post/models/Post.php | 1 - kernel/modules/post/service/PostService.php | 8 ++------ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/kernel/helpers/Slug.php b/kernel/helpers/Slug.php index 423e37e..aab7264 100644 --- a/kernel/helpers/Slug.php +++ b/kernel/helpers/Slug.php @@ -2,6 +2,7 @@ namespace kernel\helpers; +use Illuminate\Database\Eloquent\Model; use kernel\FormModel; use kernel\modules\post\models\Post; @@ -126,7 +127,17 @@ class Slug return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str; } - public static function recursiveCreateSlug($model, $slug, $tmpSlug = '', $id = 1): string + public static function createSlug(string $title, $model = null): string + { + $slug = Slug::url_slug($title, ['transliterate' => true, 'lowercase' => true]); + if ($model === null) { + return $slug; + } + + return Slug::recursiveCreateSlug($model, $slug); + } + + protected static function recursiveCreateSlug($model, string $slug, string $tmpSlug = '', int $id = 1): string { if ($tmpSlug === '') $tmpSlug = $slug; if ($model::where(['slug' => $tmpSlug])->exists()) { diff --git a/kernel/modules/post/models/Post.php b/kernel/modules/post/models/Post.php index b3482d1..76dc634 100644 --- a/kernel/modules/post/models/Post.php +++ b/kernel/modules/post/models/Post.php @@ -9,7 +9,6 @@ use kernel\modules\user\models\User; * @property string $content * @property string $title * @property string $slug -// * @property string $username * @property int $user_id * @method static where(int[] $array) * @method static find($id) diff --git a/kernel/modules/post/service/PostService.php b/kernel/modules/post/service/PostService.php index 764b6b7..7b3abad 100644 --- a/kernel/modules/post/service/PostService.php +++ b/kernel/modules/post/service/PostService.php @@ -15,7 +15,8 @@ class PostService $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)); +// $model->slug = Slug::recursiveCreateSlug(Post::class, $this->createSlug($form_model)); + $model->slug = Slug::createSlug($form_model->getItem('title'), Post::class); if ($model->save()){ return $model; } @@ -23,11 +24,6 @@ class PostService 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'); From a26eeb9441adba453db3005a82c640684a541740 Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Mon, 7 Oct 2024 11:29:56 +0300 Subject: [PATCH 2/2] fix slug --- kernel/helpers/Slug.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/helpers/Slug.php b/kernel/helpers/Slug.php index aab7264..0bde8c1 100644 --- a/kernel/helpers/Slug.php +++ b/kernel/helpers/Slug.php @@ -127,6 +127,11 @@ class Slug return $options['lowercase'] ? mb_strtolower($str, 'UTF-8') : $str; } + /** + * @param string $title + * @param $model + * @return string + */ public static function createSlug(string $title, $model = null): string { $slug = Slug::url_slug($title, ['transliterate' => true, 'lowercase' => true]); @@ -137,7 +142,14 @@ class Slug return Slug::recursiveCreateSlug($model, $slug); } - protected static function recursiveCreateSlug($model, string $slug, string $tmpSlug = '', int $id = 1): string + /** + * @param $model + * @param string $slug + * @param string $tmpSlug + * @param int $id + * @return string + */ + private static function recursiveCreateSlug($model, string $slug, string $tmpSlug = '', int $id = 1): string { if ($tmpSlug === '') $tmpSlug = $slug; if ($model::where(['slug' => $tmpSlug])->exists()) {