MailPoet\EmailEditor\Validator

Validator::validate_and_sanitize_one_of()privateWC 1.0

Mirrors rest_find_one_matching_schema().

Method of the class: Validator{}

No Hooks.

Return

Mixed|WP_Error.

Usage

// private - for code of main (parent) class only
$result = $this->validate_and_sanitize_one_of( $value, $one_of_schema, $param_name );
$value(mixed) (required)
The value to validate.
$one_of_schema(array) (required)
The schema to validate against.
$param_name(string) (required)
The parameter name.

Validator::validate_and_sanitize_one_of() code WC 9.8.1

private function validate_and_sanitize_one_of( $value, array $one_of_schema, string $param_name ) {
	$matching_schemas = array();
	$errors           = array();
	$data             = null;
	foreach ( $one_of_schema['oneOf'] as $index => $schema ) {
		$result = $this->validate_and_sanitize_value_from_schema( $value, $schema, $param_name );
		if ( is_wp_error( $result ) ) {
			$errors[] = array(
				'error_object' => $result,
				'schema'       => $schema,
				'index'        => $index,
			);
		} else {
			$data                       = $result;
			$matching_schemas[ $index ] = $schema;
		}
	}

	if ( ! $matching_schemas ) {
		/* @phpstan-ignore-next-line Wrong annotation for parameter in WP. */
		return rest_get_combining_operation_error( $value, $param_name, $errors );
	}

	if ( count( $matching_schemas ) > 1 ) {
		// reuse WP method to generate detailed error.
		$invalid_schema = array( 'type' => array() );
		$one_of         = array_replace( array_fill( 0, count( $one_of_schema['oneOf'] ), $invalid_schema ), $matching_schemas );
		return rest_find_one_matching_schema( $value, array( 'oneOf' => $one_of ), $param_name );
	}
	return $data;
}