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; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\Model; |
15
|
|
|
use Zemit\Mvc\Model\Behavior\Snapshot as SnapshotBehavior; |
16
|
|
|
use Zemit\Mvc\Model\Traits\Abstracts\AbstractBehavior; |
17
|
|
|
use Zemit\Mvc\Model\Traits\Abstracts\AbstractEventsManager; |
18
|
|
|
use Zemit\Mvc\Model\Traits\Abstracts\AbstractOptions; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Trait that provides snapshot functionality for a model. |
22
|
|
|
*/ |
23
|
|
|
trait Snapshot |
24
|
|
|
{ |
25
|
|
|
use AbstractEventsManager; |
26
|
|
|
use AbstractOptions; |
27
|
|
|
use AbstractBehavior; |
28
|
|
|
|
29
|
|
|
abstract protected function keepSnapshots(bool $keepSnapshot): void; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialize the snapshot for the model. |
33
|
|
|
* |
34
|
|
|
* @param array|null $options An array of options for initializing the snapshot (default: null) |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
56 |
|
public function initializeSnapshot(?array $options = null): void |
39
|
|
|
{ |
40
|
56 |
|
$options ??= $this->getOptionsManager()->get('snapshot') ?? []; |
41
|
|
|
|
42
|
56 |
|
$this->keepSnapshots($options['keepSnapshots'] ?? true); |
43
|
56 |
|
$this->setSnapshotBehavior(new SnapshotBehavior($options)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the SnapshotBehavior for the model |
48
|
|
|
* |
49
|
|
|
* @param SnapshotBehavior $snapshotBehavior The SnapshotBehavior instance to set |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
56 |
|
public function setSnapshotBehavior(SnapshotBehavior $snapshotBehavior): void |
54
|
|
|
{ |
55
|
56 |
|
$this->setBehavior('snapshot', $snapshotBehavior); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the SnapshotBehavior instance for the model. |
60
|
|
|
* |
61
|
|
|
* @return SnapshotBehavior The SnapshotBehavior instance. |
62
|
|
|
*/ |
63
|
|
|
public function getSnapshotBehavior(): SnapshotBehavior |
64
|
|
|
{ |
65
|
|
|
$behavior = $this->getBehavior('snapshot'); |
66
|
|
|
assert($behavior instanceof SnapshotBehavior); |
67
|
|
|
return $behavior; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Creates a closure that can be used as a callback to determine if a model attribute has changed. |
72
|
|
|
* |
73
|
|
|
* @param callable $callback The callback function to be executed if the model attribute has changed. |
74
|
|
|
* @param bool $anyField Determines whether to check for changes in any field (default: true). |
75
|
|
|
* |
76
|
|
|
* @return \Closure A closure that takes a Model instance and a field name as arguments, and returns the result of the callback |
77
|
|
|
* function if the attribute has changed, or the value of the attribute if it has not changed. |
78
|
|
|
*/ |
79
|
56 |
|
public function hasChangedCallback(callable $callback, bool $anyField = true): \Closure |
80
|
|
|
{ |
81
|
56 |
|
return function (Model $model, $field) use ($callback, $anyField) { |
82
|
|
|
return (!$model->hasSnapshotData() |
83
|
|
|
|| $model->hasChanged($anyField ? null : $field) |
84
|
|
|
|| $model->hasUpdated($anyField ? null : $field)) |
85
|
|
|
? $callback($model, $field) |
86
|
|
|
: $model->readAttribute($field); |
87
|
56 |
|
}; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|