1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the eav package. |
4
|
|
|
* |
5
|
|
|
* @author Aleksandr Drobotik <[email protected]> |
6
|
|
|
* @copyright 2023 Aleksandr Drobotik |
7
|
|
|
* @license https://opensource.org/license/mit The MIT License |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Drobotik\Eav\Value; |
12
|
|
|
|
13
|
|
|
use Drobotik\Eav\Trait\ContainerTrait; |
14
|
|
|
|
15
|
|
|
class ValueManager |
16
|
|
|
{ |
17
|
|
|
use ContainerTrait; |
18
|
|
|
private ValueState $runtime; |
19
|
|
|
private ValueState $stored; |
20
|
|
|
|
21
|
|
|
private int $key; |
22
|
|
|
|
23
|
2 |
|
public function __construct() |
24
|
|
|
{ |
25
|
2 |
|
$this->runtime = new ValueState(); |
26
|
2 |
|
$this->stored = new ValueState(); |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
public function hasKey(): bool |
30
|
|
|
{ |
31
|
2 |
|
return isset($this->key) && $this->key > 0; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
public function getKey(): int |
35
|
|
|
{ |
36
|
2 |
|
return $this->key; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
public function setKey(int $key): self |
40
|
|
|
{ |
41
|
2 |
|
$this->key = $key; |
42
|
|
|
|
43
|
2 |
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function isClean(): bool |
47
|
|
|
{ |
48
|
1 |
|
return $this->isEquivalent(); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
private function isEquivalent(): bool |
52
|
|
|
{ |
53
|
1 |
|
$orig = $this->getStored(); |
54
|
1 |
|
$runtime = $this->getRuntime(); |
55
|
1 |
|
if ($orig === $runtime) { |
56
|
1 |
|
return true; |
57
|
|
|
} |
58
|
1 |
|
if (!$this->isRuntime()) { |
59
|
1 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return is_numeric($orig) && is_numeric($runtime) |
63
|
1 |
|
&& 0 === strcmp((string) $orig, (string) $runtime); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function isStored(): bool |
67
|
|
|
{ |
68
|
1 |
|
return $this->stored->isChanged(); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function getStored(): mixed |
72
|
|
|
{ |
73
|
1 |
|
return $this->stored->get(); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
public function setStored(mixed $value): self |
77
|
|
|
{ |
78
|
1 |
|
$this->stored->set($value); |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
public function clearStored(): self |
84
|
|
|
{ |
85
|
1 |
|
$this->stored->clear(); |
86
|
|
|
|
87
|
1 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
public function isRuntime(): bool |
91
|
|
|
{ |
92
|
2 |
|
return $this->runtime->isChanged(); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function getRuntime(): mixed |
96
|
|
|
{ |
97
|
1 |
|
return $this->runtime->get(); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function setRuntime(mixed $value): self |
101
|
|
|
{ |
102
|
1 |
|
$this->runtime->set($value); |
103
|
|
|
|
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function clearRuntime(): self |
108
|
|
|
{ |
109
|
1 |
|
$this->runtime->clear(); |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
public function getValue() |
115
|
|
|
{ |
116
|
1 |
|
if ($this->isRuntime()) { |
117
|
1 |
|
return $this->getRuntime(); |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
return $this->getStored(); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function setValue(mixed $value): self |
124
|
|
|
{ |
125
|
1 |
|
$this->setRuntime($value); |
126
|
|
|
|
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
public function clearValue(): self |
131
|
|
|
{ |
132
|
1 |
|
$this->clearRuntime(); |
133
|
|
|
|
134
|
1 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|