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