Automattic\WooCommerce\Internal\Fulfillments

FulfillmentsManager::get_best_parsing_resultprivateWC 1.0

Get the best parsing result from multiple results.

This method finds the provider with the highest ambiguity score from the results.

Method of the class: FulfillmentsManager{}

No Hooks.

Returns

Array. The best parsing result.

Usage

// private - for code of main (parent) class only
$result = $this->get_best_parsing_result( $results, $tracking_number ): array;
$results(array) (required)
The results from multiple providers.
$tracking_number(string) (required)
The tracking number being parsed.

FulfillmentsManager::get_best_parsing_result() code WC 10.3.3

private function get_best_parsing_result( array $results, string $tracking_number ): array {
	$best_result   = null;
	$best_provider = '';
	$best_score    = 0;
	foreach ( $results as $provider_key => $result ) {
		if ( ! isset( $result['ambiguity_score'] ) || ! is_numeric( $result['ambiguity_score'] ) ) {
			continue; // Skip if ambiguity score is not set or not numeric.
		}

		if ( is_null( $best_result ) || $result['ambiguity_score'] > $best_score ) {
			$best_result   = $result;
			$best_provider = $provider_key;
			$best_score    = $result['ambiguity_score'];
		}
	}
	return is_null( $best_result ) ? array() : array(
		'tracking_number'   => $tracking_number,
		'shipping_provider' => $best_provider,
		'tracking_url'      => $best_result['url'],
	);
}