1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Charcoal\Object; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
// From 'charcoal-core' |
8
|
|
|
use Charcoal\Loader\CollectionLoader; |
9
|
|
|
|
10
|
|
|
// From 'charcoal-object' |
11
|
|
|
use Charcoal\Object\ObjectRevision; |
12
|
|
|
use Charcoal\Object\ObjectRevisionInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
trait RevisionableTrait |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var boolean $revisionEnabled |
21
|
|
|
*/ |
22
|
|
|
protected $revisionEnabled = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The class name of the object revision model. |
26
|
|
|
* |
27
|
|
|
* Must be a fully-qualified PHP namespace and an implementation of |
28
|
|
|
* {@see \Charcoal\Object\ObjectRevisionInterface}. Used by the model factory. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $objectRevisionClass = ObjectRevision::class; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param boolean $enabled The (revision) enabled flag. |
36
|
|
|
* @return RevisionableInterface Chainable |
37
|
|
|
*/ |
38
|
|
|
public function setRevisionEnabled($enabled) |
39
|
|
|
{ |
40
|
|
|
$this->revisionEnabled = !!$enabled; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return boolean |
46
|
|
|
*/ |
47
|
|
|
public function getRevisionEnabled() |
48
|
|
|
{ |
49
|
|
|
return $this->revisionEnabled; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a revision object. |
54
|
|
|
* |
55
|
|
|
* @return ObjectRevisionInterface |
56
|
|
|
*/ |
57
|
|
|
public function createRevisionObject() |
58
|
|
|
{ |
59
|
|
|
$rev = $this->modelFactory()->create($this->getObjectRevisionClass()); |
60
|
|
|
|
61
|
|
|
return $rev; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set the class name of the object revision model. |
66
|
|
|
* |
67
|
|
|
* @param string $className The class name of the object revision model. |
68
|
|
|
* @throws InvalidArgumentException If the class name is not a string. |
69
|
|
|
* @return AbstractPropertyDisplay Chainable |
70
|
|
|
*/ |
71
|
|
|
protected function setObjectRevisionClass($className) |
72
|
|
|
{ |
73
|
|
|
if (!is_string($className)) { |
74
|
|
|
throw new InvalidArgumentException( |
75
|
|
|
'Route class name must be a string.' |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->objectRevisionClass = $className; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Retrieve the class name of the object revision model. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getObjectRevisionClass() |
89
|
|
|
{ |
90
|
|
|
return $this->objectRevisionClass; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Alias of {@see self::getObjectRevisionClass()}. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function objectRevisionClass() |
99
|
|
|
{ |
100
|
|
|
return $this->getObjectRevisionClass(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @see \Charcoal\Object\ObjectRevision::create_fromObject() |
105
|
|
|
* @return ObjectRevision |
106
|
|
|
*/ |
107
|
|
|
public function generateRevision() |
108
|
|
|
{ |
109
|
|
|
$rev = $this->createRevisionObject(); |
110
|
|
|
|
111
|
|
|
$rev->createFromObject($this); |
112
|
|
|
if (!empty($rev->getDataDiff())) { |
113
|
|
|
$rev->save(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $rev; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @see \Charcoal\Object\ObejctRevision::lastObjectRevision |
121
|
|
|
* @return ObjectRevision |
122
|
|
|
*/ |
123
|
|
|
public function latestRevision() |
124
|
|
|
{ |
125
|
|
|
$rev = $this->createRevisionObject(); |
126
|
|
|
$rev = $rev->lastObjectRevision($this); |
127
|
|
|
|
128
|
|
|
return $rev; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @see \Charcoal\Object\ObejctRevision::objectRevisionNum |
133
|
|
|
* @param integer $revNum The revision number. |
134
|
|
|
* @return ObjectRevision |
135
|
|
|
*/ |
136
|
|
|
public function revisionNum($revNum) |
137
|
|
|
{ |
138
|
|
|
$rev = $this->createRevisionObject(); |
139
|
|
|
$rev = $rev->objectRevisionNum($this, intval($revNum)); |
140
|
|
|
|
141
|
|
|
return $rev; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Retrieves all revisions for the current objet |
146
|
|
|
* |
147
|
|
|
* @param callable $callback Optional object callback. |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function allRevisions(callable $callback = null) |
151
|
|
|
{ |
152
|
|
|
$loader = new CollectionLoader([ |
153
|
|
|
'logger' => $this->logger, |
|
|
|
|
154
|
|
|
'factory' => $this->modelFactory(), |
155
|
|
|
]); |
156
|
|
|
$loader->setModel($this->createRevisionObject()); |
|
|
|
|
157
|
|
|
$loader->addFilter('targetType', $this->objType()); |
|
|
|
|
158
|
|
|
$loader->addFilter('targetId', $this->id()); |
|
|
|
|
159
|
|
|
$loader->addOrder('revTs', 'desc'); |
160
|
|
|
if ($callback !== null) { |
161
|
|
|
$loader->setCallback($callback); |
162
|
|
|
} |
163
|
|
|
$revisions = $loader->load(); |
164
|
|
|
|
165
|
|
|
return $revisions->objects(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param integer $revNum The revision number to revert to. |
170
|
|
|
* @throws InvalidArgumentException If revision number is invalid. |
171
|
|
|
* @return boolean Success / Failure. |
172
|
|
|
*/ |
173
|
|
|
public function revertToRevision($revNum) |
174
|
|
|
{ |
175
|
|
|
if (!$revNum) { |
176
|
|
|
throw new InvalidArgumentException( |
177
|
|
|
'Invalid revision number' |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$rev = $this->revisionNum(intval($revNum)); |
182
|
|
|
|
183
|
|
|
if (!$rev->id()) { |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (isset($obj['lastModifiedBy'])) { |
|
|
|
|
188
|
|
|
$obj['lastModifiedBy'] = $rev->getRevUser(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$this->setData($rev->getDataObj()); |
|
|
|
|
192
|
|
|
$this->update(); |
|
|
|
|
193
|
|
|
|
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Retrieve the object model factory. |
199
|
|
|
* |
200
|
|
|
* @return \Charcoal\Factory\FactoryInterface |
201
|
|
|
*/ |
202
|
|
|
abstract public function modelFactory(); |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return \Charcoal\Model\MetadataInterface |
206
|
|
|
*/ |
207
|
|
|
abstract public function metadata(); |
208
|
|
|
} |
209
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: