Skip to content

Commit 6ad0a0d

Browse files
committed
WIP: Separating methods into pearate classes
1 parent b14b42e commit 6ad0a0d

File tree

3 files changed

+107
-18
lines changed

3 files changed

+107
-18
lines changed

src/Post.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22

33
namespace Chrisjk123\Blogger;
44

5+
use Chrisjk123\Blogger\Traits\PostAttributes;
6+
use Chrisjk123\Blogger\Traits\PostScopes;
57
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Support\Collection;
78

89
class Post extends Model
910
{
11+
use PostScopes, PostAttributes;
12+
13+
const PUBLISHED = 'published';
14+
const DRAFT = 'draft';
15+
const SCHEDULED = 'scheduled';
16+
1017
protected $table = 'posts';
1118

1219
protected $primaryKey = 'id';
@@ -15,6 +22,8 @@ class Post extends Model
1522

1623
public $timestamps = true;
1724

25+
protected $appends = ['tagsCount'];
26+
1827
public function category()
1928
{
2029
return $this->hasOne(Category::class, 'category_id');
@@ -30,21 +39,8 @@ public function tags()
3039
return $this->morphToMany(Tag::class, 'taggable');
3140
}
3241

33-
public function scopeWhereCategories($query, $categories = null)
34-
{
35-
$query->with('category');
36-
37-
if (is_null($categories)) {
38-
return $query;
39-
}
40-
41-
if ($categories instanceof Collection) {
42-
return $query->whereIn(
43-
'category_id',
44-
$categories->pluck('id')->toArray()
45-
);
46-
}
47-
48-
return $query->where('category_id', $categories->id);
49-
}
42+
// public function path()
43+
// {
44+
// return "/posts/{$this->slug}";
45+
// }
5046
}

src/Traits/PostAttributes.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Chrisjk123\Blogger\Traits;
4+
5+
trait PostAttributes
6+
{
7+
public function getTagsCountAttribute()
8+
{
9+
return $this->tags->count();
10+
}
11+
12+
public function getTimeToReadAttribute()
13+
{
14+
$word_count = str_word_count(strip_tags($this->content));
15+
16+
$minutes = floor($word_count / 200);
17+
$seconds = floor($word_count % 200 / (200 / 60));
18+
19+
$str_minutes = ($minutes == 1) ? 'minute' : 'minutes';
20+
$str_seconds = ($seconds == 1) ? 'second' : 'seconds';
21+
22+
if ($minutes == 0) {
23+
return "{$seconds} {$str_seconds}";
24+
}
25+
26+
return "{$minutes} {$str_minutes}, {$seconds} {$str_seconds}";
27+
}
28+
}

src/Traits/PostScopes.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Chrisjk123\Blogger\Traits;
4+
5+
use Chrisjk123\Blogger\Post;
6+
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Support\Collection;
8+
9+
trait PostScopes
10+
{
11+
public function scopeWhereCategories(Builder $query, $categories = null)
12+
{
13+
$query->with('category');
14+
15+
if (is_null($categories)) {
16+
return $query;
17+
}
18+
19+
if ($categories instanceof Collection) {
20+
return $query->whereIn(
21+
'category_id',
22+
$categories->pluck('id')->toArray()
23+
);
24+
}
25+
26+
return $query->where('category_id', $categories->id);
27+
}
28+
29+
public function scopeWhereCategory(Builder $query, $category, $type = 'slug')
30+
{
31+
$query->with('category');
32+
33+
return $query->whereHas('category', function (Builder $query) use ($type, $category) {
34+
return $query->where($type, $category);
35+
});
36+
}
37+
38+
public function scopeRelatedByPostTags(Builder $query, Post $post)
39+
{
40+
return $query->whereHas('tags', function (Builder $query) use ($post) {
41+
return $query->whereIn('name', $post->tags->pluck('name'));
42+
})->where('id', '!=', $post->id);
43+
}
44+
45+
public function scopeRelatedByPostCategory(Builder $query, Post $post)
46+
{
47+
return $query->whereHas('category', function (Builder $query) use ($post) {
48+
return $query->where('id', $post->category->id);
49+
})->where('id', '!=', $post->id);
50+
}
51+
52+
public function scopeIsPublished(Builder $query)
53+
{
54+
return $query->where('status', self::PUBLISHED)
55+
->whereNotNull('published_at');
56+
}
57+
58+
public function scopeIsNotPublished(Builder $query)
59+
{
60+
return $query->whereIn('status', [
61+
self::DRAFT,
62+
self::SCHEDULED,
63+
])->whereNull('published_at');
64+
}
65+
}

0 commit comments

Comments
 (0)