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\Traits\Blameable; |
13
|
|
|
|
14
|
|
|
use Zemit\Db\Column; |
15
|
|
|
use Zemit\Mvc\Model\Behavior\Transformable; |
16
|
|
|
use Zemit\Mvc\Model\Traits\Abstracts\AbstractBehavior; |
17
|
|
|
use Zemit\Mvc\Model\Traits\Abstracts\AbstractBlameable; |
18
|
|
|
use Zemit\Mvc\Model\Traits\Identity; |
19
|
|
|
use Zemit\Mvc\Model\Traits\Options; |
20
|
|
|
use Zemit\Mvc\Model\Traits\Snapshot; |
21
|
|
|
use Zemit\Mvc\Model\Traits\SoftDelete; |
22
|
|
|
|
23
|
|
|
trait Updated |
24
|
|
|
{ |
25
|
|
|
use AbstractBehavior; |
26
|
|
|
use AbstractBlameable; |
27
|
|
|
use Options; |
28
|
|
|
use Identity; |
29
|
|
|
use Snapshot; |
30
|
|
|
use SoftDelete; |
31
|
|
|
use BlameAt; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initializing Updated |
35
|
|
|
*/ |
36
|
56 |
|
public function initializeUpdated(?array $options = null): void |
37
|
|
|
{ |
38
|
56 |
|
$options ??= $this->getOptionsManager()->get('updated') ?? []; |
39
|
|
|
|
40
|
56 |
|
$fieldBy = $options['fieldBy'] ?? 'updatedBy'; |
41
|
56 |
|
$fieldAs = $options['fieldAs'] ?? 'updatedAs'; |
42
|
56 |
|
$fieldAt = $options['fieldAt'] ?? 'updatedAt'; |
43
|
|
|
|
44
|
56 |
|
$this->addUserRelationship($fieldBy, 'UpdatedBy'); |
45
|
56 |
|
$this->addUserRelationship($fieldAs, 'UpdatedAs'); |
46
|
|
|
|
47
|
56 |
|
$this->setUpdatedBehavior(new Transformable([ |
48
|
56 |
|
'beforeValidationOnUpdate' => [ |
49
|
56 |
|
$fieldBy => $this->hasChangedCallback(function () { |
50
|
|
|
return $this->getCurrentUserIdCallback(false)(); |
51
|
56 |
|
}), |
52
|
56 |
|
$fieldAs => $this->hasChangedCallback(function () { |
53
|
|
|
return $this->getCurrentUserIdCallback(true)(); |
54
|
56 |
|
}), |
55
|
56 |
|
$fieldAt => $this->hasChangedCallback(function () { |
56
|
|
|
return $this->getDateCallback(Column::DATETIME_FORMAT)(); |
57
|
56 |
|
}), |
58
|
56 |
|
], |
59
|
56 |
|
])); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set Updated Behavior |
64
|
|
|
*/ |
65
|
56 |
|
public function setUpdatedBehavior(Transformable $updatedBehavior): void |
66
|
|
|
{ |
67
|
56 |
|
$this->setBehavior('updated', $updatedBehavior); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get Updated Behavior |
72
|
|
|
*/ |
73
|
|
|
public function getUpdatedBehavior(): Transformable |
74
|
|
|
{ |
75
|
|
|
$behavior = $this->getBehavior('updated'); |
76
|
|
|
assert($behavior instanceof Transformable); |
77
|
|
|
return $behavior; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|