1 | <?php namespace Cviebrock\EloquentTaggable; |
||
16 | trait Taggable |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * Boot the trait. |
||
21 | * |
||
22 | 23 | * Listen for the deleting event of a model, then remove the relation between it and tags |
|
23 | */ |
||
24 | 23 | protected static function bootTaggable() |
|
32 | |||
33 | /** |
||
34 | * Get a collection of all tags the model has. |
||
35 | 23 | * |
|
36 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
37 | 23 | */ |
|
38 | public function tags(): MorphToMany |
||
44 | |||
45 | /** |
||
46 | * Attach one or multiple tags to the model. |
||
47 | * |
||
48 | * @param string|array $tags |
||
49 | * |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function tag($tags) |
||
63 | |||
64 | /** |
||
65 | * Detach one or multiple tags from the model. |
||
66 | * |
||
67 | * @param string|array $tags |
||
68 | * |
||
69 | * @return $this |
||
70 | */ |
||
71 | 1 | public function untag($tags) |
|
81 | 2 | ||
82 | /** |
||
83 | 2 | * Remove all tags from the model and assign the given ones. |
|
84 | * |
||
85 | 2 | * @param string|array $tags |
|
86 | * |
||
87 | * @return $this |
||
88 | */ |
||
89 | public function retag($tags) |
||
93 | 23 | ||
94 | /** |
||
95 | 23 | * Remove all tags from the model. |
|
96 | * |
||
97 | 23 | * @return $this |
|
98 | 23 | */ |
|
99 | 23 | public function detag() |
|
105 | |||
106 | /** |
||
107 | 1 | * Add one tag to the model. |
|
108 | * |
||
109 | 1 | * @param string $tagName |
|
110 | */ |
||
111 | 1 | protected function addOneTag(string $tagName) |
|
120 | |||
121 | /** |
||
122 | 13 | * Remove one tag from the model |
|
123 | * |
||
124 | 2 | * @param string $tagName |
|
125 | 13 | */ |
|
126 | protected function removeOneTag(string $tagName) |
||
134 | |||
135 | 2 | /** |
|
136 | * Get all the tags of the model as a delimited string. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getTagListAttribute(): string |
||
144 | |||
145 | 11 | /** |
|
146 | * Get all normalized tags of a model as a delimited string. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getTagListNormalizedAttribute(): string |
||
154 | |||
155 | 2 | /** |
|
156 | * Get all tags of a model as an array. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | public function getTagArrayAttribute(): array |
||
164 | |||
165 | /** |
||
166 | 1 | * Get all normalized tags of a model as an array. |
|
167 | * |
||
168 | 1 | * @return array |
|
169 | */ |
||
170 | public function getTagArrayNormalizedAttribute(): array |
||
174 | |||
175 | /** |
||
176 | * Query scope for models that have all of the given tags. |
||
177 | * |
||
178 | * @param Builder $query |
||
179 | * @param array|string $tags |
||
180 | * |
||
181 | * @return Builder |
||
182 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
183 | 1 | * @throws \ErrorException |
|
184 | */ |
||
185 | 1 | public function scopeWithAllTags(Builder $query, $tags): Builder |
|
215 | |||
216 | 2 | /** |
|
217 | * Query scope for models that have any of the given tags. |
||
218 | 2 | * |
|
219 | * @param Builder $query |
||
220 | * @param array|string $tags |
||
221 | * |
||
222 | * @return Builder |
||
223 | * @throws \Cviebrock\EloquentTaggable\Exceptions\NoTagsSpecifiedException |
||
224 | * @throws \ErrorException |
||
225 | */ |
||
226 | 1 | public function scopeWithAnyTags(Builder $query, $tags): Builder |
|
249 | |||
250 | /** |
||
251 | * Query scope for models that have any tag. |
||
252 | * |
||
253 | * @param Builder $query |
||
254 | * |
||
255 | * @return Builder |
||
256 | */ |
||
257 | public function scopeIsTagged(Builder $query): Builder |
||
261 | |||
262 | /** |
||
263 | * Query scope for models that do not have all of the given tags. |
||
264 | * |
||
265 | * @param Builder $query |
||
266 | * @param string|array $tags |
||
267 | * @param bool $includeUntagged |
||
268 | * |
||
269 | * @return Builder |
||
270 | * @throws \ErrorException |
||
271 | */ |
||
272 | public function scopeWithoutAllTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
292 | |||
293 | /** |
||
294 | * Query scope for models that do not have any of the given tags. |
||
295 | * |
||
296 | * @param Builder $query |
||
297 | * @param string|array $tags |
||
298 | * @param bool $includeUntagged |
||
299 | * |
||
300 | * @return Builder |
||
301 | * @throws \ErrorException |
||
302 | */ |
||
303 | public function scopeWithoutAnyTags(Builder $query, $tags, bool $includeUntagged = false): Builder |
||
322 | |||
323 | /** |
||
324 | * Query scope for models that does not have have any tags. |
||
325 | * |
||
326 | * @param Builder $query |
||
327 | * |
||
328 | * @return Builder |
||
329 | */ |
||
330 | public function scopeIsNotTagged(Builder $query): Builder |
||
337 | |||
338 | /** |
||
339 | * @param Builder $query |
||
340 | * @param string $joinType |
||
341 | * |
||
342 | * @return Builder |
||
343 | */ |
||
344 | private function prepareTableJoin(Builder $query, string $joinType): Builder |
||
362 | |||
363 | /** |
||
364 | * Get a collection of all the tag models used for the called class. |
||
365 | * |
||
366 | * @return Collection |
||
367 | */ |
||
368 | public static function allTagModels(): Collection |
||
372 | |||
373 | /** |
||
374 | * Get an array of all tags used for the called class. |
||
375 | * |
||
376 | * @return array |
||
377 | */ |
||
378 | public static function allTags(): array |
||
385 | |||
386 | /** |
||
387 | * Get all the tags used for the called class as a delimited string. |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | public static function allTagsList(): string |
||
395 | |||
396 | /** |
||
397 | * Rename one the tags for the called class. |
||
398 | * |
||
399 | * @param string $oldTag |
||
400 | * @param string $newTag |
||
401 | * |
||
402 | * @return int |
||
403 | */ |
||
404 | public static function renameTag(string $oldTag, string $newTag): int |
||
408 | |||
409 | /** |
||
410 | * Get the most popular tags for the called class. |
||
411 | * |
||
412 | * @param int $limit |
||
413 | * @param int $minCount |
||
414 | * |
||
415 | * @return array |
||
416 | */ |
||
417 | public static function popularTags(int $limit = null, int $minCount = 1): array |
||
424 | |||
425 | /** |
||
426 | * Get the most popular tags for the called class. |
||
427 | * |
||
428 | * @param int $limit |
||
429 | * @param int $minCount |
||
430 | * |
||
431 | * @return array |
||
432 | */ |
||
433 | public static function popularTagsNormalized(int $limit = null, int $minCount = 1): array |
||
440 | } |
||
441 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.