Automattic\WooCommerce\Internal\Shipping\FlatRate

WeightPlaceholder::validate │ public │ WC 11.3.0

Validate weight limits before saving a cost formula.

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

Хуков нет.

Возвращает

void. Ничего (null).

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

$WeightPlaceholder = new WeightPlaceholder();
$WeightPlaceholder->validate( $sum ): void;
$sum(строка) (обязательный)
Cost formula.

Список изменений

С версии 11.3.0 Введена.

Код WeightPlaceholder::validate() WC 11.3.0

public function validate( string $sum ): void {
	preg_match_all( self::PLACEHOLDER_PATTERN, $sum, $matches, PREG_SET_ORDER );

	foreach ( $matches as $match ) {
		$atts = (array) shortcode_parse_atts( $match['attributes'] );

		foreach ( array( 'min', 'max' ) as $attribute ) {
			$limit = $atts[ $attribute ] ?? '';

			if ( '' !== $limit && ( ! is_numeric( $limit ) || (float) $limit < 0 ) ) {
				throw new \InvalidArgumentException(
					sprintf(
						/* translators: %s: weight placeholder attribute, either min or max. */
						esc_html__( 'The [weight] %s value must be a non-negative number with a dot decimal separator and no thousands separators, e.g. 1000.5.', 'woocommerce' ),
						esc_html( $attribute )
					)
				);
			}
		}

		$min = $atts['min'] ?? '';
		$max = $atts['max'] ?? '';

		if ( '' !== $min && '' !== $max && (float) $min > (float) $max ) {
			throw new \InvalidArgumentException( esc_html__( 'The [weight] min value cannot be greater than the max value.', 'woocommerce' ) );
		}
	}
}