Automattic\WooCommerce\Internal\Abilities\REST

RestAbilityFactory::should_widen_to_output_unionprivate staticWC 1.0

Decide whether an output-schema type should be widened to the full union.

Widens when the declared type is either a single scalar (integer, string, etc.) or an array union whose members are all scalars and/or null. Leaves the declaration alone if any compound type (object, array) appears, since the schema author was explicit about admitting a structured value.

Handles the WC quirk where fields declared as [integer, null] (e.g. low_stock_amount) are returned as empty strings when unset, which neither member of the declared union admits.

Method of the class: RestAbilityFactory{}

No Hooks.

Returns

true|false. True if the type should be replaced with {@see self::OUTPUT_SCALAR_UNION}.

Usage

$result = RestAbilityFactory::should_widen_to_output_union( $type ): bool;
$type(mixed) (required)
Schema type value (string, array, or other).

RestAbilityFactory::should_widen_to_output_union() code WC 11.0.1

private static function should_widen_to_output_union( $type ): bool {
	if ( is_string( $type ) ) {
		return in_array( $type, self::SCALAR_TYPES, true );
	}

	if ( ! is_array( $type ) || empty( $type ) ) {
		return false;
	}

	$widenable = array_merge( self::SCALAR_TYPES, array( 'null' ) );
	foreach ( $type as $member ) {
		if ( ! is_string( $member ) || ! in_array( $member, $widenable, true ) ) {
			return false;
		}
	}

	return true;
}