@@ -19,6 +19,16 @@ class ModelProperty {
1919 */
2020 private bool $ isDirty = false ;
2121
22+ /**
23+ * Whether the original value has been set.
24+ */
25+ private bool $ isOriginalValueSet = false ;
26+
27+ /**
28+ * Whether the value has been set.
29+ */
30+ private bool $ isValueSet = false ;
31+
2232 /**
2333 * The key of the property.
2434 */
@@ -58,6 +68,8 @@ public function __construct( string $key, ModelPropertyDefinition $definition, $
5868
5969 $ this ->value = $ initialValue ;
6070 $ this ->originalValue = $ this ->value ;
71+ $ this ->isValueSet = true ;
72+ $ this ->isOriginalValueSet = true ;
6173 }
6274 }
6375
@@ -98,7 +110,7 @@ public function getOriginalValue() {
98110 * @return mixed
99111 */
100112 public function getValue () {
101- return $ this ->value ?? null ;
113+ return $ this ->isValueSet ? $ this -> value : null ;
102114 }
103115
104116 /**
@@ -125,7 +137,7 @@ public function isDirty(): bool {
125137 * @since 2.0.0
126138 */
127139 public function isSet (): bool {
128- return isset ( $ this ->value ) ;
140+ return $ this ->isValueSet ;
129141 }
130142
131143 /**
@@ -134,10 +146,11 @@ public function isSet(): bool {
134146 * @since 2.0.0
135147 */
136148 public function revertChanges (): void {
137- if ( isset ( $ this ->originalValue ) ) {
149+ if ( $ this ->isOriginalValueSet ) {
138150 $ this ->value = $ this ->originalValue ;
151+ $ this ->isValueSet = true ;
139152 } else {
140- unset( $ this ->value ) ;
153+ $ this ->isValueSet = false ;
141154 }
142155
143156 $ this ->isDirty = false ;
@@ -150,6 +163,7 @@ public function revertChanges(): void {
150163 */
151164 public function commitChanges (): void {
152165 $ this ->originalValue = $ this ->value ;
166+ $ this ->isOriginalValueSet = $ this ->isValueSet ;
153167 $ this ->isDirty = false ;
154168 }
155169
@@ -166,7 +180,8 @@ public function setValue( $value ): self {
166180 }
167181
168182 $ this ->value = $ value ;
169- $ this ->isDirty = $ value !== $ this ->originalValue ;
183+ $ this ->isValueSet = true ;
184+ $ this ->isDirty = ! $ this ->isOriginalValueSet || $ value !== $ this ->originalValue ;
170185
171186 return $ this ;
172187 }
@@ -177,12 +192,10 @@ public function setValue( $value ): self {
177192 * @since 2.0.0
178193 */
179194 public function unset (): void {
180- // Only attempt to unset if the property is already set
181- if (isset ($ this ->value )) {
182- unset( $ this ->value );
183- }
195+ // Mark the value as unset
196+ $ this ->isValueSet = false ;
184197
185198 // If the orginal value had a value we have now deviated
186- $ this ->isDirty = isset ( $ this ->originalValue ) ;
199+ $ this ->isDirty = $ this ->isOriginalValueSet ;
187200 }
188201}
0 commit comments