1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neurony\Revisions\Options; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
|
8
|
|
|
class RevisionOptions |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Flag whether to make a revision on model creation. |
12
|
|
|
* |
13
|
|
|
* @var bool |
14
|
|
|
*/ |
15
|
|
|
private $revisionOnCreate = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The limit of revisions to be created for a model instance. |
19
|
|
|
* If the limit is reached, oldest revisions will start getting deleted to make room for new ones. |
20
|
|
|
* |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
private $revisionLimit; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The fields that should be revisionable. |
27
|
|
|
* By default (null) all fields are revisionable. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $revisionFields = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The fields that should be excluded from revisioning. |
35
|
|
|
* By default (null) no fields are excluded from revisioning. |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $revisionNotFields = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The model's relations that should be revisionable. |
43
|
|
|
* By default (null) none of the model's relations are revisionable. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $revisionRelations = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Flag indicating whether to create a revision for the model, when rolling back another revision of that model. |
51
|
|
|
* If set to "true", before rolling back a revision, the original model instance's data will be stored to a new revision. |
52
|
|
|
* If set to "false", after rolling back a revision, the original model instance's data will NOT be stored to a new revision. |
53
|
|
|
* |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
private $createRevisionWhenRollingBack = true; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the value of a property of this class. |
60
|
|
|
* |
61
|
|
|
* @param $name |
62
|
|
|
* @return mixed |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
|
public function __get($name) |
66
|
|
|
{ |
67
|
|
|
if (property_exists(static::class, $name)) { |
68
|
|
|
return $this->{$name}; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new Exception( |
72
|
|
|
'The property "'.$name.'" does not exist in class "'.static::class.'"' |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get a fresh instance of this class. |
78
|
|
|
* |
79
|
|
|
* @return RevisionOptions |
80
|
|
|
*/ |
81
|
|
|
public static function instance(): self |
82
|
|
|
{ |
83
|
|
|
return new static(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set the $revisionOnCreate to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
88
|
|
|
* |
89
|
|
|
* @return RevisionOptions |
90
|
|
|
*/ |
91
|
|
|
public function enableRevisionOnCreate(): self |
92
|
|
|
{ |
93
|
|
|
$this->revisionOnCreate = true; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the $revisionLimit to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
100
|
|
|
* |
101
|
|
|
* @param int $limit |
102
|
|
|
* @return RevisionOptions |
103
|
|
|
*/ |
104
|
|
|
public function limitRevisionsTo(int $limit): self |
105
|
|
|
{ |
106
|
|
|
$this->revisionLimit = (int) $limit; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set the $revisionFields to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
113
|
|
|
* |
114
|
|
|
* @param $fields |
115
|
|
|
* @return RevisionOptions |
116
|
|
|
*/ |
117
|
|
|
public function fieldsToRevision(...$fields): self |
118
|
|
|
{ |
119
|
|
|
$this->revisionFields = Arr::flatten($fields); |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set the $revisionNotFields to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
126
|
|
|
* |
127
|
|
|
* @param $fields |
128
|
|
|
* @return RevisionOptions |
129
|
|
|
*/ |
130
|
|
|
public function fieldsToNotRevision(...$fields): self |
131
|
|
|
{ |
132
|
|
|
$this->revisionNotFields = Arr::flatten($fields); |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the $revisionRelations to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
139
|
|
|
* |
140
|
|
|
* @param $relations |
141
|
|
|
* @return RevisionOptions |
142
|
|
|
*/ |
143
|
|
|
public function relationsToRevision(...$relations): self |
144
|
|
|
{ |
145
|
|
|
$this->revisionRelations = Arr::flatten($relations); |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Set the $createRevisionWhenRollingBack to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
152
|
|
|
* |
153
|
|
|
* @return RevisionOptions |
154
|
|
|
*/ |
155
|
|
|
public function disableRevisioningWhenRollingBack(): self |
156
|
|
|
{ |
157
|
|
|
$this->createRevisionWhenRollingBack = false; |
158
|
|
|
|
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|