Automattic\WooCommerce\Internal\Abilities\REST
RestAbilityFactory::normalize_type
Normalize a schema type value.
Handles both string types ('string', 'date-time', etc.) and array types (['string', 'null']) used for nullable fields.
Method of the class: RestAbilityFactory{}
No Hooks.
Returns
Array. Schema with normalized type (or type removed if all invalid).
Usage
$result = RestAbilityFactory::normalize_type( $schema, $type ): array;
- $schema(array) (required)
- The schema node being built.
- $type(string|array) (required)
- The type value to normalize.
RestAbilityFactory::normalize_type() RestAbilityFactory::normalize type code WC 10.7.0
private static function normalize_type( array $schema, $type ): array {
if ( is_string( $type ) ) {
if ( 'date-time' === $type ) {
$schema['type'] = 'string';
if ( ! isset( $schema['format'] ) ) {
$schema['format'] = 'date-time';
}
} elseif ( 'action' === $type ) {
$schema['type'] = 'object';
} elseif ( in_array( $type, self::$valid_types, true ) ) {
$schema['type'] = $type;
} else {
unset( $schema['type'] );
}
return $schema;
}
if ( is_array( $type ) ) {
$normalized = array();
foreach ( $type as $single ) {
if ( ! is_string( $single ) ) {
continue;
}
if ( 'date-time' === $single ) {
$single = 'string';
if ( ! isset( $schema['format'] ) ) {
$schema['format'] = 'date-time';
}
} elseif ( 'action' === $single ) {
$single = 'object';
} elseif ( ! in_array( $single, self::$valid_types, true ) ) {
continue;
}
$normalized[] = $single;
}
$normalized = array_values( array_unique( $normalized ) );
if ( empty( $normalized ) ) {
unset( $schema['type'] );
} elseif ( 1 === count( $normalized ) ) {
$schema['type'] = $normalized[0];
} else {
$schema['type'] = $normalized;
}
return $schema;
}
// Non-string, non-array type — remove it.
unset( $schema['type'] );
return $schema;
}