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\Database\Eloquent\SoftDeletes; |
15
|
|
|
use Illuminate\Support\Arr; |
16
|
|
|
use SebastianBergmann\Diff\Differ; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Version. |
20
|
|
|
* |
21
|
|
|
* @property Model $versionable |
22
|
|
|
* @property array $contents |
23
|
|
|
*/ |
24
|
|
|
class Version extends Model |
25
|
|
|
{ |
26
|
|
|
use SoftDeletes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $casts = [ |
32
|
|
|
'contents' => 'array', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
37
|
|
|
*/ |
38
|
|
|
public function user() |
39
|
|
|
{ |
40
|
|
|
$useSoftDeletes = \in_array(SoftDeletes::class, \class_uses(\config('versionable.user_model'))); |
41
|
|
|
|
42
|
|
|
return \tap( |
43
|
2 |
|
$this->belongsTo( |
44
|
|
|
\config('versionable.user_model'), |
45
|
2 |
|
\config('versionable.user_foreign_key') |
46
|
|
|
), |
47
|
|
|
fn ($relation) => $useSoftDeletes ? $relation->withTrashed() : $relation |
|
|
|
|
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
53
|
6 |
|
*/ |
54
|
|
|
public function versionable() |
55
|
6 |
|
{ |
56
|
|
|
return $this->morphTo('versionable'); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
6 |
|
/** |
60
|
6 |
|
* @param \Illuminate\Database\Eloquent\Model $model |
61
|
6 |
|
* @param array $attributes |
62
|
6 |
|
* |
63
|
|
|
* @return \Overtrue\LaravelVersionable\Version |
64
|
6 |
|
*/ |
65
|
|
|
public static function createForModel(Model $model, array $attributes = []) |
66
|
6 |
|
{ |
67
|
|
|
$versionClass = $model->getVersionModel(); |
68
|
|
|
|
69
|
|
|
$version = new $versionClass(); |
70
|
|
|
|
71
|
|
|
$version->versionable_id = $model->getKey(); |
72
|
1 |
|
$version->versionable_type = $model->getMorphClass(); |
73
|
|
|
$version->{\config('versionable.user_foreign_key')} = \auth()->id(); |
74
|
1 |
|
$version->contents = \array_merge($attributes, $model->getVersionableAttributes()); |
75
|
|
|
|
76
|
|
|
$version->save(); |
77
|
|
|
|
78
|
|
|
return $version; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
1 |
|
* @return bool |
83
|
|
|
*/ |
84
|
1 |
|
public function revert() |
85
|
|
|
{ |
86
|
1 |
|
return $this->versionable->fill($this->contents)->save(); |
87
|
1 |
|
} |
88
|
|
|
|
89
|
1 |
|
/** |
90
|
|
|
* @param \Illuminate\Database\Eloquent\Model|null $model |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
1 |
|
*/ |
94
|
|
|
public function diff(Model $model = null) |
95
|
|
|
{ |
96
|
1 |
|
$model || $model = $this->versionable; |
97
|
|
|
|
98
|
|
|
if ($model instanceof Version) { |
99
|
|
|
$source = $model->contents; |
100
|
|
|
} else { |
101
|
|
|
if (!\in_array(Versionable::class, \class_uses($model))) { |
102
|
|
|
throw new \InvalidArgumentException(\sprintf('Model %s is not versionable.', \get_class($model))); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$source = $model->versionableFromArray($this->versionable->toArray()); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return (new Differ())->diff(Arr::only($source, \array_keys($this->contents)), $this->contents); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|