|
| 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 | +} |
0 commit comments