1
1
<?php namespace Conner \Tagging \Model ;
2
2
3
3
use Conner \Tagging \Contracts \TaggingUtility ;
4
+ use Illuminate \Database \Eloquent \Builder ;
4
5
use Illuminate \Database \Eloquent \Model as Eloquent ;
5
6
6
7
/**
11
12
* @property string slug
12
13
* @property bool suggest
13
14
* @property integer count
15
+ * @property TagGroup group
14
16
*/
15
17
class Tag extends Eloquent
16
18
{
@@ -34,68 +36,65 @@ public function __construct(array $attributes = [])
34
36
$ this ->taggingUtility = app (TaggingUtility::class);
35
37
}
36
38
37
- /**
38
- * (non-PHPdoc)
39
- * @see \Illuminate\Database\Eloquent\Model::save()
40
- */
41
39
public function save (array $ options = [])
42
40
{
43
- $ validator = app ('validator ' )->make (
44
- array ('name ' => $ this ->name ),
45
- array ('name ' => 'required|min:1 ' )
46
- );
41
+ if (strlen ($ this ->name ) < 1 ) {
42
+ throw new \RuntimeException ('Cannot save a tag with an empty name ' );
43
+ }
47
44
48
- if ($ validator ->passes ()) {
49
- $ normalizer = config ('tagging.normalizer ' );
50
- $ normalizer = $ normalizer ?: [$ this ->taggingUtility , 'slug ' ];
45
+ $ normalizer = config ('tagging.normalizer ' );
46
+ $ normalizer = $ normalizer ?: [$ this ->taggingUtility , 'slug ' ];
51
47
52
- $ this ->slug = call_user_func ($ normalizer , $ this ->name );
53
- return parent ::save ($ options );
54
- } else {
55
- throw new \RuntimeException ('Tag Name is required ' );
56
- }
48
+ $ this ->slug = call_user_func ($ normalizer , $ this ->name );
49
+ return parent ::save ($ options );
57
50
}
58
51
59
52
/**
60
53
* Tag group setter
54
+ * @param string $groupName
55
+ * @return Tag
61
56
*/
62
- public function setGroup ($ group_name )
57
+ public function setGroup ($ groupName )
63
58
{
64
- $ tagGroup = TagGroup::where ('slug ' , $ this ->taggingUtility ->slug ($ group_name ))->first ();
59
+ $ tagGroup = TagGroup::where ('slug ' , $ this ->taggingUtility ->slug ($ groupName ))->first ();
65
60
66
61
if ($ tagGroup ) {
67
62
$ this ->group ()->associate ($ tagGroup );
68
63
$ this ->save ();
69
64
70
65
return $ this ;
71
66
} else {
72
- throw new \RuntimeException ('No Tag Group found ' );
67
+ throw new \RuntimeException ('No Tag Group found: ' . $ groupName );
73
68
}
74
69
}
75
70
76
71
/**
77
72
* Tag group remove
73
+ * @param string $groupName
74
+ * @return Tag
78
75
*/
79
- public function removeGroup ($ group_name )
76
+ public function removeGroup (string $ groupName )
80
77
{
81
- $ tagGroup = TagGroup::where ('slug ' , $ this ->taggingUtility ->slug ($ group_name ))->first ();
78
+ $ tagGroup = TagGroup::query ()-> where ('slug ' , $ this ->taggingUtility ->slug ($ groupName ))->first ();
82
79
83
80
if ($ tagGroup ) {
84
81
$ this ->group ()->dissociate ($ tagGroup );
85
82
$ this ->save ();
86
83
87
84
return $ this ;
88
85
} else {
89
- throw new \RuntimeException ('No Tag Group found ' );
86
+ throw new \RuntimeException ('No Tag Group found: ' . $ groupName );
90
87
}
91
88
}
92
89
93
90
/**
94
91
* Tag group helper function
92
+ * @param string $groupName
93
+ * @return bool
95
94
*/
96
- public function isInGroup ($ group_name )
95
+ public function isInGroup ($ groupName ): bool
97
96
{
98
- if ($ this ->group && ($ this ->group ->slug == $ this ->taggingUtility ->slug ($ group_name ))) {
97
+ if ($ this ->group && ($ this ->group ->slug == $ this ->taggingUtility ->slug ($ groupName ))) {
99
98
return true ;
100
99
}
101
100
return false ;
@@ -106,7 +105,7 @@ public function isInGroup($group_name)
106
105
*/
107
106
public function group ()
108
107
{
109
- return $ this ->belongsTo (' \Conner\Tagging\Model\ TagGroup' , 'tag_group_id ' );
108
+ return $ this ->belongsTo (TagGroup::class , 'tag_group_id ' );
110
109
}
111
110
112
111
/**
@@ -119,13 +118,16 @@ public function scopeSuggested($query)
119
118
120
119
/**
121
120
* Get suggested tags
121
+ * @param Builder $query
122
+ * @param $groupName
123
+ * @return
122
124
*/
123
- public function scopeInGroup ($ query , $ group_name )
125
+ public function scopeInGroup (Builder $ query , $ groupName )
124
126
{
125
- $ group_slug = $ this ->taggingUtility ->slug ($ group_name );
127
+ $ groupSlug = $ this ->taggingUtility ->slug ($ groupName );
126
128
127
- return $ query ->whereHas ('group ' , function ($ query ) use ($ group_slug ) {
128
- $ query ->where ('slug ' , $ group_slug );
129
+ return $ query ->whereHas ('group ' , function (Builder $ query ) use ($ groupSlug ) {
130
+ $ query ->where ('slug ' , $ groupSlug );
129
131
});
130
132
}
131
133
@@ -134,7 +136,7 @@ public function scopeInGroup($query, $group_name)
134
136
*
135
137
* @param string $value
136
138
*/
137
- public function setNameAttribute ($ value )
139
+ public function setNameAttribute (string $ value )
138
140
{
139
141
$ displayer = config ('tagging.displayer ' );
140
142
$ displayer = empty ($ displayer ) ? '\Illuminate\Support\Str::title ' : $ displayer ;
0 commit comments