rest_handle_multi_type_schema

函式
rest_handle_multi_type_schema ( $value, $args, $param = '' )
引數
  • (mixed) $value The value to check.
    Required:
  • (array) $args The schema array to use.
    Required:
  • (string) $param The parameter name, used in error messages.
    Required:
    Default: (empty)
返回值
  • (string)
定義位置
相關方法
rest_find_one_matching_schemarest_find_matching_pattern_property_schemarest_validate_null_value_from_schemarest_find_any_matching_schemarest_validate_value_from_schema
引入
5.5.0
棄用
-

rest_handle_multi_type_schema: 這個函式用來處理一個允許多種型別的模式。它接受兩個引數,第一個是允許多種型別的模式,第二個是代表正在驗證的資料型別的字串。如果根據模式不允許資料型別,它返回一個錯誤陣列。

處理為多型別模式獲取最佳型別的問題。

這是{@see}的一個封裝器,用於處理使用無效型別的模式的後向相容性。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function rest_handle_multi_type_schema( $value, $args, $param = '' ) {
$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
$invalid_types = array_diff( $args['type'], $allowed_types );
if ( $invalid_types ) {
_doing_it_wrong(
__FUNCTION__,
/* translators: 1: Parameter, 2: List of allowed types. */
wp_sprintf( __( 'The "type" schema keyword for %1$s can only contain the built-in types: %2$l.' ), $param, $allowed_types ),
'5.5.0'
);
}
$best_type = rest_get_best_type_for_value( $value, $args['type'] );
if ( ! $best_type ) {
if ( ! $invalid_types ) {
return '';
}
// Backward compatibility for previous behavior which allowed the value if there was an invalid type used.
$best_type = reset( $invalid_types );
}
return $best_type;
}
function rest_handle_multi_type_schema( $value, $args, $param = '' ) { $allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' ); $invalid_types = array_diff( $args['type'], $allowed_types ); if ( $invalid_types ) { _doing_it_wrong( __FUNCTION__, /* translators: 1: Parameter, 2: List of allowed types. */ wp_sprintf( __( 'The "type" schema keyword for %1$s can only contain the built-in types: %2$l.' ), $param, $allowed_types ), '5.5.0' ); } $best_type = rest_get_best_type_for_value( $value, $args['type'] ); if ( ! $best_type ) { if ( ! $invalid_types ) { return ''; } // Backward compatibility for previous behavior which allowed the value if there was an invalid type used. $best_type = reset( $invalid_types ); } return $best_type; }
function rest_handle_multi_type_schema( $value, $args, $param = '' ) {
	$allowed_types = array( 'array', 'object', 'string', 'number', 'integer', 'boolean', 'null' );
	$invalid_types = array_diff( $args['type'], $allowed_types );

	if ( $invalid_types ) {
		_doing_it_wrong(
			__FUNCTION__,
			/* translators: 1: Parameter, 2: List of allowed types. */
			wp_sprintf( __( 'The "type" schema keyword for %1$s can only contain the built-in types: %2$l.' ), $param, $allowed_types ),
			'5.5.0'
		);
	}

	$best_type = rest_get_best_type_for_value( $value, $args['type'] );

	if ( ! $best_type ) {
		if ( ! $invalid_types ) {
			return '';
		}

		// Backward compatibility for previous behavior which allowed the value if there was an invalid type used.
		$best_type = reset( $invalid_types );
	}

	return $best_type;
}

常見問題

FAQs
檢視更多 >