rest_get_best_type_for_value( mixed $value, array $types )

Gets the best type for a value.

Parameters

$value

(mixed) (Required) The value to check.

$types

(array) (Required) The list of possible types.

Return

(string) The best matching type, an empty string if no types match.

Source

File: wp-includes/rest-api.php

function rest_get_best_type_for_value( $value, $types ) {
	static $checks = array(
		'array'   => 'rest_is_array',
		'object'  => 'rest_is_object',
		'integer' => 'rest_is_integer',
		'number'  => 'is_numeric',
		'boolean' => 'rest_is_boolean',
		'string'  => 'is_string',
		'null'    => 'is_null',
	);

	// Both arrays and objects allow empty strings to be converted to their types.
	// But the best answer for this type is a string.
	if ( '' === $value && in_array( 'string', $types, true ) ) {
		return 'string';
	}

	foreach ( $types as $type ) {
		if ( isset( $checks[ $type ] ) && $checks[ $type ]( $value ) ) {
			return $type;
		}
	}

	return '';
}

Changelog

Version Description
5.5.0 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/rest_get_best_type_for_value