1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/laravel-versionable. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Overtrue\LaravelVersionable; |
12
|
|
|
|
13
|
|
|
use Illuminate\Database\Eloquent\Model; |
14
|
|
|
use Illuminate\Support\Arr; |
15
|
|
|
use SebastianBergmann\Diff\Differ; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Version. |
19
|
|
|
* |
20
|
|
|
* @property Model $versionable |
21
|
|
|
* @property array $contents |
22
|
|
|
*/ |
23
|
|
|
class Version extends Model |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $casts = [ |
29
|
|
|
'contents' => 'array', |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
34
|
|
|
*/ |
35
|
|
|
public function user() |
36
|
|
|
{ |
37
|
|
|
return $this->belongsTo(\config('versionable.user_model'), \config('versionable.user_foreign_key')); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
42
|
|
|
*/ |
43
|
2 |
|
public function versionable() |
44
|
|
|
{ |
45
|
2 |
|
return $this->morphTo('versionable'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
50
|
|
|
* |
51
|
|
|
* @return \Overtrue\LaravelVersionable\Version |
52
|
|
|
*/ |
53
|
5 |
|
public static function createForModel(Model $model) |
54
|
|
|
{ |
55
|
5 |
|
$versionClass = $model->getVersionModel(); |
56
|
|
|
|
57
|
5 |
|
$version = new $versionClass(); |
58
|
|
|
|
59
|
5 |
|
$version->versionable_id = $model->getKey(); |
60
|
5 |
|
$version->versionable_type = $model->getMorphClass(); |
61
|
5 |
|
$version->{\config('versionable.user_foreign_key')} = \auth()->id(); |
|
|
|
|
62
|
5 |
|
$version->contents = $model->getVersionableAttributes(); |
63
|
|
|
|
64
|
5 |
|
$version->save(); |
65
|
|
|
|
66
|
5 |
|
return $version; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
1 |
|
public function revert() |
73
|
|
|
{ |
74
|
1 |
|
return $this->versionable->forceFill($this->contents)->save(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $model |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
public function diff(Model $model = null) |
83
|
|
|
{ |
84
|
1 |
|
$model || $model = $this->versionable; |
|
|
|
|
85
|
|
|
|
86
|
1 |
|
if ($model instanceof Version) { |
87
|
1 |
|
$source = $model->contents; |
88
|
|
|
} else { |
89
|
1 |
|
if (!\in_array(Versionable::class, \class_uses($model))) { |
90
|
|
|
throw new \InvalidArgumentException(\sprintf('Model %s is not versionable.', \get_class($model))); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
$source = $model->versionableFromArray($this->versionable->toArray()); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return (new Differ())->diff(Arr::only($source, \array_keys($this->contents)), $this->contents); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: