@@ -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,149 +13,149 @@ |
||
| 13 | 13 | class TagService |
| 14 | 14 | { |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Find an existing tag by name. |
|
| 18 | - * |
|
| 19 | - * @param string $tagName |
|
| 20 | - * |
|
| 21 | - * @return \Cviebrock\EloquentTaggable\Models\Tag|null |
|
| 22 | - */ |
|
| 23 | - public function find($tagName) |
|
| 24 | - { |
|
| 25 | - $normalized = $this->normalize($tagName); |
|
| 26 | - |
|
| 27 | - return Tag::where('normalized', $normalized)->first(); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Find an existing tag (or create a new one) by name. |
|
| 32 | - * |
|
| 33 | - * @param string $tagName |
|
| 34 | - * |
|
| 35 | - * @return \Cviebrock\EloquentTaggable\Models\Tag |
|
| 36 | - */ |
|
| 37 | - public function findOrCreate($tagName) |
|
| 38 | - { |
|
| 39 | - $tag = $this->find($tagName); |
|
| 40 | - |
|
| 41 | - if (!$tag) { |
|
| 42 | - $tag = Tag::create(['name' => $tagName]); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - return $tag; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Convert a delimited string into an array of tag strings. |
|
| 50 | - * |
|
| 51 | - * @param string|array $tags |
|
| 52 | - * |
|
| 53 | - * @return array |
|
| 54 | - */ |
|
| 55 | - public function buildTagArray($tags) |
|
| 56 | - { |
|
| 57 | - if (is_array($tags)) { |
|
| 58 | - return $tags; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - if (is_string($tags)) { |
|
| 62 | - return preg_split( |
|
| 63 | - '#[' . preg_quote(config('taggable.delimiters'), '#') . ']#', |
|
| 64 | - $tags, |
|
| 65 | - null, |
|
| 66 | - PREG_SPLIT_NO_EMPTY |
|
| 67 | - ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - return (array)$tags; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Convert a delimited string into an array of normalized tag strings. |
|
| 75 | - * |
|
| 76 | - * @param string|array $tags |
|
| 77 | - * |
|
| 78 | - * @return array |
|
| 79 | - */ |
|
| 80 | - public function buildTagArrayNormalized($tags) |
|
| 81 | - { |
|
| 82 | - $tags = $this->buildTagArray($tags); |
|
| 83 | - |
|
| 84 | - return array_map([$this, 'normalize'], $tags); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Build a delimited string from a model's tags. |
|
| 89 | - * |
|
| 90 | - * @param \Illuminate\Database\Eloquent\Model $model |
|
| 91 | - * @param string $field |
|
| 92 | - * |
|
| 93 | - * @return string |
|
| 94 | - */ |
|
| 95 | - public function makeTagList(Model $model, $field = 'name') |
|
| 96 | - { |
|
| 97 | - $tags = $this->makeTagArray($model, $field); |
|
| 98 | - |
|
| 99 | - return $this->joinList($tags); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Join a list of strings together using glue. |
|
| 104 | - * |
|
| 105 | - * @param array $array |
|
| 106 | - * |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function joinList(array $array) |
|
| 110 | - { |
|
| 111 | - return implode(config('taggable.glue'), $array); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Build a simple array of a model's tags. |
|
| 116 | - * |
|
| 117 | - * @param \Illuminate\Database\Eloquent\Model $model |
|
| 118 | - * @param string $field |
|
| 119 | - * |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function makeTagArray(Model $model, $field = 'name') |
|
| 123 | - { |
|
| 124 | - /** @var \Illuminate\Database\Eloquent\Collection $tags */ |
|
| 125 | - $tags = $model->tags; |
|
| 126 | - |
|
| 127 | - return $tags->pluck($field)->all(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Normalize a string. |
|
| 132 | - * |
|
| 133 | - * @param string $string |
|
| 134 | - * |
|
| 135 | - * @return mixed |
|
| 136 | - */ |
|
| 137 | - public function normalize($string) |
|
| 138 | - { |
|
| 139 | - return call_user_func(config('taggable.normalizer'), $string); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get all Tags for the given class. |
|
| 144 | - * |
|
| 145 | - * @param \Illuminate\Database\Eloquent\Model|string $class |
|
| 146 | - * |
|
| 147 | - * @return \Illuminate\Database\Eloquent\Collection |
|
| 148 | - */ |
|
| 149 | - public function getAllTags($class) |
|
| 150 | - { |
|
| 151 | - if ($class instanceof Model) { |
|
| 152 | - $class = get_class($class); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $sql = 'SELECT DISTINCT t.*' . |
|
| 156 | - ' FROM taggable_taggables tt LEFT JOIN taggable_tags t ON tt.tag_id=t.tag_id' . |
|
| 157 | - ' WHERE tt.taggable_type = ?'; |
|
| 158 | - |
|
| 159 | - return Tag::hydrateRaw($sql, [$class]); |
|
| 160 | - } |
|
| 16 | + /** |
|
| 17 | + * Find an existing tag by name. |
|
| 18 | + * |
|
| 19 | + * @param string $tagName |
|
| 20 | + * |
|
| 21 | + * @return \Cviebrock\EloquentTaggable\Models\Tag|null |
|
| 22 | + */ |
|
| 23 | + public function find($tagName) |
|
| 24 | + { |
|
| 25 | + $normalized = $this->normalize($tagName); |
|
| 26 | + |
|
| 27 | + return Tag::where('normalized', $normalized)->first(); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Find an existing tag (or create a new one) by name. |
|
| 32 | + * |
|
| 33 | + * @param string $tagName |
|
| 34 | + * |
|
| 35 | + * @return \Cviebrock\EloquentTaggable\Models\Tag |
|
| 36 | + */ |
|
| 37 | + public function findOrCreate($tagName) |
|
| 38 | + { |
|
| 39 | + $tag = $this->find($tagName); |
|
| 40 | + |
|
| 41 | + if (!$tag) { |
|
| 42 | + $tag = Tag::create(['name' => $tagName]); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + return $tag; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Convert a delimited string into an array of tag strings. |
|
| 50 | + * |
|
| 51 | + * @param string|array $tags |
|
| 52 | + * |
|
| 53 | + * @return array |
|
| 54 | + */ |
|
| 55 | + public function buildTagArray($tags) |
|
| 56 | + { |
|
| 57 | + if (is_array($tags)) { |
|
| 58 | + return $tags; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + if (is_string($tags)) { |
|
| 62 | + return preg_split( |
|
| 63 | + '#[' . preg_quote(config('taggable.delimiters'), '#') . ']#', |
|
| 64 | + $tags, |
|
| 65 | + null, |
|
| 66 | + PREG_SPLIT_NO_EMPTY |
|
| 67 | + ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + return (array)$tags; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Convert a delimited string into an array of normalized tag strings. |
|
| 75 | + * |
|
| 76 | + * @param string|array $tags |
|
| 77 | + * |
|
| 78 | + * @return array |
|
| 79 | + */ |
|
| 80 | + public function buildTagArrayNormalized($tags) |
|
| 81 | + { |
|
| 82 | + $tags = $this->buildTagArray($tags); |
|
| 83 | + |
|
| 84 | + return array_map([$this, 'normalize'], $tags); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Build a delimited string from a model's tags. |
|
| 89 | + * |
|
| 90 | + * @param \Illuminate\Database\Eloquent\Model $model |
|
| 91 | + * @param string $field |
|
| 92 | + * |
|
| 93 | + * @return string |
|
| 94 | + */ |
|
| 95 | + public function makeTagList(Model $model, $field = 'name') |
|
| 96 | + { |
|
| 97 | + $tags = $this->makeTagArray($model, $field); |
|
| 98 | + |
|
| 99 | + return $this->joinList($tags); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Join a list of strings together using glue. |
|
| 104 | + * |
|
| 105 | + * @param array $array |
|
| 106 | + * |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function joinList(array $array) |
|
| 110 | + { |
|
| 111 | + return implode(config('taggable.glue'), $array); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Build a simple array of a model's tags. |
|
| 116 | + * |
|
| 117 | + * @param \Illuminate\Database\Eloquent\Model $model |
|
| 118 | + * @param string $field |
|
| 119 | + * |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function makeTagArray(Model $model, $field = 'name') |
|
| 123 | + { |
|
| 124 | + /** @var \Illuminate\Database\Eloquent\Collection $tags */ |
|
| 125 | + $tags = $model->tags; |
|
| 126 | + |
|
| 127 | + return $tags->pluck($field)->all(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Normalize a string. |
|
| 132 | + * |
|
| 133 | + * @param string $string |
|
| 134 | + * |
|
| 135 | + * @return mixed |
|
| 136 | + */ |
|
| 137 | + public function normalize($string) |
|
| 138 | + { |
|
| 139 | + return call_user_func(config('taggable.normalizer'), $string); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get all Tags for the given class. |
|
| 144 | + * |
|
| 145 | + * @param \Illuminate\Database\Eloquent\Model|string $class |
|
| 146 | + * |
|
| 147 | + * @return \Illuminate\Database\Eloquent\Collection |
|
| 148 | + */ |
|
| 149 | + public function getAllTags($class) |
|
| 150 | + { |
|
| 151 | + if ($class instanceof Model) { |
|
| 152 | + $class = get_class($class); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $sql = 'SELECT DISTINCT t.*' . |
|
| 156 | + ' FROM taggable_taggables tt LEFT JOIN taggable_tags t ON tt.tag_id=t.tag_id' . |
|
| 157 | + ' WHERE tt.taggable_type = ?'; |
|
| 158 | + |
|
| 159 | + return Tag::hydrateRaw($sql, [$class]); |
|
| 160 | + } |
|
| 161 | 161 | } |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | if (is_string($tags)) { |
| 62 | 62 | return preg_split( |
| 63 | - '#[' . preg_quote(config('taggable.delimiters'), '#') . ']#', |
|
| 63 | + '#['.preg_quote(config('taggable.delimiters'), '#').']#', |
|
| 64 | 64 | $tags, |
| 65 | 65 | null, |
| 66 | 66 | PREG_SPLIT_NO_EMPTY |
| 67 | 67 | ); |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - return (array)$tags; |
|
| 70 | + return (array) $tags; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -152,8 +152,8 @@ discard block |
||
| 152 | 152 | $class = get_class($class); |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - $sql = 'SELECT DISTINCT t.*' . |
|
| 156 | - ' FROM taggable_taggables tt LEFT JOIN taggable_tags t ON tt.tag_id=t.tag_id' . |
|
| 155 | + $sql = 'SELECT DISTINCT t.*'. |
|
| 156 | + ' FROM taggable_taggables tt LEFT JOIN taggable_tags t ON tt.tag_id=t.tag_id'. |
|
| 157 | 157 | ' WHERE tt.taggable_type = ?'; |
| 158 | 158 | |
| 159 | 159 | 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 | } |