2121class TaggableBehavior extends Behavior
2222{
2323 /**
24- * @var boolean whether to return the tag names as array instead of string
24+ * @var boolean whether to return tags as array instead of string
2525 */
26- public $ tagNamesAsArray = false ;
26+ public $ tagValuesAsArray = false ;
2727 /**
2828 * @var string the tags relation name
2929 */
3030 public $ tagRelation = 'tags ' ;
3131 /**
32- * @var string the tags model name attribute name
32+ * @var string the tags model value attribute name
3333 */
34- public $ tagNameAttribute = 'name ' ;
34+ public $ tagValueAttribute = 'name ' ;
3535 /**
3636 * @var string|false the tags model frequency attribute name
3737 */
3838 public $ tagFrequencyAttribute = 'frequency ' ;
3939 /**
4040 * @var string[]
4141 */
42- private $ _tagNames ;
42+ private $ _tagValues ;
4343
4444 /**
4545 * @inheritdoc
@@ -54,70 +54,78 @@ public function events()
5454 }
5555
5656 /**
57- * Returns the tag names .
57+ * Returns tags .
5858 * @param boolean|null $asArray
5959 * @return string|string[]
6060 */
61- public function getTagNames ($ asArray = null )
61+ public function getTagValues ($ asArray = null )
6262 {
63- if (!$ this ->owner ->getIsNewRecord () && $ this ->_tagNames === null ) {
64- $ this ->_tagNames = [];
63+ if (!$ this ->owner ->getIsNewRecord () && $ this ->_tagValues === null ) {
64+ $ this ->_tagValues = [];
6565
6666 /* @var ActiveRecord $tag */
6767 foreach ($ this ->owner ->{$ this ->tagRelation } as $ tag ) {
68- $ this ->_tagNames [] = $ tag ->getAttribute ($ this ->tagNameAttribute );
68+ $ this ->_tagValues [] = $ tag ->getAttribute ($ this ->tagValueAttribute );
6969 }
7070 }
7171
7272 if ($ asArray === null ) {
73- $ asArray = $ this ->tagNamesAsArray ;
73+ $ asArray = $ this ->tagValuesAsArray ;
7474 }
7575
7676 if ($ asArray ) {
77- return $ this ->_tagNames === null ? [] : $ this ->_tagNames ;
77+ return $ this ->_tagValues === null ? [] : $ this ->_tagValues ;
7878 } else {
79- return $ this ->_tagNames === null ? '' : implode (', ' , $ this ->_tagNames );
79+ return $ this ->_tagValues === null ? '' : implode (', ' , $ this ->_tagValues );
8080 }
8181 }
8282
8383 /**
84- * Sets the tag names .
85- * @param string|string[] $names
84+ * Sets tags .
85+ * @param string|string[] $values
8686 */
87- public function setTagNames ( $ names )
87+ public function setTagValues ( $ values )
8888 {
89- $ this ->_tagNames = array_unique ( $ this ->filterTagNames ( $ names ) );
89+ $ this ->_tagValues = $ this ->filterTagValues ( $ values );
9090 }
9191
9292 /**
93- * Adds the tag names .
94- * @param string|string[] $names
93+ * Adds tags .
94+ * @param string|string[] $values
9595 */
96- public function addTagNames ( $ names )
96+ public function addTagValues ( $ values )
9797 {
98- $ this ->_tagNames = array_unique (array_merge ($ this ->getTagNames (true ), $ this ->filterTagNames ( $ names )));
98+ $ this ->_tagValues = array_unique (array_merge ($ this ->getTagValues (true ), $ this ->filterTagValues ( $ values )));
9999 }
100100
101101 /**
102- * Removes the tag names .
103- * @param string|string[] $names
102+ * Removes tags .
103+ * @param string|string[] $values
104104 */
105- public function removeTagNames ( $ names )
105+ public function removeTagValues ( $ values )
106106 {
107- $ this ->_tagNames = array_diff ($ this ->getTagNames (true ), $ this ->filterTagNames ( $ names ));
107+ $ this ->_tagValues = array_diff ($ this ->getTagValues (true ), $ this ->filterTagValues ( $ values ));
108108 }
109109
110110 /**
111- * Returns a value indicating whether the tag names exists.
112- * @param string|string[] $names
111+ * Removes all tags.
112+ */
113+ public function removeAllTagValues ()
114+ {
115+ $ this ->_tagValues = [];
116+ }
117+
118+ /**
119+ * Returns a value indicating whether tags exists.
120+ * @param string|string[] $values
113121 * @return boolean
114122 */
115- public function hasTagNames ( $ names )
123+ public function hasTagValues ( $ values )
116124 {
117- $ tagNames = $ this ->getTagNames (true );
125+ $ tagValues = $ this ->getTagValues (true );
118126
119- foreach ($ this ->filterTagNames ( $ names ) as $ name ) {
120- if (!in_array ($ name , $ tagNames )) {
127+ foreach ($ this ->filterTagValues ( $ values ) as $ value ) {
128+ if (!in_array ($ value , $ tagValues )) {
121129 return false ;
122130 }
123131 }
@@ -130,7 +138,7 @@ public function hasTagNames($names)
130138 */
131139 public function afterSave ()
132140 {
133- if ($ this ->_tagNames === null ) {
141+ if ($ this ->_tagValues === null ) {
134142 return ;
135143 }
136144
@@ -144,13 +152,13 @@ public function afterSave()
144152 $ class = $ tagRelation ->modelClass ;
145153 $ rows = [];
146154
147- foreach ($ this ->_tagNames as $ name ) {
155+ foreach ($ this ->_tagValues as $ value ) {
148156 /* @var ActiveRecord $tag */
149- $ tag = $ class ::findOne ([$ this ->tagNameAttribute => $ name ]);
157+ $ tag = $ class ::findOne ([$ this ->tagValueAttribute => $ value ]);
150158
151159 if ($ tag === null ) {
152160 $ tag = new $ class ();
153- $ tag ->setAttribute ($ this ->tagNameAttribute , $ name );
161+ $ tag ->setAttribute ($ this ->tagValueAttribute , $ value );
154162 }
155163
156164 if ($ this ->tagFrequencyAttribute !== false ) {
@@ -201,17 +209,17 @@ public function beforeDelete()
201209 }
202210
203211 /**
204- * Filters the tag names .
205- * @param string|string[] $names
212+ * Filters tags .
213+ * @param string|string[] $values
206214 * @return string[]
207215 */
208- public function filterTagNames ( $ names )
216+ public function filterTagValues ( $ values )
209217 {
210- return preg_split (
218+ return array_unique ( preg_split (
211219 '/\s*,\s*/u ' ,
212- preg_replace ('/\s+/u ' , ' ' , is_array ($ names ) ? implode (', ' , $ names ) : $ names ),
220+ preg_replace ('/\s+/u ' , ' ' , is_array ($ values ) ? implode (', ' , $ values ) : $ values ),
213221 -1 ,
214222 PREG_SPLIT_NO_EMPTY
215- );
223+ )) ;
216224 }
217225}
0 commit comments