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