Automattic\WooCommerce\Internal\Admin\Settings

PaymentsRestController::prepare_payment_providers_response_recursiveprivateWC 1.0

Recursively prepare the response items for the GET payment providers request.

Method of the class: PaymentsRestController{}

No Hooks.

Returns

Mixed. The prepared response item.

Usage

// private - for code of main (parent) class only
$result = $this->prepare_payment_providers_response_recursive( $response_item, $schema );
$response_item(mixed) (required)
The response item to prepare.
$schema(array) (required)
The schema to use for preparing the response.

PaymentsRestController::prepare_payment_providers_response_recursive() code WC 9.8.5

private function prepare_payment_providers_response_recursive( $response_item, array $schema ) {
	if ( is_null( $response_item ) ||
		! array_key_exists( 'properties', $schema ) ||
		! is_array( $schema['properties'] ) ) {
		return $response_item;
	}

	$prepared_response = array();
	foreach ( $schema['properties'] as $key => $property_schema ) {
		if ( is_array( $response_item ) && array_key_exists( $key, $response_item ) ) {
			if ( is_array( $property_schema ) && array_key_exists( 'properties', $property_schema ) ) {
				$prepared_response[ $key ] = $this->prepare_payment_providers_response_recursive( $response_item[ $key ], $property_schema );
			} elseif ( is_array( $property_schema ) && array_key_exists( 'items', $property_schema ) ) {
				$prepared_response[ $key ] = array_map(
					fn( $item ) => $this->prepare_payment_providers_response_recursive( $item, $property_schema['items'] ),
					$response_item[ $key ]
				);
			} else {
				$prepared_response[ $key ] = $response_item[ $key ];
			}
		}
	}

	// Ensure the order is the same as in the schema.
	$prepared_response = array_merge( array_fill_keys( array_keys( $schema['properties'] ), null ), $prepared_response );

	// Remove any null values from the response.
	$prepared_response = array_filter( $prepared_response, fn( $value ) => ! is_null( $value ) );

	return $prepared_response;
}