|
1
|
|
|
<?php namespace Cviebrock\EloquentSluggable; |
|
2
|
|
|
|
|
3
|
|
|
use Cocur\Slugify\Slugify; |
|
4
|
|
|
use Cviebrock\EloquentSluggable\Services\SlugService; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Sluggable |
|
10
|
|
|
* |
|
11
|
|
|
* @package Cviebrock\EloquentSluggable |
|
12
|
|
|
*/ |
|
13
|
|
|
trait Sluggable |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Hook into the Eloquent model events to create or |
|
18
|
|
|
* update the slug as required. |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function bootSluggable(): void |
|
21
|
|
|
{ |
|
22
|
|
|
static::observe(app(SluggableObserver::class)); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Register a slugging model event with the dispatcher. |
|
27
|
|
|
* |
|
28
|
|
|
* @param \Closure|string $callback |
|
29
|
|
|
*/ |
|
30
|
|
|
public static function slugging($callback): void |
|
31
|
|
|
{ |
|
32
|
|
|
static::registerModelEvent('slugging', $callback); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Register a slugged model event with the dispatcher. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Closure|string $callback |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function slugged($callback): void |
|
41
|
|
|
{ |
|
42
|
|
|
static::registerModelEvent('slugged', $callback); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritDoc |
|
47
|
|
|
*/ |
|
48
|
|
|
public function replicate(array $except = null) |
|
49
|
|
|
{ |
|
50
|
|
|
$instance = parent::replicate($except); |
|
51
|
|
|
(new SlugService())->slug($instance, true); |
|
52
|
|
|
|
|
53
|
|
|
return $instance; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Query scope for finding "similar" slugs, used to determine uniqueness. |
|
58
|
|
|
* |
|
59
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
60
|
|
|
* @param string $attribute |
|
61
|
|
|
* @param array $config |
|
62
|
|
|
* @param string $slug |
|
63
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
64
|
|
|
*/ |
|
65
|
|
|
public function scopeFindSimilarSlugs(Builder $query, string $attribute, array $config, string $slug): Builder |
|
66
|
|
|
{ |
|
67
|
|
|
$separator = $config['separator']; |
|
68
|
|
|
|
|
69
|
|
|
return $query->where(function(Builder $q) use ($attribute, $slug, $separator) { |
|
70
|
|
|
$q->where($attribute, '=', $slug) |
|
71
|
|
|
->orWhere($attribute, 'LIKE', $slug . $separator . '%'); |
|
72
|
|
|
}); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return the sluggable configuration array for this model. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
abstract public function sluggable(): array; |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Optionally customize the cocur/slugify engine. |
|
85
|
|
|
* |
|
86
|
|
|
* @param \Cocur\Slugify\Slugify $engine |
|
87
|
|
|
* @param string $attribute |
|
88
|
|
|
* @return \Cocur\Slugify\Slugify |
|
89
|
|
|
*/ |
|
90
|
|
|
public function customizeSlugEngine(Slugify $engine, string $attribute): Slugify |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
return $engine; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Optionally add constraints to the query that determines uniqueness. |
|
97
|
|
|
* |
|
98
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
|
99
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
100
|
|
|
* @param string $attribute |
|
101
|
|
|
* @param array $config |
|
102
|
|
|
* @param string $slug |
|
103
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
104
|
|
|
*/ |
|
105
|
|
|
public function scopeWithUniqueSlugConstraints( |
|
106
|
|
|
Builder $query, |
|
107
|
|
|
Model $model, |
|
|
|
|
|
|
108
|
|
|
string $attribute, |
|
|
|
|
|
|
109
|
|
|
array $config, |
|
|
|
|
|
|
110
|
|
|
string $slug |
|
|
|
|
|
|
111
|
|
|
): Builder |
|
112
|
|
|
{ |
|
113
|
|
|
return $query; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.