Skip to content

Commit 29b1910

Browse files
committed
More type hintint improvements, handle case of empty tag names
1 parent 9c85a59 commit 29b1910

File tree

3 files changed

+48
-30
lines changed

3 files changed

+48
-30
lines changed

src/Model/Tag.php

+32-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Conner\Tagging\Model;
22

33
use Conner\Tagging\Contracts\TaggingUtility;
4+
use Illuminate\Database\Eloquent\Builder;
45
use Illuminate\Database\Eloquent\Model as Eloquent;
56

67
/**
@@ -11,6 +12,7 @@
1112
* @property string slug
1213
* @property bool suggest
1314
* @property integer count
15+
* @property TagGroup group
1416
*/
1517
class Tag extends Eloquent
1618
{
@@ -34,68 +36,65 @@ public function __construct(array $attributes = [])
3436
$this->taggingUtility = app(TaggingUtility::class);
3537
}
3638

37-
/**
38-
* (non-PHPdoc)
39-
* @see \Illuminate\Database\Eloquent\Model::save()
40-
*/
4139
public function save(array $options = [])
4240
{
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+
}
4744

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'];
5147

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);
5750
}
5851

5952
/**
6053
* Tag group setter
54+
* @param string $groupName
55+
* @return Tag
6156
*/
62-
public function setGroup($group_name)
57+
public function setGroup($groupName)
6358
{
64-
$tagGroup = TagGroup::where('slug', $this->taggingUtility->slug($group_name))->first();
59+
$tagGroup = TagGroup::where('slug', $this->taggingUtility->slug($groupName))->first();
6560

6661
if ($tagGroup) {
6762
$this->group()->associate($tagGroup);
6863
$this->save();
6964

7065
return $this;
7166
} else {
72-
throw new \RuntimeException('No Tag Group found');
67+
throw new \RuntimeException('No Tag Group found: '. $groupName);
7368
}
7469
}
7570

7671
/**
7772
* Tag group remove
73+
* @param string $groupName
74+
* @return Tag
7875
*/
79-
public function removeGroup($group_name)
76+
public function removeGroup(string $groupName)
8077
{
81-
$tagGroup = TagGroup::where('slug', $this->taggingUtility->slug($group_name))->first();
78+
$tagGroup = TagGroup::query()->where('slug', $this->taggingUtility->slug($groupName))->first();
8279

8380
if ($tagGroup) {
8481
$this->group()->dissociate($tagGroup);
8582
$this->save();
8683

8784
return $this;
8885
} else {
89-
throw new \RuntimeException('No Tag Group found');
86+
throw new \RuntimeException('No Tag Group found: '. $groupName);
9087
}
9188
}
9289

9390
/**
9491
* Tag group helper function
92+
* @param string $groupName
93+
* @return bool
9594
*/
96-
public function isInGroup($group_name)
95+
public function isInGroup($groupName): bool
9796
{
98-
if ($this->group && ($this->group->slug == $this->taggingUtility->slug($group_name))) {
97+
if ($this->group && ($this->group->slug == $this->taggingUtility->slug($groupName))) {
9998
return true;
10099
}
101100
return false;
@@ -106,7 +105,7 @@ public function isInGroup($group_name)
106105
*/
107106
public function group()
108107
{
109-
return $this->belongsTo('\Conner\Tagging\Model\TagGroup', 'tag_group_id');
108+
return $this->belongsTo(TagGroup::class, 'tag_group_id');
110109
}
111110

112111
/**
@@ -119,13 +118,16 @@ public function scopeSuggested($query)
119118

120119
/**
121120
* Get suggested tags
121+
* @param Builder $query
122+
* @param $groupName
123+
* @return
122124
*/
123-
public function scopeInGroup($query, $group_name)
125+
public function scopeInGroup(Builder $query, $groupName)
124126
{
125-
$group_slug = $this->taggingUtility->slug($group_name);
127+
$groupSlug = $this->taggingUtility->slug($groupName);
126128

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);
129131
});
130132
}
131133

@@ -134,7 +136,7 @@ public function scopeInGroup($query, $group_name)
134136
*
135137
* @param string $value
136138
*/
137-
public function setNameAttribute($value)
139+
public function setNameAttribute(string $value)
138140
{
139141
$displayer = config('tagging.displayer');
140142
$displayer = empty($displayer) ? '\Illuminate\Support\Str::title' : $displayer;

src/Taggable.php

+4
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ private function addTag($tagName)
278278
{
279279
$tagName = trim($tagName);
280280

281+
if(strlen($tagName) == 0) {
282+
return;
283+
}
284+
281285
$normalizer = config('tagging.normalizer');
282286
$normalizer = $normalizer ?: [static::$taggingUtility, 'slug'];
283287

tests/CommonUsageTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ public function test_setting_tag_names_array()
124124
$this->assertCount(2, $tags);
125125
$this->assertEquals('Foo', $stub->tags[0]->name);
126126
}
127+
128+
public function test_tagging_with_empty_tags()
129+
{
130+
/** @var Stub $stub */
131+
$stub = Stub::create(['name'=>123]);
132+
133+
$tagName = "Japan, Asia, Economy, , , , , ";
134+
135+
$stub->retag($tagName);
136+
137+
$this->assertEquals(['Japan', 'Asia', 'Economy'], $stub->tag_names);
138+
}
127139
}
128140

129141
/**

0 commit comments

Comments
 (0)