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
|
|
|
class SluggableObserver |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var \Cviebrock\EloquentSluggable\Services\SlugService |
12
|
|
|
*/ |
13
|
|
|
private $slugService; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
17
|
|
|
*/ |
18
|
|
|
private $events; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* SluggableObserver constructor. |
22
|
|
|
* |
23
|
|
|
* @param \Cviebrock\EloquentSluggable\Services\SlugService $slugService |
24
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
25
|
|
|
*/ |
26
|
|
|
public function __construct(SlugService $slugService, Dispatcher $events) |
27
|
|
|
{ |
28
|
|
|
$this->slugService = $slugService; |
29
|
|
|
$this->events = $events; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
34
|
|
|
* @return boolean|null |
35
|
|
|
*/ |
36
|
|
|
public function saving(Model $model) |
37
|
|
|
{ |
38
|
|
|
return $this->generateSlug($model, 'saving'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
43
|
|
|
* @param string $event |
44
|
|
|
* @return boolean|null |
45
|
|
|
*/ |
46
|
|
|
protected function generateSlug(Model $model, $event) |
47
|
|
|
{ |
48
|
|
|
// If the "slugging" event returns a value, abort |
49
|
|
|
if ($this->fireSluggingEvent($model, $event) !== null) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
$wasSlugged = $this->slugService->slug($model); |
53
|
|
|
|
54
|
|
|
$this->fireSluggedEvent($model, $wasSlugged); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Fire the namespaced validating event. |
59
|
|
|
* |
60
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
61
|
|
|
* @param string $event |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
protected function fireSluggingEvent(Model $model, $event) |
65
|
|
|
{ |
66
|
|
|
return $this->events->until('eloquent.slugging: ' . get_class($model), [$model, $event]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Fire the namespaced post-validation event. |
71
|
|
|
* |
72
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
73
|
|
|
* @param string $status |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
protected function fireSluggedEvent(Model $model, $status) |
77
|
|
|
{ |
78
|
|
|
$this->events->fire('eloquent.slugged: ' . get_class($model), [$model, $status]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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: