Skip to content

Commit 1dc5cef

Browse files
Changes how model properties are determined as set (#28)
1 parent 9d33cd7 commit 1dc5cef

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

src/Models/ModelProperty.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

tests/wpunit/ModelPropertyTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,39 @@ public function testRevertChanges() {
184184
$this->assertSame('John', $property->getOriginalValue());
185185
}
186186

187+
/**
188+
* @since 2.0.0
189+
*
190+
* @covers ::isSet
191+
*/
192+
public function testIsSet() {
193+
$definition = new ModelPropertyDefinition();
194+
195+
// Test with initial value
196+
$property = new ModelProperty('name', $definition, 'John');
197+
$this->assertTrue($property->isSet());
198+
199+
// Test with no initial value
200+
$property = new ModelProperty('name', $definition);
201+
$this->assertFalse($property->isSet());
202+
203+
// Test with default value
204+
$definition = (new ModelPropertyDefinition())->default('default-value');
205+
$property = new ModelProperty('name', $definition);
206+
$this->assertTrue($property->isSet());
207+
208+
// Test with null value
209+
$definition = (new ModelPropertyDefinition())->nullable();
210+
$property = new ModelProperty('name', $definition);
211+
$this->assertFalse($property->isSet());
212+
$property->setValue(null);
213+
$this->assertTrue($property->isSet());
214+
215+
// Test that a null initial value is also considered set
216+
$property = new ModelProperty('name', $definition, null);
217+
$this->assertTrue($property->isSet());
218+
}
219+
187220
/**
188221
* @since 2.0.0
189222
*

0 commit comments

Comments
 (0)