Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors

ComparisonOperation::compare()public staticWC 1.0

Compare two operands using the specified operation.

Method of the class: ComparisonOperation{}

No Hooks.

Return

null. Nothing (null).

Usage

$result = ComparisonOperation::compare( $left_operand, $right_operand, $operation );
$left_operand(object) (required)
The left hand operand.
$right_operand(object) (required)
The right hand operand -- 'value' from the rule definition.
$operation(string) (required)
The operation used to compare the operands.

ComparisonOperation::compare() code WC 9.0.1

public static function compare( $left_operand, $right_operand, $operation ) {
	switch ( $operation ) {
		case '=':
			return $left_operand === $right_operand;
		case '<':
			return $left_operand < $right_operand;
		case '<=':
			return $left_operand <= $right_operand;
		case '>':
			return $left_operand > $right_operand;
		case '>=':
			return $left_operand >= $right_operand;
		case '!=':
			return $left_operand !== $right_operand;
		case 'contains':
			if ( is_array( $left_operand ) && is_string( $right_operand ) ) {
				return in_array( $right_operand, $left_operand, true );
			}
			if ( is_string( $right_operand ) && is_string( $left_operand ) ) {
				return strpos( $right_operand, $left_operand ) !== false;
			}
			break;
		case '!contains':
			if ( is_array( $left_operand ) && is_string( $right_operand ) ) {
				return ! in_array( $right_operand, $left_operand, true );
			}
			if ( is_string( $right_operand ) && is_string( $left_operand ) ) {
				return strpos( $right_operand, $left_operand ) === false;
			}
			break;
		case 'in':
			if ( is_array( $right_operand ) && is_string( $left_operand ) ) {
				return in_array( $left_operand, $right_operand, true );
			}
			if ( is_string( $left_operand ) && is_string( $right_operand ) ) {
				return strpos( $left_operand, $right_operand ) !== false;
			}
			break;
		case '!in':
			if ( is_array( $right_operand ) && is_string( $left_operand ) ) {
				return ! in_array( $left_operand, $right_operand, true );
			}
			if ( is_string( $left_operand ) && is_string( $right_operand ) ) {
				return strpos( $left_operand, $right_operand ) === false;
			}
			break;
		case 'range':
			if ( ! is_array( $right_operand ) || count( $right_operand ) !== 2 ) {
				return false;
			}
			return $left_operand >= $right_operand[0] && $left_operand <= $right_operand[1];
	}

	return false;
}