1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model\Blameable; |
13
|
|
|
|
14
|
|
|
use Zemit\Mvc\Model; |
15
|
|
|
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior; |
16
|
|
|
use Zemit\Mvc\Model\Behavior\Transformable; |
17
|
|
|
use Zemit\Mvc\Model\Identity; |
18
|
|
|
use Zemit\Mvc\Model\Options; |
19
|
|
|
use Zemit\Mvc\Model\Snapshots; |
20
|
|
|
use Zemit\Mvc\Model\SoftDelete; |
21
|
|
|
|
22
|
|
|
trait Updated |
23
|
|
|
{ |
24
|
|
|
use AbstractBehavior; |
25
|
|
|
use Options; |
26
|
|
|
use Identity; |
27
|
|
|
use Snapshots; |
28
|
|
|
use SoftDelete; |
29
|
|
|
|
30
|
|
|
public Transformable $updatedBehavior; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializing Updated |
34
|
|
|
*/ |
35
|
|
|
public function initializeUpdated(?array $options = null): void |
36
|
|
|
{ |
37
|
|
|
$options ??= $this->getOptionsManager()->get('updated') ?? []; |
38
|
|
|
|
39
|
|
|
$fieldBy = $options['fieldBy'] ?? 'updatedBy'; |
40
|
|
|
$fieldAs = $options['fieldAs'] ?? 'updatedAs'; |
41
|
|
|
$fieldAt = $options['fieldAt'] ?? 'updatedAt'; |
42
|
|
|
|
43
|
|
|
$this->setUpdatedBehavior(new Transformable([ |
44
|
|
|
'beforeValidationOnUpdate' => [ |
45
|
|
|
$fieldBy => $this->hasChangedCallback(function () { |
|
|
|
|
46
|
|
|
return $this->getCurrentUserIdCallback(false)(); |
47
|
|
|
}), |
48
|
|
|
$fieldAs => $this->hasChangedCallback(function () { |
49
|
|
|
return $this->getCurrentUserIdCallback(true)(); |
50
|
|
|
}), |
51
|
|
|
$fieldAt => $this->hasChangedCallback(function () { |
52
|
|
|
return date(Model::DATETIME_FORMAT); |
53
|
|
|
}), |
54
|
|
|
], |
55
|
|
|
])); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set Updated Behavior |
60
|
|
|
*/ |
61
|
|
|
public function setUpdatedBehavior(Transformable $updatedBehavior): void |
62
|
|
|
{ |
63
|
|
|
$this->updatedBehavior = $updatedBehavior; |
64
|
|
|
$this->addBehavior($this->updatedBehavior); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get Updated Behavior |
69
|
|
|
*/ |
70
|
|
|
public function getUpdatedBehavior(): Transformable |
71
|
|
|
{ |
72
|
|
|
return $this->updatedBehavior; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|