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
|
|
|
/** @var string */ |
16
|
|
|
public const SAVING = 'saving'; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
public const SAVED = 'saved'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Cviebrock\EloquentSluggable\Services\SlugService |
23
|
|
|
*/ |
24
|
|
|
private $slugService; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
28
|
|
|
*/ |
29
|
|
|
private $events; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* SluggableObserver constructor. |
33
|
|
|
* |
34
|
|
|
* @param \Cviebrock\EloquentSluggable\Services\SlugService $slugService |
35
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
36
|
|
|
*/ |
37
|
|
|
public function __construct(SlugService $slugService, Dispatcher $events) |
38
|
|
|
{ |
39
|
|
|
$this->slugService = $slugService; |
40
|
|
|
$this->events = $events; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
45
|
|
|
* @return bool|void |
46
|
|
|
*/ |
47
|
|
|
public function saving(Model $model) |
48
|
|
|
{ |
49
|
|
|
if ($model->sluggableEvent() !== self::SAVING) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->generateSlug($model, 'saving'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
58
|
|
|
* @return bool|void |
59
|
|
|
*/ |
60
|
|
|
public function saved(Model $model) |
61
|
|
|
{ |
62
|
|
|
if ($model->sluggableEvent() !== self::SAVED) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
if ($this->generateSlug($model, 'saved')) { |
66
|
|
|
return $model->saveQuietly(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
72
|
|
|
* @param string $event |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
protected function generateSlug(Model $model, string $event): bool |
76
|
|
|
{ |
77
|
|
|
// If the "slugging" event returns false, abort |
78
|
|
|
if ($this->fireSluggingEvent($model, $event) === false) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
$wasSlugged = $this->slugService->slug($model); |
82
|
|
|
|
83
|
|
|
$this->fireSluggedEvent($model, $wasSlugged); |
|
|
|
|
84
|
|
|
|
85
|
|
|
return $wasSlugged; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fire the namespaced validating event. |
90
|
|
|
* |
91
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
92
|
|
|
* @param string $event |
93
|
|
|
* @return array|null |
94
|
|
|
*/ |
95
|
|
|
protected function fireSluggingEvent(Model $model, string $event): ?array |
96
|
|
|
{ |
97
|
|
|
return $this->events->until('eloquent.slugging: ' . get_class($model), [$model, $event]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Fire the namespaced post-validation event. |
102
|
|
|
* |
103
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
104
|
|
|
* @param string $status |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function fireSluggedEvent(Model $model, string $status): void |
108
|
|
|
{ |
109
|
|
|
$this->events->dispatch('eloquent.slugged: ' . get_class($model), [$model, $status]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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: