Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: update text block conversion to use text block type #7697

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: always set default properties and always cast values as intended…
… to avoid unexpected type errors
  • Loading branch information
Jon Waldstein committed Jan 24, 2025
commit e9cd42dfafd0cf80a2a3ea4c9928eb39e60b535f
12 changes: 4 additions & 8 deletions src/Framework/Blocks/BlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,11 @@ public function isAttributeTypeValid(string $key, $value): bool
}

/**
* @unreleased updated to always cast value even if null
* @since 3.8.0
*/
public function castAttributeType(string $key, $value)
{
if (is_null($value)) {
return null;
}

$type = $this->getPropertyType($key);

switch ($type) {
Expand All @@ -178,7 +175,7 @@ public function castAttributeType(string $key, $value)
case 'string':
return (string)($value);
case 'bool':
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
return (bool)filter_var($value, FILTER_VALIDATE_BOOLEAN);
case 'array':
return (array)($value);
case 'float':
Expand Down Expand Up @@ -224,14 +221,13 @@ protected function setDefaultProperties(): array
}

/**
* @unreleased updated to set always default properties
* @since 3.8.0
*/
private function fillDefaultProperties(): void
{
foreach ($this->setDefaultProperties() as $key => $type) {
if ($this->hasAttribute($key)) {
$this->properties[$key] = $type;
}
$this->properties[$key] = $type;
}
}
}
39 changes: 39 additions & 0 deletions tests/Unit/Framework/Blocks/TestBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,45 @@ public static function name(): string
$this->assertIsFloat($blockType->intFloatAttribute);
}

/**
* @unreleased
*/
public function testShouldCastNullValuesAsIntendedType(): void
{
$blockModel = BlockModel::make([
'name' => 'givewp/donation-amount',
'attributes' => [
'floatAttribute' => null,
'stringAttribute' => null,
'intAttribute' => null,
'boolAttribute' => null,
]
]);

$blockType = new class ($blockModel) extends BlockType {
protected $properties = [
'floatAttribute' => 'float',
'stringAttribute' => 'string',
'intAttribute' => 'int',
'boolAttribute' => 'bool',
'extraStringAttribute' => 'string',
'extraBoolAttribute' => 'bool',
];

public static function name(): string
{
return 'givewp/donation-amount';
}
};

$this->assertIsFloat($blockType->floatAttribute);
$this->assertIsString($blockType->stringAttribute);
$this->assertIsInt($blockType->intAttribute);
$this->assertIsBool($blockType->boolAttribute);
$this->assertIsBool($blockType->extraBoolAttribute);
$this->assertIsString($blockType->extraStringAttribute);
}

/**
* @since 3.8.0
*
Expand Down
Loading