Completed
Push — master ( 8ca1bc...de6553 )
by Colin
01:08
created

Tag::taggedModels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php namespace Cviebrock\EloquentTaggable\Models;
2
3
use Cviebrock\EloquentTaggable\Services\TagService;
4
use Illuminate\Database\Eloquent\Builder;
5
use Illuminate\Database\Eloquent\Model;
6
7
8
/**
9
 * Class Tag
10
 */
11
class Tag extends Model
12
{
13
14
    /**
15
     * @inheritdoc
16
     */
17
    protected $table = 'taggable_tags';
18
19
    /**
20
     * @inheritdoc
21
     */
22
    protected $primaryKey = 'tag_id';
23
24
    /**
25
     * @inheritdoc
26
     */
27
    protected $fillable = [
28
        'name',
29
        'normalized',
30
    ];
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function __construct(array $attributes = [])
36
    {
37
        if ($connection = config('taggable.connection')) {
38
            $this->setConnection($connection);
39
        }
40
41
        parent::__construct($attributes);
42
    }
43
44 23
    /**
45
     * Get the inverse of the polymorphic relation, via an attribute
46 23
     * defining the type of models to return.
47 23
     *
48 23
     * @param string $class
49 23
     *
50
     * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
51
     */
52
    public function taggedModels($class)
53
    {
54
        return $this->morphedByMany($class, 'taggable', 'taggable_taggables', 'tag_id');
55
    }
56
57
    /**
58
     * Set the name attribute on the model.
59
     *
60
     * @param string $value
61
     */
62
    public function setNameAttribute($value)
63
    {
64
        $value = trim($value);
65
        $this->attributes['name'] = $value;
66
        $this->attributes['normalized'] = app(TagService::class)->normalize($value);
67
    }
68
69
    /**
70
     * Find the tag with the given name.
71
     *
72
     * @param string $value
73
     *
74
     * @return static|null
75
     */
76
    public static function findByName($value)
77
    {
78
        return static::byName($value)->first();
0 ignored issues
show
Bug introduced by
The method byName() does not exist on Cviebrock\EloquentTaggable\Models\Tag. Did you maybe mean findByName()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
79
    }
80
81
    /**
82
     * Scope to find tags by name.
83
     *
84
     * @param \Illuminate\Database\Eloquent\Builder $query
85
     * @param $value
86
     *
87
     * @return \Illuminate\Database\Eloquent\Builder
88
     */
89
    public function scopeByName(Builder $query, $value)
90
    {
91
        $normalized = app(TagService::class)->normalize($value);
92
93
        return $query->where('normalized', $normalized);
94
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99
    public function getRelationValue($key)
100
    {
101
        // Check for regular relation first
102
        if ($return = parent::getRelationValue($key)) {
103
            return $return;
104
        }
105
106
        // Check if the relation is defined via configuration
107
        $relatedClass = array_get(config('taggable.taggedModels'), $key);
108
109
        if ($relatedClass) {
110
            $relation = $this->morphedByMany($relatedClass, 'taggable', 'taggable_taggables', 'tag_id');
111
112
            return tap($relation->getResults(), function($results) use ($key) {
113
                $this->setRelation($key, $results);
114
            });
115
        }
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121
    public function __toString()
122
    {
123
        return (string) $this->getAttribute('name');
124
    }
125
126
}
127