Skip to content

Commit 94f1fc5

Browse files
committed
A sizeable refactor making TaggingUtility static and cleaning up other issues
1 parent 07dbd87 commit 94f1fc5

21 files changed

+263
-333
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
}
2828
},
2929
"autoload-dev": {
30-
"classmap": [
31-
"tests/TestCase.php"
32-
]
30+
"psr-4": {
31+
"Tests\\": "tests/"
32+
}
3333
},
3434
"extra": {
3535
"laravel": {

docs/how-to-override-util.md

-29
This file was deleted.

src/Console/Commands/GenerateTagGroup.php

+7-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Conner\Tagging\Console\Commands;
44

5-
use Conner\Tagging\Contracts\TaggingUtility;
5+
use Conner\Tagging\TaggingUtility;
66
use Conner\Tagging\Model\TagGroup;
77
use Illuminate\Console\Command;
88

@@ -22,31 +22,21 @@ class GenerateTagGroup extends Command
2222
*/
2323
protected $description = 'Create a laravel tag group';
2424

25-
/** @var TaggingUtility $taggingUtility */
26-
protected $taggingUtility;
27-
28-
public function __construct(TaggingUtility $taggingUtility)
29-
{
30-
parent::__construct();
31-
32-
$this->taggingUtility = $taggingUtility;
33-
}
34-
3525
/**
3626
* Execute the console command.
3727
*
3828
* @return mixed
3929
*/
4030
public function handle()
4131
{
42-
$group_name = $this->argument('group_name');
32+
$groupName = $this->argument('group_name');
4333

44-
$tag_group = new TagGroup();
45-
$tag_group->name = $group_name;
46-
$tag_group->slug = $this->taggingUtility->slug($group_name);
34+
$tagGroup = new TagGroup();
35+
$tagGroup->name = $groupName;
36+
$tagGroup->slug = TaggingUtility::normalize($groupName);
4737

48-
$tag_group->save();
38+
$tagGroup->save();
4939

50-
$this->info('Created tag group: ' . $group_name);
40+
$this->info('Created tag group: ' . $groupName);
5141
}
5242
}

src/Contracts/TaggingUtility.php

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
<?php namespace Conner\Tagging\Contracts;
1+
<?php
2+
3+
namespace Conner\Tagging\Contracts;
24

35
/**
46
* Interface of utility functions to help with various tagging functionality.
57
*
68
* @copyright Copyright (C) 2015 Robert Conner
7-
* @author Rob Conner <[email protected]>
9+
* @author Robert Conner <[email protected]>
810
*/
911
interface TaggingUtility
1012
{
@@ -17,12 +19,20 @@ interface TaggingUtility
1719
public function makeTagArray($tagNames);
1820

1921
/**
20-
* Create a web friendly URL slug from a string.
22+
* Normalize a tag string
23+
*
24+
* @param string $string
25+
* @return string
26+
*/
27+
public static function normalize($string);
28+
29+
/**
30+
* Create a web friendly URL slug from a string
2131
*
22-
* @param string $str
32+
* @param string $string
2333
* @return string
2434
*/
25-
public static function slug($str);
35+
public static function slug($string);
2636

2737
/**
2838
* Private! Please do not call this function directly, just let the Tag library use it.
@@ -45,7 +55,7 @@ public function incrementCount($tagString, $tagSlug, $count);
4555
public function decrementCount($tagString, $tagSlug, $count);
4656

4757
/**
48-
* Look at the tags table and delete any tags that are no londer in use by any taggable database rows.
58+
* Look at the tags table and delete any tags that are no longer in use by any taggable database rows.
4959
* Does not delete tags where 'suggest' is true
5060
*
5161
* @return int
@@ -57,5 +67,5 @@ public function deleteUnusedTags();
5767
*
5868
* @return string
5969
*/
60-
public function tagModelString();
70+
public static function tagModelString();
6171
}

src/Events/TagAdded.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Conner\Tagging\Events;
22

3+
use Conner\Tagging\Taggable;
34
use Illuminate\Queue\SerializesModels;
45
use Illuminate\Database\Eloquent\Model;
56

@@ -13,10 +14,10 @@ class TagAdded
1314
/**
1415
* Create a new event instance.
1516
*
16-
* @param Model $model
17+
* @param Taggable|Model $model
1718
* @return void
1819
*/
19-
public function __construct(Model $model)
20+
public function __construct($model)
2021
{
2122
$this->model = $model;
2223
}

src/Events/TagRemoved.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
<?php namespace Conner\Tagging\Events;
1+
<?php
22

3-
use Illuminate\Queue\SerializesModels;
3+
namespace Conner\Tagging\Events;
4+
5+
use Conner\Tagging\Taggable;
46
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Queue\SerializesModels;
58

69
class TagRemoved
710
{
811
use SerializesModels;
912

10-
/** @var \Illuminate\Database\Eloquent\Model **/
13+
/** @var \Conner\Tagging\Taggable **/
1114
public $model;
1215

1316
/**
1417
* Create a new event instance.
1518
*
16-
* @param Model $model
19+
* @param Taggable|Model $model
1720
* @return void
1821
*/
19-
public function __construct(Model $model)
22+
public function __construct($model)
2023
{
2124
$this->model = $model;
2225
}

src/Model/Tag.php

+26-28
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
<?php namespace Conner\Tagging\Model;
1+
<?php
22

3-
use Conner\Tagging\Contracts\TaggingUtility;
3+
namespace Conner\Tagging\Model;
4+
5+
use Conner\Tagging\TaggingUtility;
46
use Illuminate\Database\Eloquent\Builder;
5-
use Illuminate\Database\Eloquent\Model as Eloquent;
7+
use Illuminate\Database\Eloquent\Model;
68

79
/**
8-
* Copyright (C) 2014 Robert Conner
910
* @package Conner\Tagging\Model
1011
* @property string id
1112
* @property string name
1213
* @property string slug
1314
* @property bool suggest
1415
* @property integer count
1516
* @property TagGroup group
17+
* @method static suggested()
18+
* @method static inGroup(string $groupName)
1619
*/
17-
class Tag extends Eloquent
20+
class Tag extends Model
1821
{
1922
protected $table = 'tagging_tags';
2023
public $timestamps = false;
21-
protected $softDelete = false;
2224
public $fillable = ['name'];
23-
protected $taggingUtility;
2425

2526
/**
2627
* @param array $attributes
@@ -29,23 +30,20 @@ public function __construct(array $attributes = [])
2930
{
3031
parent::__construct($attributes);
3132

32-
if (function_exists('config') && $connection = config('tagging.connection')) {
33-
$this->connection = $connection;
34-
}
35-
36-
$this->taggingUtility = app(TaggingUtility::class);
33+
$this->connection = config('tagging.connection');
3734
}
3835

36+
/**
37+
* @inheritDoc
38+
*/
3939
public function save(array $options = [])
4040
{
4141
if(strlen($this->name) < 1) {
4242
throw new \RuntimeException('Cannot save a tag with an empty name');
4343
}
4444

45-
$normalizer = config('tagging.normalizer');
46-
$normalizer = $normalizer ?: [$this->taggingUtility, 'slug'];
45+
$this->slug = TaggingUtility::normalize($this->name);
4746

48-
$this->slug = call_user_func($normalizer, $this->name);
4947
return parent::save($options);
5048
}
5149

@@ -56,7 +54,9 @@ public function save(array $options = [])
5654
*/
5755
public function setGroup($groupName)
5856
{
59-
$tagGroup = TagGroup::where('slug', $this->taggingUtility->slug($groupName))->first();
57+
$tagGroup = TagGroup::query()
58+
->where('slug', TaggingUtility::normalize($groupName))
59+
->first();
6060

6161
if ($tagGroup) {
6262
$this->group()->associate($tagGroup);
@@ -75,7 +75,7 @@ public function setGroup($groupName)
7575
*/
7676
public function removeGroup(string $groupName)
7777
{
78-
$tagGroup = TagGroup::query()->where('slug', $this->taggingUtility->slug($groupName))->first();
78+
$tagGroup = TagGroup::query()->where('slug', TaggingUtility::normalize($groupName))->first();
7979

8080
if ($tagGroup) {
8181
$this->group()->dissociate($tagGroup);
@@ -94,9 +94,10 @@ public function removeGroup(string $groupName)
9494
*/
9595
public function isInGroup($groupName): bool
9696
{
97-
if ($this->group && ($this->group->slug == $this->taggingUtility->slug($groupName))) {
97+
if ($this->group && ($this->group->slug == TaggingUtility::normalize($groupName))) {
9898
return true;
9999
}
100+
100101
return false;
101102
}
102103

@@ -120,11 +121,11 @@ public function scopeSuggested($query)
120121
* Get suggested tags
121122
* @param Builder $query
122123
* @param $groupName
123-
* @return
124+
* @return Builder
124125
*/
125126
public function scopeInGroup(Builder $query, $groupName)
126127
{
127-
$groupSlug = $this->taggingUtility->slug($groupName);
128+
$groupSlug = TaggingUtility::normalize($groupName);
128129

129130
return $query->whereHas('group', function (Builder $query) use ($groupSlug) {
130131
$query->where('slug', $groupSlug);
@@ -138,21 +139,18 @@ public function scopeInGroup(Builder $query, $groupName)
138139
*/
139140
public function setNameAttribute(string $value)
140141
{
141-
$displayer = config('tagging.displayer');
142-
$displayer = empty($displayer) ? '\Illuminate\Support\Str::title' : $displayer;
143-
144-
$this->attributes['name'] = call_user_func($displayer, $value);
142+
$this->attributes['name'] = TaggingUtility::displayize($value);
145143
}
146144

147145
/**
148-
* Look at the tags table and delete any tags that are no londer in use by any taggable database rows.
149-
* Does not delete tags where 'suggest'value is true
146+
* Look at the tags table and delete any tags that are no longer in use by any taggable database rows.
147+
* Does not delete tags where 'suggest' value is true
150148
*
151-
* @return int
149+
* @return mixed
152150
*/
153151
public static function deleteUnused()
154152
{
155-
return (new static )->newQuery()
153+
return (new static)->newQuery()
156154
->where('count', '=', 0)
157155
->where('suggest', false)
158156
->delete();

src/Model/TagGroup.php

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
<?php namespace Conner\Tagging\Model;
22

3-
use Conner\Tagging\Contracts\TaggingUtility;
4-
use Illuminate\Database\Eloquent\Model as Eloquent;
3+
use Conner\Tagging\TaggingUtility;
4+
use Illuminate\Database\Eloquent\Model;
55
use Illuminate\Support\Collection;
6-
use Illuminate\Support\Str;
76

87
/**
9-
* Copyright (C) 2014 Robert Conner
108
* @package Conner\Tagging\Model
9+
*
1110
* @property string id
1211
* @property string slug
1312
* @property string name
1413
* @property-read Collection|Tag[] tags
1514
*/
16-
class TagGroup extends Eloquent
15+
class TagGroup extends Model
1716
{
1817
protected $table = 'tagging_tag_groups';
1918
public $timestamps = false;
20-
protected $softDelete = false;
21-
public $fillable = ['name'];
2219

2320
/** @var TaggingUtility $taggingUtility */
2421
protected $taggingUtility;
@@ -30,31 +27,25 @@ public function __construct(array $attributes = [])
3027
{
3128
parent::__construct($attributes);
3229

33-
if (function_exists('config') && $connection = config('tagging.connection')) {
34-
$this->connection = $connection;
35-
}
36-
37-
$this->taggingUtility = app(TaggingUtility::class);
30+
$this->connection = config('tagging.connection');
3831
}
3932

4033
/**
4134
* Get suggested tags
4235
*/
4336
public function tags()
4437
{
45-
$model = $this->taggingUtility->tagModelString();
38+
$model = TaggingUtility::tagModelString();
4639

4740
return $this->hasMany($model, 'tag_group_id');
4841
}
4942

5043
/**
51-
* sets the slug when setting the group name
52-
*
53-
* @return void
44+
* @param $value
5445
*/
5546
public function setNameAttribute($value)
5647
{
5748
$this->attributes['name'] = $value;
58-
$this->attributes['slug'] = Str::slug($value);
49+
$this->attributes['slug'] = TaggingUtility::normalize($value);
5950
}
6051
}

0 commit comments

Comments
 (0)