|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* apparat-object |
|
5
|
|
|
* |
|
6
|
|
|
* @category Apparat |
|
7
|
|
|
* @package Apparat\Object |
|
8
|
|
|
* @subpackage Apparat\Object\Domain |
|
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
|
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
/*********************************************************************************** |
|
15
|
|
|
* The MIT License (MIT) |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
|
18
|
|
|
* |
|
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
21
|
|
|
* the Software without restriction, including without limitation the rights to |
|
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
24
|
|
|
* subject to the following conditions: |
|
25
|
|
|
* |
|
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
27
|
|
|
* copies or substantial portions of the Software. |
|
28
|
|
|
* |
|
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
|
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
|
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
|
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
|
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
35
|
|
|
***********************************************************************************/ |
|
36
|
|
|
|
|
37
|
|
|
namespace Apparat\Object\Domain\Model\Object\Traits; |
|
38
|
|
|
|
|
39
|
|
|
use Apparat\Object\Domain\Model\Properties\SystemProperties; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Object states trait |
|
43
|
|
|
* |
|
44
|
|
|
* @package Apparat\Object |
|
45
|
|
|
* @subpackage Apparat\Object\Domain\Model\Object\Traits |
|
46
|
|
|
* @property SystemProperties $systemProperties |
|
47
|
|
|
*/ |
|
48
|
|
|
trait StatesTrait |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* Object state |
|
52
|
|
|
* |
|
53
|
|
|
* @var int |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $state = self::STATE_CLEAN; |
|
56
|
|
|
/** |
|
57
|
|
|
* Property collection states |
|
58
|
|
|
* |
|
59
|
|
|
* @var array |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $collectionStates = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return whether the object is in mutated state |
|
65
|
|
|
* |
|
66
|
|
|
* @return boolean Mutated state |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function hasBeenMutated() |
|
69
|
|
|
{ |
|
70
|
3 |
|
return !!($this->state & self::STATE_MUTATED); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Return whether the object is in published state |
|
75
|
|
|
* |
|
76
|
|
|
* @return boolean Published state |
|
77
|
|
|
*/ |
|
78
|
|
|
public function isPublished() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->systemProperties->isPublished(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Return whether the object has been deleted |
|
85
|
|
|
* |
|
86
|
|
|
* @return boolean Object is deleted |
|
87
|
|
|
*/ |
|
88
|
|
|
public function isDeleted() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->systemProperties->isDeleted(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Return whether the object has just been deleted |
|
95
|
|
|
* |
|
96
|
|
|
* @return boolean Object has just been deleted |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function hasBeenDeleted() |
|
99
|
|
|
{ |
|
100
|
1 |
|
return !!($this->state & self::STATE_DELETED); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return whether the object has just been undeleted |
|
105
|
|
|
* |
|
106
|
|
|
* @return boolean Object has just been undeleted |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function hasBeenUndeleted() |
|
109
|
|
|
{ |
|
110
|
1 |
|
return !!($this->state & self::STATE_UNDELETED); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Return whether the object is in modified state |
|
115
|
|
|
* |
|
116
|
|
|
* @return boolean Modified state |
|
117
|
|
|
*/ |
|
118
|
3 |
|
public function hasBeenModified() |
|
119
|
|
|
{ |
|
120
|
3 |
|
return !!($this->state & self::STATE_MODIFIED); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Set the object state to mutated |
|
125
|
|
|
*/ |
|
126
|
4 |
|
protected function setMutatedState() |
|
127
|
|
|
{ |
|
128
|
|
|
// Make this object a draft if not already the case and not just |
|
129
|
4 |
|
if (!$this->isDraft() && !$this->hasBeenPublished()) { |
|
130
|
|
|
// TODO: Send signal |
|
131
|
4 |
|
$this->convertToDraft(); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// Enable the mutated state |
|
135
|
4 |
|
$this->state |= self::STATE_MUTATED; |
|
136
|
|
|
|
|
137
|
|
|
// Enable the modified state |
|
138
|
4 |
|
$this->setModifiedState(); |
|
139
|
4 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Return the object draft mode |
|
143
|
|
|
* |
|
144
|
|
|
* @return boolean Object draft mode |
|
145
|
|
|
*/ |
|
146
|
5 |
|
public function isDraft() |
|
147
|
|
|
{ |
|
148
|
5 |
|
return $this->systemProperties->isDraft(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Return whether the object has just been published |
|
153
|
|
|
* |
|
154
|
|
|
* @return boolean Object has just been published |
|
155
|
|
|
*/ |
|
156
|
4 |
|
public function hasBeenPublished() |
|
157
|
|
|
{ |
|
158
|
4 |
|
return !!($this->state & self::STATE_PUBLISHED); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Set the object state to modified |
|
163
|
|
|
*/ |
|
164
|
7 |
|
protected function setModifiedState() |
|
165
|
|
|
{ |
|
166
|
|
|
// If this object is not in modified state yet |
|
167
|
7 |
|
if (!($this->state & self::STATE_MODIFIED)) { |
|
168
|
|
|
// TODO: Send signal |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Enable the modified state |
|
172
|
7 |
|
$this->state |= self::STATE_MODIFIED; |
|
173
|
|
|
|
|
174
|
|
|
// Update the modification timestamp |
|
175
|
7 |
|
$this->setSystemProperties($this->systemProperties->touch(), true); |
|
|
|
|
|
|
176
|
7 |
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Set the object state to deleted |
|
180
|
|
|
*/ |
|
181
|
|
|
protected function setDeletedState() |
|
182
|
|
|
{ |
|
183
|
|
|
// If this object is not in deleted state yet |
|
184
|
|
|
if (!($this->state & self::STATE_DELETED)) { |
|
185
|
|
|
// TODO: Send signal |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
// Enable the deleted state |
|
189
|
|
|
$this->state |= self::STATE_DELETED; |
|
190
|
|
|
$this->state &= ~self::STATE_UNDELETED; |
|
191
|
|
|
|
|
192
|
|
|
// Update system properties |
|
193
|
|
|
$this->setSystemProperties($this->systemProperties->delete(), true); |
|
|
|
|
|
|
194
|
|
|
$this->setModifiedState(); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Set the object state to undeleted |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function setUndeletedState() |
|
201
|
|
|
{ |
|
202
|
|
|
// If this object is in deleted state |
|
203
|
|
|
if ($this->state & self::STATE_DELETED) { |
|
204
|
|
|
// TODO: Send signal |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
// Disable the deleted state |
|
208
|
|
|
$this->state |= self::STATE_UNDELETED; |
|
209
|
|
|
$this->state &= ~self::STATE_DELETED; |
|
210
|
|
|
|
|
211
|
|
|
// Update system properties |
|
212
|
|
|
$this->setSystemProperties($this->systemProperties->undelete(), true); |
|
|
|
|
|
|
213
|
|
|
$this->setModifiedState(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Set the object state to published |
|
218
|
|
|
*/ |
|
219
|
1 |
|
protected function setPublishedState() |
|
220
|
|
|
{ |
|
221
|
|
|
// If this object is not in published state yet |
|
222
|
1 |
|
if (!($this->state & self::STATE_PUBLISHED)) { |
|
223
|
|
|
// TODO: Send signal |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
// Set the published flag |
|
227
|
1 |
|
$this->state |= self::STATE_PUBLISHED; |
|
228
|
|
|
|
|
229
|
|
|
// Update system properties |
|
230
|
1 |
|
$this->setSystemProperties($this->systemProperties->publish(), true); |
|
|
|
|
|
|
231
|
1 |
|
$this->setModifiedState(); |
|
232
|
1 |
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Reset the object state |
|
236
|
|
|
*/ |
|
237
|
24 |
|
protected function resetState() |
|
238
|
|
|
{ |
|
239
|
|
|
// If this object is not clean |
|
240
|
24 |
|
if ($this->state != self::STATE_CLEAN) { |
|
241
|
|
|
// TODO: Send signal |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
24 |
|
$this->state = self::STATE_CLEAN; |
|
245
|
24 |
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.