Skip to content

Commit

Permalink
Adding isSet, changing scope of hasDefault to protected.
Browse files Browse the repository at this point in the history
  • Loading branch information
stratease committed May 10, 2024
1 parent fb609c0 commit 5361486
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ public function getOriginal( string $key = null ) {
return $key ? $this->original[ $key ] : $this->original;
}

/**
* Whether the property is set or not. This is different from isset() because this considers a `null` value as
* being set. Defaults are considered set as well.
*
* @since TBD
*
* @return boolean
*/
public function isSet( string $key ): bool {
return array_key_exists( $key, $this->attributes ) || $this->hasDefault( $key );
}

/**
* Check if there is a default value for a property.
*
Expand All @@ -133,7 +145,7 @@ public function getOriginal( string $key = null ) {
*
* @return bool
*/
public function hasDefault( string $key ): bool {
protected function hasDefault( string $key ): bool {
return is_array( $this->properties[ $key ] ) && array_key_exists( 1, $this->properties[ $key ] );
}

Expand Down
15 changes: 11 additions & 4 deletions tests/wpunit/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,18 @@ public function testShouldSetMultipleAttributes() {
*
* @return void
*/
public function testHasDefaultValue() {
public function testIsSet() {
$model = new MockModel();
$this->assertTrue( $model->hasDefault( 'firstName' ) );
$this->assertTrue( $model->hasDefault( 'emails' ) );
$this->assertFalse( $model->hasDefault( 'lastName' ) );

// This has a default so we should see as set.
$this->assertTrue( $model->isSet( 'firstName' ) );

// No default, and hasn't been set so show false.
$this->assertFalse( $model->isSet( 'lastName' ) );

// Now we set it, so it should be true - even though we set it to null.
$model->lastName = null;
$this->assertTrue( $model->isSet( 'lastName' ) );
}

/**
Expand Down

0 comments on commit 5361486

Please sign in to comment.