@@ -5,36 +5,36 @@ |
||
5 | 5 | |
6 | 6 | class CreateTaggableTable extends Migration |
7 | 7 | { |
8 | - /** |
|
9 | - * Run the migrations. |
|
10 | - * |
|
11 | - * @return void |
|
12 | - */ |
|
13 | - public function up() |
|
14 | - { |
|
15 | - Schema::create('taggable_tags', function (Blueprint $table) { |
|
16 | - $table->increments('tag_id'); |
|
17 | - $table->string('name'); |
|
18 | - $table->string('normalized'); |
|
19 | - $table->timestamps(); |
|
20 | - }); |
|
8 | + /** |
|
9 | + * Run the migrations. |
|
10 | + * |
|
11 | + * @return void |
|
12 | + */ |
|
13 | + public function up() |
|
14 | + { |
|
15 | + Schema::create('taggable_tags', function (Blueprint $table) { |
|
16 | + $table->increments('tag_id'); |
|
17 | + $table->string('name'); |
|
18 | + $table->string('normalized'); |
|
19 | + $table->timestamps(); |
|
20 | + }); |
|
21 | 21 | |
22 | - Schema::create('taggable_taggables', function (Blueprint $table) { |
|
23 | - $table->unsignedInteger('tag_id'); |
|
24 | - $table->unsignedInteger('taggable_id'); |
|
25 | - $table->string('taggable_type'); |
|
26 | - $table->timestamps(); |
|
27 | - }); |
|
28 | - } |
|
22 | + Schema::create('taggable_taggables', function (Blueprint $table) { |
|
23 | + $table->unsignedInteger('tag_id'); |
|
24 | + $table->unsignedInteger('taggable_id'); |
|
25 | + $table->string('taggable_type'); |
|
26 | + $table->timestamps(); |
|
27 | + }); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * Reverse the migrations. |
|
32 | - * |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function down() |
|
36 | - { |
|
37 | - Schema::drop('taggable_tags'); |
|
38 | - Schema::drop('taggable_taggables'); |
|
39 | - } |
|
30 | + /** |
|
31 | + * Reverse the migrations. |
|
32 | + * |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function down() |
|
36 | + { |
|
37 | + Schema::drop('taggable_tags'); |
|
38 | + Schema::drop('taggable_taggables'); |
|
39 | + } |
|
40 | 40 | } |
@@ -12,14 +12,14 @@ |
||
12 | 12 | */ |
13 | 13 | public function up() |
14 | 14 | { |
15 | - Schema::create('taggable_tags', function (Blueprint $table) { |
|
15 | + Schema::create('taggable_tags', function(Blueprint $table) { |
|
16 | 16 | $table->increments('tag_id'); |
17 | 17 | $table->string('name'); |
18 | 18 | $table->string('normalized'); |
19 | 19 | $table->timestamps(); |
20 | 20 | }); |
21 | 21 | |
22 | - Schema::create('taggable_taggables', function (Blueprint $table) { |
|
22 | + Schema::create('taggable_taggables', function(Blueprint $table) { |
|
23 | 23 | $table->unsignedInteger('tag_id'); |
24 | 24 | $table->unsignedInteger('taggable_id'); |
25 | 25 | $table->string('taggable_type'); |
@@ -10,43 +10,43 @@ |
||
10 | 10 | */ |
11 | 11 | class ServiceProvider extends LaravelServiceProvider |
12 | 12 | { |
13 | - /** |
|
14 | - * Indicates if loading of the provider is deferred. |
|
15 | - * |
|
16 | - * @var bool |
|
17 | - */ |
|
18 | - protected $defer = false; |
|
19 | - |
|
20 | - /** |
|
21 | - * Bootstrap the application events. |
|
22 | - */ |
|
23 | - public function boot() |
|
24 | - { |
|
25 | - $this->publishes([ |
|
26 | - __DIR__.'/../resources/config/taggable.php' => config_path('taggable.php'), |
|
27 | - ], 'config'); |
|
28 | - |
|
29 | - if (! class_exists('CreateTaggableTable')) { |
|
30 | - // Publish the migration |
|
31 | - $timestamp = date('Y_m_d_His', time()); |
|
32 | - $src = __DIR__.'/../resources/migrations/0000_00_00_000000_create_taggable_table.php'; |
|
33 | - $dst = database_path('migrations/'.$timestamp.'_create_taggable_table.php'); |
|
34 | - |
|
35 | - $this->publishes([ |
|
36 | - $src => $dst, |
|
37 | - ], 'migrations'); |
|
38 | - } |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Register the service provider. |
|
43 | - * |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - public function register() |
|
47 | - { |
|
48 | - $this->mergeConfigFrom(__DIR__.'/../resources/config/taggable.php', 'taggable'); |
|
49 | - |
|
50 | - $this->app->singleton(TagService::class); |
|
51 | - } |
|
13 | + /** |
|
14 | + * Indicates if loading of the provider is deferred. |
|
15 | + * |
|
16 | + * @var bool |
|
17 | + */ |
|
18 | + protected $defer = false; |
|
19 | + |
|
20 | + /** |
|
21 | + * Bootstrap the application events. |
|
22 | + */ |
|
23 | + public function boot() |
|
24 | + { |
|
25 | + $this->publishes([ |
|
26 | + __DIR__.'/../resources/config/taggable.php' => config_path('taggable.php'), |
|
27 | + ], 'config'); |
|
28 | + |
|
29 | + if (! class_exists('CreateTaggableTable')) { |
|
30 | + // Publish the migration |
|
31 | + $timestamp = date('Y_m_d_His', time()); |
|
32 | + $src = __DIR__.'/../resources/migrations/0000_00_00_000000_create_taggable_table.php'; |
|
33 | + $dst = database_path('migrations/'.$timestamp.'_create_taggable_table.php'); |
|
34 | + |
|
35 | + $this->publishes([ |
|
36 | + $src => $dst, |
|
37 | + ], 'migrations'); |
|
38 | + } |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Register the service provider. |
|
43 | + * |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + public function register() |
|
47 | + { |
|
48 | + $this->mergeConfigFrom(__DIR__.'/../resources/config/taggable.php', 'taggable'); |
|
49 | + |
|
50 | + $this->app->singleton(TagService::class); |
|
51 | + } |
|
52 | 52 | } |
@@ -26,7 +26,7 @@ |
||
26 | 26 | __DIR__.'/../resources/config/taggable.php' => config_path('taggable.php'), |
27 | 27 | ], 'config'); |
28 | 28 | |
29 | - if (! class_exists('CreateTaggableTable')) { |
|
29 | + if (!class_exists('CreateTaggableTable')) { |
|
30 | 30 | // Publish the migration |
31 | 31 | $timestamp = date('Y_m_d_His', time()); |
32 | 32 | $src = __DIR__.'/../resources/migrations/0000_00_00_000000_create_taggable_table.php'; |
@@ -10,149 +10,149 @@ |
||
10 | 10 | */ |
11 | 11 | class TagService |
12 | 12 | { |
13 | - /** |
|
14 | - * Find an existing tag by name. |
|
15 | - * |
|
16 | - * @param string $tagName |
|
17 | - * |
|
18 | - * @return \Cviebrock\EloquentTaggable\Models\Tag|null |
|
19 | - */ |
|
20 | - public function find($tagName) |
|
21 | - { |
|
22 | - $normalized = $this->normalize($tagName); |
|
23 | - |
|
24 | - return Tag::where('normalized', $normalized)->first(); |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Find an existing tag (or create a new one) by name. |
|
29 | - * |
|
30 | - * @param string $tagName |
|
31 | - * |
|
32 | - * @return \Cviebrock\EloquentTaggable\Models\Tag |
|
33 | - */ |
|
34 | - public function findOrCreate($tagName) |
|
35 | - { |
|
36 | - $tag = $this->find($tagName); |
|
37 | - |
|
38 | - if (! $tag) { |
|
39 | - $tag = Tag::create(['name' => $tagName]); |
|
40 | - } |
|
41 | - |
|
42 | - return $tag; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Convert a delimited string into an array of tag strings. |
|
47 | - * |
|
48 | - * @param string|array $tags |
|
49 | - * |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - public function buildTagArray($tags) |
|
53 | - { |
|
54 | - if (is_array($tags)) { |
|
55 | - return $tags; |
|
56 | - } |
|
57 | - |
|
58 | - if (is_string($tags)) { |
|
59 | - return preg_split( |
|
60 | - '#['.preg_quote(config('taggable.delimiters'), '#').']#', |
|
61 | - $tags, |
|
62 | - null, |
|
63 | - PREG_SPLIT_NO_EMPTY |
|
64 | - ); |
|
65 | - } |
|
66 | - |
|
67 | - return (array) $tags; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Convert a delimited string into an array of normalized tag strings. |
|
72 | - * |
|
73 | - * @param string|array $tags |
|
74 | - * |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - public function buildTagArrayNormalized($tags) |
|
78 | - { |
|
79 | - $tags = $this->buildTagArray($tags); |
|
80 | - |
|
81 | - return array_map([$this, 'normalize'], $tags); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Build a delimited string from a model's tags. |
|
86 | - * |
|
87 | - * @param \Illuminate\Database\Eloquent\Model $model |
|
88 | - * @param string $field |
|
89 | - * |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public function makeTagList(Model $model, $field = 'name') |
|
93 | - { |
|
94 | - $tags = $this->makeTagArray($model, $field); |
|
95 | - |
|
96 | - return $this->joinList($tags); |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Join a list of strings together using glue. |
|
101 | - * |
|
102 | - * @param array $array |
|
103 | - * |
|
104 | - * @return string |
|
105 | - */ |
|
106 | - public function joinList(array $array) |
|
107 | - { |
|
108 | - return implode(config('taggable.glue'), $array); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Build a simple array of a model's tags. |
|
113 | - * |
|
114 | - * @param \Illuminate\Database\Eloquent\Model $model |
|
115 | - * @param string $field |
|
116 | - * |
|
117 | - * @return array |
|
118 | - */ |
|
119 | - public function makeTagArray(Model $model, $field = 'name') |
|
120 | - { |
|
121 | - /** @var \Illuminate\Database\Eloquent\Collection $tags */ |
|
122 | - $tags = $model->tags; |
|
123 | - |
|
124 | - return $tags->pluck($field)->all(); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Normalize a string. |
|
129 | - * |
|
130 | - * @param string $string |
|
131 | - * |
|
132 | - * @return mixed |
|
133 | - */ |
|
134 | - public function normalize($string) |
|
135 | - { |
|
136 | - return call_user_func(config('taggable.normalizer'), $string); |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Get all Tags for the given class. |
|
141 | - * |
|
142 | - * @param \Illuminate\Database\Eloquent\Model|string $class |
|
143 | - * |
|
144 | - * @return \Illuminate\Database\Eloquent\Collection |
|
145 | - */ |
|
146 | - public function getAllTags($class) |
|
147 | - { |
|
148 | - if ($class instanceof Model) { |
|
149 | - $class = get_class($class); |
|
150 | - } |
|
151 | - |
|
152 | - $sql = 'SELECT DISTINCT t.*'. |
|
153 | - ' FROM taggable_taggables tt LEFT JOIN taggable_tags t ON tt.tag_id=t.tag_id'. |
|
154 | - ' WHERE tt.taggable_type = ?'; |
|
155 | - |
|
156 | - return Tag::hydrateRaw($sql, [$class]); |
|
157 | - } |
|
13 | + /** |
|
14 | + * Find an existing tag by name. |
|
15 | + * |
|
16 | + * @param string $tagName |
|
17 | + * |
|
18 | + * @return \Cviebrock\EloquentTaggable\Models\Tag|null |
|
19 | + */ |
|
20 | + public function find($tagName) |
|
21 | + { |
|
22 | + $normalized = $this->normalize($tagName); |
|
23 | + |
|
24 | + return Tag::where('normalized', $normalized)->first(); |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Find an existing tag (or create a new one) by name. |
|
29 | + * |
|
30 | + * @param string $tagName |
|
31 | + * |
|
32 | + * @return \Cviebrock\EloquentTaggable\Models\Tag |
|
33 | + */ |
|
34 | + public function findOrCreate($tagName) |
|
35 | + { |
|
36 | + $tag = $this->find($tagName); |
|
37 | + |
|
38 | + if (! $tag) { |
|
39 | + $tag = Tag::create(['name' => $tagName]); |
|
40 | + } |
|
41 | + |
|
42 | + return $tag; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Convert a delimited string into an array of tag strings. |
|
47 | + * |
|
48 | + * @param string|array $tags |
|
49 | + * |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + public function buildTagArray($tags) |
|
53 | + { |
|
54 | + if (is_array($tags)) { |
|
55 | + return $tags; |
|
56 | + } |
|
57 | + |
|
58 | + if (is_string($tags)) { |
|
59 | + return preg_split( |
|
60 | + '#['.preg_quote(config('taggable.delimiters'), '#').']#', |
|
61 | + $tags, |
|
62 | + null, |
|
63 | + PREG_SPLIT_NO_EMPTY |
|
64 | + ); |
|
65 | + } |
|
66 | + |
|
67 | + return (array) $tags; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Convert a delimited string into an array of normalized tag strings. |
|
72 | + * |
|
73 | + * @param string|array $tags |
|
74 | + * |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + public function buildTagArrayNormalized($tags) |
|
78 | + { |
|
79 | + $tags = $this->buildTagArray($tags); |
|
80 | + |
|
81 | + return array_map([$this, 'normalize'], $tags); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Build a delimited string from a model's tags. |
|
86 | + * |
|
87 | + * @param \Illuminate\Database\Eloquent\Model $model |
|
88 | + * @param string $field |
|
89 | + * |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public function makeTagList(Model $model, $field = 'name') |
|
93 | + { |
|
94 | + $tags = $this->makeTagArray($model, $field); |
|
95 | + |
|
96 | + return $this->joinList($tags); |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Join a list of strings together using glue. |
|
101 | + * |
|
102 | + * @param array $array |
|
103 | + * |
|
104 | + * @return string |
|
105 | + */ |
|
106 | + public function joinList(array $array) |
|
107 | + { |
|
108 | + return implode(config('taggable.glue'), $array); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Build a simple array of a model's tags. |
|
113 | + * |
|
114 | + * @param \Illuminate\Database\Eloquent\Model $model |
|
115 | + * @param string $field |
|
116 | + * |
|
117 | + * @return array |
|
118 | + */ |
|
119 | + public function makeTagArray(Model $model, $field = 'name') |
|
120 | + { |
|
121 | + /** @var \Illuminate\Database\Eloquent\Collection $tags */ |
|
122 | + $tags = $model->tags; |
|
123 | + |
|
124 | + return $tags->pluck($field)->all(); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Normalize a string. |
|
129 | + * |
|
130 | + * @param string $string |
|
131 | + * |
|
132 | + * @return mixed |
|
133 | + */ |
|
134 | + public function normalize($string) |
|
135 | + { |
|
136 | + return call_user_func(config('taggable.normalizer'), $string); |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Get all Tags for the given class. |
|
141 | + * |
|
142 | + * @param \Illuminate\Database\Eloquent\Model|string $class |
|
143 | + * |
|
144 | + * @return \Illuminate\Database\Eloquent\Collection |
|
145 | + */ |
|
146 | + public function getAllTags($class) |
|
147 | + { |
|
148 | + if ($class instanceof Model) { |
|
149 | + $class = get_class($class); |
|
150 | + } |
|
151 | + |
|
152 | + $sql = 'SELECT DISTINCT t.*'. |
|
153 | + ' FROM taggable_taggables tt LEFT JOIN taggable_tags t ON tt.tag_id=t.tag_id'. |
|
154 | + ' WHERE tt.taggable_type = ?'; |
|
155 | + |
|
156 | + return Tag::hydrateRaw($sql, [$class]); |
|
157 | + } |
|
158 | 158 | } |
@@ -35,7 +35,7 @@ |
||
35 | 35 | { |
36 | 36 | $tag = $this->find($tagName); |
37 | 37 | |
38 | - if (! $tag) { |
|
38 | + if (!$tag) { |
|
39 | 39 | $tag = Tag::create(['name' => $tagName]); |
40 | 40 | } |
41 | 41 |
@@ -12,217 +12,217 @@ |
||
12 | 12 | */ |
13 | 13 | trait Taggable |
14 | 14 | { |
15 | - /** |
|
16 | - * Get a Collection of all Tags a Model has. |
|
17 | - * |
|
18 | - * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
19 | - */ |
|
20 | - public function tags() |
|
21 | - { |
|
22 | - return $this->morphToMany(Tag::class, 'taggable', 'taggable_taggables') |
|
23 | - ->withTimestamps(); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Attach one or multiple Tags to a Model. |
|
28 | - * |
|
29 | - * @param string|array $tags |
|
30 | - * |
|
31 | - * @return $this |
|
32 | - */ |
|
33 | - public function tag($tags) |
|
34 | - { |
|
35 | - $tags = app(TagService::class)->buildTagArray($tags); |
|
36 | - |
|
37 | - foreach ($tags as $tagName) { |
|
38 | - $this->addOneTag($tagName); |
|
39 | - } |
|
40 | - |
|
41 | - return $this->load('tags'); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Detach one or multiple Tags from a Model. |
|
46 | - * |
|
47 | - * @param string|array $tags |
|
48 | - * |
|
49 | - * @return $this |
|
50 | - */ |
|
51 | - public function untag($tags) |
|
52 | - { |
|
53 | - $tags = app(TagService::class)->buildTagArray($tags); |
|
54 | - |
|
55 | - foreach ($tags as $tagName) { |
|
56 | - $this->removeOneTag($tagName); |
|
57 | - } |
|
58 | - |
|
59 | - return $this->load('tags'); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Remove all Tags from a Model and assign the given ones. |
|
64 | - * |
|
65 | - * @param string|array $tags |
|
66 | - * |
|
67 | - * @return $this |
|
68 | - */ |
|
69 | - public function retag($tags) |
|
70 | - { |
|
71 | - return $this->detag()->tag($tags); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Remove all tags from the model. |
|
76 | - * |
|
77 | - * @return $this |
|
78 | - */ |
|
79 | - public function detag() |
|
80 | - { |
|
81 | - $this->tags()->sync([]); |
|
82 | - |
|
83 | - return $this->load('tags'); |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Add one tag to the model. |
|
88 | - * |
|
89 | - * @param string $tagName |
|
90 | - */ |
|
91 | - protected function addOneTag($tagName) |
|
92 | - { |
|
93 | - $tag = app(TagService::class)->findOrCreate($tagName); |
|
94 | - |
|
95 | - if (! $this->tags->contains($tag->getKey())) { |
|
96 | - $this->tags()->attach($tag->getKey()); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Remove one tag from the model. |
|
102 | - * |
|
103 | - * @param string $tagName |
|
104 | - */ |
|
105 | - protected function removeOneTag($tagName) |
|
106 | - { |
|
107 | - $tag = app(TagService::class)->find($tagName); |
|
108 | - |
|
109 | - if ($tag) { |
|
110 | - $this->tags()->detach($tag); |
|
111 | - } |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Get all tags of a Model as a string in which the tags are delimited |
|
116 | - * by the character defined in config('taggable.delimiters'). |
|
117 | - * |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function getTagListAttribute() |
|
121 | - { |
|
122 | - return app(TagService::class)->makeTagList($this); |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Get all normalized tags of a Model as a string in which the tags are delimited |
|
127 | - * by the character defined in config('taggable.delimiters'). |
|
128 | - * |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function getTagListNormalizedAttribute() |
|
132 | - { |
|
133 | - return app(TagService::class)->makeTagList($this, 'normalized'); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get all tags of a Model as an array. |
|
138 | - * |
|
139 | - * @return array |
|
140 | - */ |
|
141 | - public function getTagArrayAttribute() |
|
142 | - { |
|
143 | - return app(TagService::class)->makeTagArray($this); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Get all normalized tags of a Model as an array. |
|
148 | - * |
|
149 | - * @return array |
|
150 | - */ |
|
151 | - public function getTagArrayNormalizedAttribute() |
|
152 | - { |
|
153 | - return app(TagService::class)->makeTagArray($this, 'normalized'); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Scope for a Model that has all of the given tags. |
|
158 | - * |
|
159 | - * @param \Illuminate\Database\Eloquent\Builder $query |
|
160 | - * @param array|string $tags |
|
161 | - * |
|
162 | - * @return \Illuminate\Database\Eloquent\Builder |
|
163 | - */ |
|
164 | - public function scopeWithAllTags(Builder $query, $tags) |
|
165 | - { |
|
166 | - $normalized = app(TagService::class)->buildTagArrayNormalized($tags); |
|
167 | - |
|
168 | - return $query->has('tags', '=', count($normalized), 'and', function (Builder $q) use ($normalized) { |
|
169 | - $q->whereIn('normalized', $normalized); |
|
170 | - }); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Scope for a Model that has any of the given tags. |
|
175 | - * |
|
176 | - * @param \Illuminate\Database\Eloquent\Builder $query |
|
177 | - * @param array $tags |
|
178 | - * |
|
179 | - * @return \Illuminate\Database\Eloquent\Builder |
|
180 | - */ |
|
181 | - public function scopeWithAnyTags(Builder $query, $tags = []) |
|
182 | - { |
|
183 | - $normalized = app(TagService::class)->buildTagArrayNormalized($tags); |
|
184 | - |
|
185 | - if (empty($normalized)) { |
|
186 | - return $query->has('tags'); |
|
187 | - } |
|
188 | - |
|
189 | - return $query->has('tags', '>', 0, 'and', function (Builder $q) use ($normalized) { |
|
190 | - $q->whereIn('normalized', $normalized); |
|
191 | - }); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Scope for a Model that doesn't have any tags. |
|
196 | - * |
|
197 | - * @param \Illuminate\Database\Eloquent\Builder $query |
|
198 | - * |
|
199 | - * @return \Illuminate\Database\Eloquent\Builder |
|
200 | - */ |
|
201 | - public function scopeWithoutTags(Builder $query) |
|
202 | - { |
|
203 | - return $query->has('tags', '=', 0); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Get an array of all tags used for the called class. |
|
208 | - * |
|
209 | - * @return array |
|
210 | - */ |
|
211 | - public static function allTags() |
|
212 | - { |
|
213 | - /** @var Collection $tags */ |
|
214 | - $tags = app(TagService::class)->getAllTags(get_called_class()); |
|
215 | - |
|
216 | - return $tags->pluck('name')->sort()->all(); |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Get all the tags used for the called class as a delimited string. |
|
221 | - * |
|
222 | - * @return string |
|
223 | - */ |
|
224 | - public static function allTagsList() |
|
225 | - { |
|
226 | - return app(TagService::class)->joinList(static::allTags()); |
|
227 | - } |
|
15 | + /** |
|
16 | + * Get a Collection of all Tags a Model has. |
|
17 | + * |
|
18 | + * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
|
19 | + */ |
|
20 | + public function tags() |
|
21 | + { |
|
22 | + return $this->morphToMany(Tag::class, 'taggable', 'taggable_taggables') |
|
23 | + ->withTimestamps(); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Attach one or multiple Tags to a Model. |
|
28 | + * |
|
29 | + * @param string|array $tags |
|
30 | + * |
|
31 | + * @return $this |
|
32 | + */ |
|
33 | + public function tag($tags) |
|
34 | + { |
|
35 | + $tags = app(TagService::class)->buildTagArray($tags); |
|
36 | + |
|
37 | + foreach ($tags as $tagName) { |
|
38 | + $this->addOneTag($tagName); |
|
39 | + } |
|
40 | + |
|
41 | + return $this->load('tags'); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Detach one or multiple Tags from a Model. |
|
46 | + * |
|
47 | + * @param string|array $tags |
|
48 | + * |
|
49 | + * @return $this |
|
50 | + */ |
|
51 | + public function untag($tags) |
|
52 | + { |
|
53 | + $tags = app(TagService::class)->buildTagArray($tags); |
|
54 | + |
|
55 | + foreach ($tags as $tagName) { |
|
56 | + $this->removeOneTag($tagName); |
|
57 | + } |
|
58 | + |
|
59 | + return $this->load('tags'); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Remove all Tags from a Model and assign the given ones. |
|
64 | + * |
|
65 | + * @param string|array $tags |
|
66 | + * |
|
67 | + * @return $this |
|
68 | + */ |
|
69 | + public function retag($tags) |
|
70 | + { |
|
71 | + return $this->detag()->tag($tags); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Remove all tags from the model. |
|
76 | + * |
|
77 | + * @return $this |
|
78 | + */ |
|
79 | + public function detag() |
|
80 | + { |
|
81 | + $this->tags()->sync([]); |
|
82 | + |
|
83 | + return $this->load('tags'); |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Add one tag to the model. |
|
88 | + * |
|
89 | + * @param string $tagName |
|
90 | + */ |
|
91 | + protected function addOneTag($tagName) |
|
92 | + { |
|
93 | + $tag = app(TagService::class)->findOrCreate($tagName); |
|
94 | + |
|
95 | + if (! $this->tags->contains($tag->getKey())) { |
|
96 | + $this->tags()->attach($tag->getKey()); |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Remove one tag from the model. |
|
102 | + * |
|
103 | + * @param string $tagName |
|
104 | + */ |
|
105 | + protected function removeOneTag($tagName) |
|
106 | + { |
|
107 | + $tag = app(TagService::class)->find($tagName); |
|
108 | + |
|
109 | + if ($tag) { |
|
110 | + $this->tags()->detach($tag); |
|
111 | + } |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Get all tags of a Model as a string in which the tags are delimited |
|
116 | + * by the character defined in config('taggable.delimiters'). |
|
117 | + * |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function getTagListAttribute() |
|
121 | + { |
|
122 | + return app(TagService::class)->makeTagList($this); |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Get all normalized tags of a Model as a string in which the tags are delimited |
|
127 | + * by the character defined in config('taggable.delimiters'). |
|
128 | + * |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function getTagListNormalizedAttribute() |
|
132 | + { |
|
133 | + return app(TagService::class)->makeTagList($this, 'normalized'); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get all tags of a Model as an array. |
|
138 | + * |
|
139 | + * @return array |
|
140 | + */ |
|
141 | + public function getTagArrayAttribute() |
|
142 | + { |
|
143 | + return app(TagService::class)->makeTagArray($this); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Get all normalized tags of a Model as an array. |
|
148 | + * |
|
149 | + * @return array |
|
150 | + */ |
|
151 | + public function getTagArrayNormalizedAttribute() |
|
152 | + { |
|
153 | + return app(TagService::class)->makeTagArray($this, 'normalized'); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Scope for a Model that has all of the given tags. |
|
158 | + * |
|
159 | + * @param \Illuminate\Database\Eloquent\Builder $query |
|
160 | + * @param array|string $tags |
|
161 | + * |
|
162 | + * @return \Illuminate\Database\Eloquent\Builder |
|
163 | + */ |
|
164 | + public function scopeWithAllTags(Builder $query, $tags) |
|
165 | + { |
|
166 | + $normalized = app(TagService::class)->buildTagArrayNormalized($tags); |
|
167 | + |
|
168 | + return $query->has('tags', '=', count($normalized), 'and', function (Builder $q) use ($normalized) { |
|
169 | + $q->whereIn('normalized', $normalized); |
|
170 | + }); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Scope for a Model that has any of the given tags. |
|
175 | + * |
|
176 | + * @param \Illuminate\Database\Eloquent\Builder $query |
|
177 | + * @param array $tags |
|
178 | + * |
|
179 | + * @return \Illuminate\Database\Eloquent\Builder |
|
180 | + */ |
|
181 | + public function scopeWithAnyTags(Builder $query, $tags = []) |
|
182 | + { |
|
183 | + $normalized = app(TagService::class)->buildTagArrayNormalized($tags); |
|
184 | + |
|
185 | + if (empty($normalized)) { |
|
186 | + return $query->has('tags'); |
|
187 | + } |
|
188 | + |
|
189 | + return $query->has('tags', '>', 0, 'and', function (Builder $q) use ($normalized) { |
|
190 | + $q->whereIn('normalized', $normalized); |
|
191 | + }); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Scope for a Model that doesn't have any tags. |
|
196 | + * |
|
197 | + * @param \Illuminate\Database\Eloquent\Builder $query |
|
198 | + * |
|
199 | + * @return \Illuminate\Database\Eloquent\Builder |
|
200 | + */ |
|
201 | + public function scopeWithoutTags(Builder $query) |
|
202 | + { |
|
203 | + return $query->has('tags', '=', 0); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Get an array of all tags used for the called class. |
|
208 | + * |
|
209 | + * @return array |
|
210 | + */ |
|
211 | + public static function allTags() |
|
212 | + { |
|
213 | + /** @var Collection $tags */ |
|
214 | + $tags = app(TagService::class)->getAllTags(get_called_class()); |
|
215 | + |
|
216 | + return $tags->pluck('name')->sort()->all(); |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Get all the tags used for the called class as a delimited string. |
|
221 | + * |
|
222 | + * @return string |
|
223 | + */ |
|
224 | + public static function allTagsList() |
|
225 | + { |
|
226 | + return app(TagService::class)->joinList(static::allTags()); |
|
227 | + } |
|
228 | 228 | } |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | { |
93 | 93 | $tag = app(TagService::class)->findOrCreate($tagName); |
94 | 94 | |
95 | - if (! $this->tags->contains($tag->getKey())) { |
|
95 | + if (!$this->tags->contains($tag->getKey())) { |
|
96 | 96 | $this->tags()->attach($tag->getKey()); |
97 | 97 | } |
98 | 98 | } |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | { |
166 | 166 | $normalized = app(TagService::class)->buildTagArrayNormalized($tags); |
167 | 167 | |
168 | - return $query->has('tags', '=', count($normalized), 'and', function (Builder $q) use ($normalized) { |
|
168 | + return $query->has('tags', '=', count($normalized), 'and', function(Builder $q) use ($normalized) { |
|
169 | 169 | $q->whereIn('normalized', $normalized); |
170 | 170 | }); |
171 | 171 | } |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | return $query->has('tags'); |
187 | 187 | } |
188 | 188 | |
189 | - return $query->has('tags', '>', 0, 'and', function (Builder $q) use ($normalized) { |
|
189 | + return $query->has('tags', '>', 0, 'and', function(Builder $q) use ($normalized) { |
|
190 | 190 | $q->whereIn('normalized', $normalized); |
191 | 191 | }); |
192 | 192 | } |
@@ -10,49 +10,49 @@ |
||
10 | 10 | */ |
11 | 11 | class Tag extends Eloquent |
12 | 12 | { |
13 | - /** |
|
14 | - * The table associated with the model. |
|
15 | - * |
|
16 | - * @var string |
|
17 | - */ |
|
18 | - protected $table = 'taggable_tags'; |
|
13 | + /** |
|
14 | + * The table associated with the model. |
|
15 | + * |
|
16 | + * @var string |
|
17 | + */ |
|
18 | + protected $table = 'taggable_tags'; |
|
19 | 19 | |
20 | - /** |
|
21 | - * The primary key for the model. |
|
22 | - * |
|
23 | - * @var string |
|
24 | - */ |
|
25 | - protected $primaryKey = 'tag_id'; |
|
20 | + /** |
|
21 | + * The primary key for the model. |
|
22 | + * |
|
23 | + * @var string |
|
24 | + */ |
|
25 | + protected $primaryKey = 'tag_id'; |
|
26 | 26 | |
27 | - /** |
|
28 | - * The attributes that are mass assignable. |
|
29 | - * |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $fillable = [ |
|
33 | - 'name', |
|
34 | - 'normalized', |
|
35 | - ]; |
|
27 | + /** |
|
28 | + * The attributes that are mass assignable. |
|
29 | + * |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $fillable = [ |
|
33 | + 'name', |
|
34 | + 'normalized', |
|
35 | + ]; |
|
36 | 36 | |
37 | - /** |
|
38 | - * Set the name attribute on the model. |
|
39 | - * |
|
40 | - * @param string $value |
|
41 | - */ |
|
42 | - public function setNameAttribute($value) |
|
43 | - { |
|
44 | - $value = trim($value); |
|
45 | - $this->attributes['name'] = $value; |
|
46 | - $this->attributes['normalized'] = app(TagService::class)->normalize($value); |
|
47 | - } |
|
37 | + /** |
|
38 | + * Set the name attribute on the model. |
|
39 | + * |
|
40 | + * @param string $value |
|
41 | + */ |
|
42 | + public function setNameAttribute($value) |
|
43 | + { |
|
44 | + $value = trim($value); |
|
45 | + $this->attributes['name'] = $value; |
|
46 | + $this->attributes['normalized'] = app(TagService::class)->normalize($value); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Convert the model to its string representation. |
|
51 | - * |
|
52 | - * @return string |
|
53 | - */ |
|
54 | - public function __toString() |
|
55 | - { |
|
56 | - return $this->getAttribute('name'); |
|
57 | - } |
|
49 | + /** |
|
50 | + * Convert the model to its string representation. |
|
51 | + * |
|
52 | + * @return string |
|
53 | + */ |
|
54 | + public function __toString() |
|
55 | + { |
|
56 | + return $this->getAttribute('name'); |
|
57 | + } |
|
58 | 58 | } |