|
1
|
|
|
<?php namespace Cviebrock\EloquentSluggable; |
|
2
|
|
|
|
|
3
|
|
|
use Cviebrock\EloquentSluggable\Services\SlugService; |
|
4
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class SluggableObserver |
|
9
|
|
|
* |
|
10
|
|
|
* @package Cviebrock\EloquentSluggable |
|
11
|
|
|
*/ |
|
12
|
|
|
class SluggableObserver |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var \Cviebrock\EloquentSluggable\Services\SlugService |
|
17
|
|
|
*/ |
|
18
|
|
|
private $slugService; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
|
22
|
|
|
*/ |
|
23
|
|
|
private $events; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SluggableObserver constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param \Cviebrock\EloquentSluggable\Services\SlugService $slugService |
|
29
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(SlugService $slugService, Dispatcher $events) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->slugService = $slugService; |
|
34
|
|
|
$this->events = $events; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
39
|
|
|
* @return bool|void |
|
40
|
|
|
*/ |
|
41
|
|
|
public function saved(Model $model) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($this->generateSlug($model, 'saved')) { |
|
44
|
|
|
return $model->saveQuietly(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
50
|
|
|
* @param string $event |
|
51
|
|
|
* @return boolean |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function generateSlug(Model $model, string $event): bool |
|
54
|
|
|
{ |
|
55
|
|
|
// If the "slugging" event returns false, abort |
|
56
|
|
|
if ($this->fireSluggingEvent($model, $event) === false) { |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
$wasSlugged = $this->slugService->slug($model); |
|
60
|
|
|
|
|
61
|
|
|
$this->fireSluggedEvent($model, $wasSlugged); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
return $wasSlugged; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Fire the namespaced validating event. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
70
|
|
|
* @param string $event |
|
71
|
|
|
* @return array|null |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function fireSluggingEvent(Model $model, string $event): ?array |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->events->until('eloquent.slugging: ' . get_class($model), [$model, $event]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Fire the namespaced post-validation event. |
|
80
|
|
|
* |
|
81
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
|
82
|
|
|
* @param string $status |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function fireSluggedEvent(Model $model, string $status): void |
|
86
|
|
|
{ |
|
87
|
|
|
$this->events->dispatch('eloquent.slugged: ' . get_class($model), [$model, $status]); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: