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
|
|
|
* Flag indicating whether to include timestamps in the revision. |
60
|
|
|
* |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
private $revisionTimestamps = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the value of a property of this class. |
67
|
|
|
* |
68
|
|
|
* @param $name |
69
|
|
|
* @return mixed |
70
|
|
|
* @throws Exception |
71
|
|
|
*/ |
72
|
|
|
public function __get($name) |
73
|
|
|
{ |
74
|
|
|
if (property_exists(static::class, $name)) { |
75
|
|
|
return $this->{$name}; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
throw new Exception( |
79
|
|
|
'The property "'.$name.'" does not exist in class "'.static::class.'"' |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get a fresh instance of this class. |
85
|
|
|
* |
86
|
|
|
* @return RevisionOptions |
87
|
|
|
*/ |
88
|
|
|
public static function instance(): self |
89
|
|
|
{ |
90
|
|
|
return new static(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set the $revisionOnCreate to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
95
|
|
|
* |
96
|
|
|
* @return RevisionOptions |
97
|
|
|
*/ |
98
|
|
|
public function enableRevisionOnCreate(): self |
99
|
|
|
{ |
100
|
|
|
$this->revisionOnCreate = true; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the $revisionLimit to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
107
|
|
|
* |
108
|
|
|
* @param int $limit |
109
|
|
|
* @return RevisionOptions |
110
|
|
|
*/ |
111
|
|
|
public function limitRevisionsTo(int $limit): self |
112
|
|
|
{ |
113
|
|
|
$this->revisionLimit = (int) $limit; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the $revisionFields to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
120
|
|
|
* |
121
|
|
|
* @param $fields |
122
|
|
|
* @return RevisionOptions |
123
|
|
|
*/ |
124
|
|
|
public function fieldsToRevision(...$fields): self |
125
|
|
|
{ |
126
|
|
|
$this->revisionFields = Arr::flatten($fields); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the $revisionNotFields to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
133
|
|
|
* |
134
|
|
|
* @param $fields |
135
|
|
|
* @return RevisionOptions |
136
|
|
|
*/ |
137
|
|
|
public function fieldsToNotRevision(...$fields): self |
138
|
|
|
{ |
139
|
|
|
$this->revisionNotFields = Arr::flatten($fields); |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set the $revisionRelations to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
146
|
|
|
* |
147
|
|
|
* @param $relations |
148
|
|
|
* @return RevisionOptions |
149
|
|
|
*/ |
150
|
|
|
public function relationsToRevision(...$relations): self |
151
|
|
|
{ |
152
|
|
|
$this->revisionRelations = Arr::flatten($relations); |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set the $createRevisionWhenRollingBack to work with in the Neurony\Revisions\Traits\HasRevisions trait. |
159
|
|
|
* |
160
|
|
|
* @return RevisionOptions |
161
|
|
|
*/ |
162
|
|
|
public function disableRevisioningWhenRollingBack(): self |
163
|
|
|
{ |
164
|
|
|
$this->createRevisionWhenRollingBack = false; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Enable the revisioning of timestamps. |
171
|
|
|
* |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function withTimestamps() |
175
|
|
|
{ |
176
|
|
|
$this->revisionTimestamps = true; |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|