Skip to content

Commit

Permalink
Merge pull request #11 from stellarwp/fix/nullable-default-conflict-w…
Browse files Browse the repository at this point in the history
…ith-mysql-default

Bug fix for fetching set of defaults, don't always add a null.
  • Loading branch information
stratease authored May 10, 2024
2 parents 1e364cc + 4f43a8d commit 7c21d23
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ 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.
*
* @unreleased
*
* @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.
*
* @unreleased
*
* @param string $key Property name.
*
* @return bool
*/
protected function hasDefault( string $key ): bool {
return is_array( $this->properties[ $key ] ) && array_key_exists( 1, $this->properties[ $key ] );
}

/**
* Returns the default value for a property if one is provided, otherwise null.
*
Expand All @@ -134,9 +159,11 @@ public function getOriginal( string $key = null ) {
* @return mixed|null
*/
protected function getPropertyDefault( string $key ) {
return is_array( $this->properties[ $key ] ) && isset( $this->properties[ $key ][1] )
? $this->properties[ $key ][1]
: null;
if ( $this->hasDefault( $key ) ) {
return $this->properties[ $key ][1];
}

return null;
}

/**
Expand All @@ -149,7 +176,9 @@ protected function getPropertyDefault( string $key ) {
protected function getPropertyDefaults() : array {
$defaults = [];
foreach ( array_keys( $this->properties ) as $property ) {
$defaults[ $property ] = $this->getPropertyDefault( $property );
if ( $this->hasDefault( $property ) ) {
$defaults[ $property ] = $this->getPropertyDefault( $property );
}
}

return $defaults;
Expand Down
19 changes: 19 additions & 0 deletions tests/wpunit/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,25 @@ public function testShouldSetMultipleAttributes() {
$this->assertEquals( 'Skywalker', $model->lastName );
}

/**
* @unreleased
*
* @return void
*/
public function testIsSet() {
$model = new MockModel();

// 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' ) );
}

/**
* @since 1.0.0
*
Expand Down

0 comments on commit 7c21d23

Please sign in to comment.