Skip to content

Commit

Permalink
Provides object typing on the fly functionality for the attributes of…
Browse files Browse the repository at this point in the history
… type object with typing provided in default.
  • Loading branch information
whoami-pwd committed Jan 24, 2025
1 parent ee1a658 commit 6f0a3fa
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion includes/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,13 @@ private function get_attribute_type( $name, $attribute, $prefix ) {
$type = 'Int';
break;
case 'array':
// Process attributes query.
if ( isset( $attribute['query'] ) ) {
$type = [ 'list_of' => $this->create_and_register_inner_object_type( $name, $attribute['query'], $prefix ) ];
break;
}

// Process scalar array or list of items.
$of_type = null;
if ( isset( $attribute['items'] ) ) {
$of_type = $this->get_attribute_type( $name, $attribute['items'], $prefix );
Expand All @@ -166,7 +168,13 @@ private function get_attribute_type( $name, $attribute, $prefix ) {
$type = null !== $of_type ? [ 'list_of' => $of_type ] : Scalar::ATTRIBUTES_ARRAY_TYPE_NAME;
break;
case 'object':
$type = Scalar::ATTRIBUTES_OBJECT_TYPE_NAME;
// Proceed with the typed object if typing provided, otherwise continue with scalar object.
$typed = [];
if ( ! empty( $attribute['default']['__typed'] ) ) {
$typed = $this->build_typed_object_config( $attribute['default'] );
}

$type = $typed ? $this->create_and_register_inner_object_type( $name, $typed, $prefix ) : Scalar::ATTRIBUTES_OBJECT_TYPE_NAME;
break;
}

Expand Down Expand Up @@ -241,6 +249,28 @@ private function create_and_register_inner_object_type( string $name, array $con
return $type;
}

/**
* Verifies if the default record has a typed object configuration for the object type and generates config for it.
*
* @param array $default_attribute Default record of the attribute.
*/
private function build_typed_object_config( $default_attribute ): array {
if ( ! is_array( $default_attribute['__typed'] ) ) {
return [];
}

return array_combine(
array_keys( $default_attribute['__typed'] ),
array_map(
static fn ( $key ) => [
'type' => $default_attribute['__typed'][ $key ],
'default' => $default_attribute[ $key ] ?? null,
],
array_keys( $default_attribute['__typed'] )
)
) ?: [];
}

/**
* Creates the new attribute fields for inner block types.
*
Expand Down

0 comments on commit 6f0a3fa

Please sign in to comment.