Skip to content

Commit c72a1f8

Browse files
committed
Moved classes around
1 parent 5c8f9b4 commit c72a1f8

14 files changed

+350
-254
lines changed

README.md

100755100644
File mode changed.

src/Builders/Builder.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Builders;
4+
5+
use Carbon\Carbon;
6+
use Chriscreates\Blog\Exceptions\ColumnNotFoundException;
7+
use Illuminate\Database\Eloquent\Builder as BaseBuilder;
8+
use Illuminate\Support\Facades\Schema;
9+
10+
class Builder extends BaseBuilder
11+
{
12+
/**
13+
* Return results where created_at between now and last month.
14+
*
15+
* @return \Chriscreates\Blog\Builders\Builder
16+
*/
17+
public function lastMonth() : Builder
18+
{
19+
return $this->whereBetween('created_at', [
20+
Carbon::now()->subMonth(), Carbon::now(),
21+
])->latest();
22+
}
23+
24+
/**
25+
* Return results where created_at between now and last week.
26+
*
27+
* @return \Chriscreates\Blog\Builders\Builder
28+
*/
29+
public function lastWeek() : Builder
30+
{
31+
return $this->whereBetween('created_at', [
32+
Carbon::now()->subWeek(), Carbon::now(),
33+
])->latest();
34+
}
35+
36+
/**
37+
* Return results where slug matches.
38+
*
39+
* @param string $status
40+
* @return \Chriscreates\Blog\Builders\Builder
41+
*/
42+
public function slug(string $slug) : Builder
43+
{
44+
// Check whether queried table has 'slug' column
45+
if ( ! Schema::hasColumn($this->model->getTable(), 'slug')) {
46+
$message = "Column 'slug' not found in {$this->model->getTable()} table";
47+
48+
throw new ColumnNotFoundException($message);
49+
}
50+
51+
return $this->where('slug', $slug);
52+
}
53+
}

src/Builders/CommentBuilder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Builders;
4+
5+
class CommentBuilder extends Builder
6+
{
7+
/**
8+
* Return results where Comments have been approved.
9+
*
10+
* @return \Chriscreates\Blog\Builders\CommentBuilder
11+
*/
12+
public function approved() : CommentBuilder
13+
{
14+
return $this->where('is_approved', true);
15+
}
16+
17+
/**
18+
* Return results where Comments are not yet approved.
19+
*
20+
* @return \Chriscreates\Blog\Builders\CommentBuilder
21+
*/
22+
public function unapproved() : CommentBuilder
23+
{
24+
return $this->where('is_approved', false);
25+
}
26+
}

src/Builders/PostBuilder.php

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Builders;
4+
5+
use Carbon\Carbon;
6+
use Chriscreates\Blog\Category;
7+
use Chriscreates\Blog\Post;
8+
use Illuminate\Support\Collection;
9+
10+
class PostBuilder extends Builder
11+
{
12+
/**
13+
* Return results where Posts have status.
14+
*
15+
* @param string $status
16+
* @return \Chriscreates\Blog\Builders\PostBuilder
17+
*/
18+
public function status(string $status) : PostBuilder
19+
{
20+
return $this->where('status', $status);
21+
}
22+
23+
/**
24+
* Return results where Posts have been published.
25+
*
26+
* @return \Chriscreates\Blog\Builders\PostBuilder
27+
*/
28+
public function published() : PostBuilder
29+
{
30+
return $this->whereIn('status', [Post::PUBLISHED, Post::SCHEDULED])
31+
->where('published_at', '<=', Carbon::now());
32+
}
33+
34+
/**
35+
* Return results where Posts have been scheduled to be published.
36+
*
37+
* @return \Chriscreates\Blog\Builders\PostBuilder
38+
*/
39+
public function scheduled() : PostBuilder
40+
{
41+
return $this->where(function ($query) {
42+
return $query->where('status', Post::SCHEDULED)
43+
->where('published_at', '>', Carbon::now());
44+
});
45+
}
46+
47+
/**
48+
* Return results where Posts are drafted.
49+
*
50+
* @return \Chriscreates\Blog\Builders\PostBuilder
51+
*/
52+
public function draft() : PostBuilder
53+
{
54+
return $this->where(function ($query) {
55+
return $query->where('status', Post::DRAFT)
56+
->whereNull('published_at');
57+
});
58+
}
59+
60+
/**
61+
* Return results where Posts are not yet published.
62+
*
63+
* @return \Chriscreates\Blog\Builders\PostBuilder
64+
*/
65+
public function notPublished() : PostBuilder
66+
{
67+
return $this->where(function ($query) {
68+
return $query->draft();
69+
})->orWhere(function ($query) {
70+
return $query->scheduled();
71+
});
72+
}
73+
74+
/**
75+
* Order Post results by latest published.
76+
*
77+
* @return \Chriscreates\Blog\Builders\PostBuilder
78+
*/
79+
public function orderByLatest() : PostBuilder
80+
{
81+
return $this->orderBy('published_at', 'DESC');
82+
}
83+
84+
/**
85+
* Return results where Posts have been published last month.
86+
*
87+
* @return \Chriscreates\Blog\Builders\PostBuilder
88+
*/
89+
public function publishedLastMonth() : PostBuilder
90+
{
91+
return $this->whereBetween('published_at', [
92+
Carbon::now()->subMonth(), Carbon::now(),
93+
])->orderByLatest()->limit($limit);
94+
}
95+
96+
/**
97+
* Return results where Posts have been published last week.
98+
*
99+
* @return \Chriscreates\Blog\Builders\PostBuilder
100+
*/
101+
public function publishedLastWeek() : PostBuilder
102+
{
103+
return $this->whereBetween('published_at', [
104+
Carbon::now()->subWeek(), Carbon::now(),
105+
])->orderByLatest();
106+
}
107+
108+
/**
109+
* Return results where Posts are related by the passed in Post Tags.
110+
*
111+
* @param \Chriscreates\Blog\Post $post
112+
* @return \Chriscreates\Blog\Builders\PostBuilder
113+
*/
114+
public function relatedByPostTags(Post $post) : PostBuilder
115+
{
116+
return $this->whereHas('tags', function ($query) use ($post) {
117+
return $query->whereIn('name', $post->tags->pluck('name'));
118+
})->where('id', '!=', $post->id);
119+
}
120+
121+
/**
122+
* Return results where Posts are related by the passed in Post Category.
123+
*
124+
* @param \Chriscreates\Blog\Post $post
125+
* @return \Chriscreates\Blog\Builders\PostBuilder
126+
*/
127+
public function relatedByPostCategory(Post $post) : PostBuilder
128+
{
129+
return $this->whereHas('category', function ($query) use ($post) {
130+
return $query->where('id', $post->category->id);
131+
})->where('id', '!=', $post->id);
132+
}
133+
134+
/**
135+
* Return results where Posts contain the Category(s) passed.
136+
*
137+
* @param $categories
138+
* @return \Chriscreates\Blog\Builders\PostBuilder
139+
*/
140+
public function whereCategories($categories = null) : PostBuilder
141+
{
142+
// search by category name
143+
if (is_string($categories)) {
144+
return $this->whereCategory('name', $categories);
145+
}
146+
147+
// search by category id
148+
if (is_int($categories)) {
149+
return $this->whereCategory('id', $categories);
150+
}
151+
152+
// search by multiple categories
153+
if (is_array($categories)) {
154+
if (is_int($categories[0])) {
155+
$field = 'id';
156+
} else {
157+
$field = 'name';
158+
}
159+
160+
return $this->whereCategory($field, $categories);
161+
}
162+
163+
// search by category model
164+
if ($categories instanceof Category) {
165+
return $this->whereCategory('id', $categories->id);
166+
}
167+
168+
// search by categories collection
169+
if ($categories instanceof Collection) {
170+
return $this->whereCategory('id', $categories->pluck('id')->toArray());
171+
}
172+
173+
return $this;
174+
}
175+
176+
/**
177+
* Return results where Posts contain the Category(s) passed.
178+
*
179+
* @param array $options
180+
* @return \Chriscreates\Blog\Builders\PostBuilder
181+
*/
182+
public function whereCategory(...$options) : PostBuilder
183+
{
184+
$collection = collect([
185+
'field' => 'id',
186+
'operator' => '=',
187+
'value' => null,
188+
]);
189+
190+
// Search by field and value
191+
if (count($options) == 2) {
192+
$collection = $collection->replace(['field' => $options[0]])
193+
->replace(['value' => $options[1]]);
194+
}
195+
196+
// Search by field, operator and value
197+
if (count($options) == 3) {
198+
$collection = $collection->replace(['field' => $options[0]])
199+
->replace(['operator' => $options[1]])
200+
->replace(['value' => $options[2]]);
201+
}
202+
203+
// $this->with('category');
204+
205+
if (is_array($collection['value'])) {
206+
return $this->whereHas(
207+
'category',
208+
function ($query) use ($collection) {
209+
return $query->whereIn(
210+
$collection['field'],
211+
$collection['value']
212+
);
213+
}
214+
);
215+
}
216+
217+
return $this->whereHas(
218+
'category',
219+
function ($query) use ($collection) {
220+
return $query->where(
221+
$collection['field'],
222+
$collection['operator'],
223+
$collection['value']
224+
);
225+
}
226+
);
227+
}
228+
}

src/Category.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class Category extends Model
1010

1111
protected $primaryKey = 'id';
1212

13-
public $guarded = [];
13+
public $guarded = ['id'];
1414

1515
public $timestamps = true;
1616

1717
public function posts()
1818
{
19-
return $this->hasMany(Post::class);
19+
return $this->hasMany(Post::class, 'category_id', 'id');
2020
}
2121
}

src/Comment.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22

33
namespace Chriscreates\Blog;
44

5+
use Chriscreates\Blog\Builders\CommentBuilder;
56
use Chriscreates\Blog\Traits\Comment\CommentApproval;
6-
use Chriscreates\Blog\Traits\Comment\CommentScopes;
77
use Chriscreates\Blog\Traits\IsAuthorable;
88
use Illuminate\Database\Eloquent\Model;
99

1010
class Comment extends Model
1111
{
12-
use CommentScopes,
13-
CommentApproval,
12+
use CommentApproval,
1413
IsAuthorable;
1514

1615
protected $table = 'comments';
1716

1817
protected $primaryKey = 'id';
1918

20-
public $guarded = [];
19+
public $guarded = ['id'];
2120

2221
public $timestamps = true;
2322

@@ -29,4 +28,23 @@ public function commentable()
2928
{
3029
return $this->morphTo();
3130
}
31+
32+
public function approve()
33+
{
34+
$this->update(['is_approved' => true]);
35+
36+
return $this;
37+
}
38+
39+
public function disapprove()
40+
{
41+
$this->update(['is_approved' => false]);
42+
43+
return $this;
44+
}
45+
46+
public function newEloquentBuilder($query) : CommentBuilder
47+
{
48+
return new CommentBuilder($query);
49+
}
3250
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Chriscreates\Blog\Exceptions;
4+
5+
use Exception;
6+
7+
class ColumnNotFoundException extends Exception
8+
{
9+
// ---
10+
}

0 commit comments

Comments
 (0)