rest_get_best_type_for_value

The timeline below displays how wordpress function rest_get_best_type_for_value has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Gets the best type for a value.
 *
 * @since 5.5.0
 *
 * @param mixed    $value The value to check.
 * @param string[] $types The list of possible types.
 * @return string The best matching type, an empty string if no types match.
 */
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 '';
}

WordPress Version: 6.2

/**
 * Gets the best type for a value.
 *
 * @since 5.5.0
 *
 * @param mixed    $value The value to check.
 * @param string[] $types The list of possible types.
 * @return string The best matching type, an empty string if no types match.
 */
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 '';
}

WordPress Version: 5.5

/**
 * Gets the best type for a value.
 *
 * @since 5.5.0
 *
 * @param mixed $value The value to check.
 * @param array $types The list of possible types.
 * @return string The best matching type, an empty string if no types match.
 */
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 '';
}