1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Tags; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection as DbCollection; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Spatie\EloquentSortable\Sortable; |
9
|
|
|
use Spatie\EloquentSortable\SortableTrait; |
10
|
|
|
use Spatie\Translatable\HasTranslations; |
11
|
|
|
|
12
|
|
|
class Tag extends Model implements Sortable |
13
|
|
|
{ |
14
|
|
|
use SortableTrait, HasTranslations, HasSlug; |
15
|
|
|
|
16
|
|
|
public $translatable = ['name', 'slug']; |
17
|
|
|
|
18
|
|
|
public $guarded = []; |
19
|
|
|
|
20
|
|
|
public function scopeWithType(Builder $query, string $type = null): Builder |
21
|
|
|
{ |
22
|
|
|
if (is_null($type)) { |
23
|
|
|
return $query; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $query->where('type', $type)->ordered(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function scopeContaining(Builder $query, string $name, $locale = null): Builder |
30
|
|
|
{ |
31
|
|
|
$locale = $locale ?? app()->getLocale(); |
32
|
|
|
|
33
|
|
|
return $query->whereRaw('lower('.$this->getQuery()->getGrammar()->wrap('name->'.$locale).') like ?', ['%'.mb_strtolower($name).'%']); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string|array|\ArrayAccess $values |
38
|
|
|
* @param string|null $type |
39
|
|
|
* @param string|null $locale |
40
|
|
|
* |
41
|
|
|
* @return \Spatie\Tags\Tag|static |
42
|
|
|
*/ |
43
|
|
|
public static function findOrCreate($values, string $type = null, string $locale = null) |
44
|
|
|
{ |
45
|
|
|
$tags = collect($values)->map(function ($value) use ($type, $locale) { |
46
|
|
|
if ($value instanceof self) { |
47
|
|
|
return $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return static::findOrCreateFromString($value, $type, $locale); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
return is_string($values) ? $tags->first() : $tags; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function getWithType(string $type): DbCollection |
57
|
|
|
{ |
58
|
|
|
return static::withType($type)->ordered()->get(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
public static function findFromString(string $name, string $type = null, string $locale = null) |
62
|
|
|
{ |
63
|
|
|
$locale = $locale ?? app()->getLocale(); |
64
|
|
|
|
65
|
|
|
return static::query() |
66
|
|
|
->where("name->{$locale}", $name) |
67
|
|
|
->where('type', $type) |
68
|
|
|
->first(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public static function findFromStringOfAnyType(string $name, string $locale = null) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$locale = $locale ?? app()->getLocale(); |
74
|
|
|
|
75
|
|
|
return static::query() |
76
|
|
|
->where("name->{$locale}", $name) |
77
|
|
|
->first(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected static function findOrCreateFromString(string $name, string $type = null, string $locale = null) |
81
|
|
|
{ |
82
|
|
|
$locale = $locale ?? app()->getLocale(); |
83
|
|
|
|
84
|
|
|
$tag = static::findFromString($name, $type, $locale); |
85
|
|
|
|
86
|
|
|
if (! $tag) { |
87
|
|
|
$tag = static::create([ |
88
|
|
|
'name' => [$locale => $name], |
89
|
|
|
'type' => $type, |
90
|
|
|
]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $tag; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setAttribute($key, $value) |
97
|
|
|
{ |
98
|
|
|
if ($key === 'name' && ! is_array($value)) { |
99
|
|
|
return $this->setTranslation($key, app()->getLocale(), $value); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return parent::setAttribute($key, $value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.