Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers

TransformerService::apply()public staticWC 1.0

Apply transformers to the given value.

Метод класса: TransformerService{}

Хуков нет.

Возвращает

Разное|null.

Использование

$result = TransformerService::apply( $target_value, $transformer_configs, $is_default_set, $default_value );
$target_value(разное) (обязательный)
a value to transform.
$transformer_configs(массив) (обязательный)
transform configuration.
$is_default_set(true|false) (обязательный)
flag on is default value set.
$default_value(строка) (обязательный)
default value.

Код TransformerService::apply() WC 9.4.2

public static function apply( $target_value, array $transformer_configs, $is_default_set, $default_value ) {
	foreach ( $transformer_configs as $transformer_config ) {
		if ( ! isset( $transformer_config->use ) ) {
			throw new InvalidArgumentException( 'Missing required config value: use' );
		}

		if ( ! isset( $transformer_config->arguments ) ) {
			$transformer_config->arguments = null;
		}

		$transformer = self::create_transformer( $transformer_config->use );
		if ( null === $transformer ) {
			throw new InvalidArgumentException( "Unable to find a transformer by name: {$transformer_config->use}" ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
		}

		$target_value = $transformer->transform( $target_value, $transformer_config->arguments, $is_default_set ? $default_value : null );

		// Break early when there's no more value to traverse.
		if ( null === $target_value ) {
			break;
		}
	}

	if ( $is_default_set ) {
		// Nulls always return the default value.
		if ( null === $target_value ) {
			return $default_value;
		}

		// When type of the default value is different from the target value, return the default value
		// to ensure type safety.
		if ( gettype( $default_value ) !== gettype( $target_value ) ) {
			return $default_value;
		}
	}

	return $target_value;
}