Transformers
1 year ago
BaseLocationCountryRuleProcessor.php
2 years ago
BaseLocationStateRuleProcessor.php
2 years ago
ComparisonOperation.php
2 years ago
ContextPluginsRuleProcessor.php
1 year ago
EvaluateAndGetStatus.php
2 years ago
EvaluateOverrides.php
1 year ago
EvaluationLogger.php
1 year ago
FailRuleProcessor.php
2 years ago
GetRuleProcessor.php
2 years ago
GetRuleProcessorForContext.php
1 year ago
IsEcommerceRuleProcessor.php
2 years ago
IsWooExpressRuleProcessor.php
2 years ago
NotRuleProcessor.php
2 years ago
NoteStatusRuleProcessor.php
2 years ago
OnboardingProfileRuleProcessor.php
2 years ago
OptionRuleProcessor.php
2 years ago
OrRuleProcessor.php
2 years ago
OrderCountRuleProcessor.php
2 years ago
OrdersProvider.php
2 years ago
PassRuleProcessor.php
2 years ago
PluginVersionRuleProcessor.php
2 years ago
PluginsActivatedRuleProcessor.php
2 years ago
ProductCountRuleProcessor.php
1 year ago
PublishAfterTimeRuleProcessor.php
2 years ago
PublishBeforeTimeRuleProcessor.php
2 years ago
RuleEvaluator.php
1 year ago
RuleProcessorInterface.php
2 years ago
StoredStateRuleProcessor.php
2 years ago
StoredStateSetupForProducts.php
2 years ago
TotalPaymentsVolumeProcessor.php
1 year ago
WCAdminActiveForProvider.php
2 years ago
WCAdminActiveForRuleProcessor.php
2 years ago
WooCommerceAdminUpdatedRuleProcessor.php
2 years ago
ComparisonOperation.php
77 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Compare two operands using the specified operation. |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | /** |
| 11 | * Compare two operands using the specified operation. |
| 12 | */ |
| 13 | class ComparisonOperation { |
| 14 | /** |
| 15 | * Compare two operands using the specified operation. |
| 16 | * |
| 17 | * @param object $left_operand The left hand operand. |
| 18 | * @param object $right_operand The right hand operand -- 'value' from the rule definition. |
| 19 | * @param string $operation The operation used to compare the operands. |
| 20 | */ |
| 21 | public static function compare( $left_operand, $right_operand, $operation ) { |
| 22 | switch ( $operation ) { |
| 23 | case '=': |
| 24 | return $left_operand === $right_operand; |
| 25 | case '<': |
| 26 | return $left_operand < $right_operand; |
| 27 | case '<=': |
| 28 | return $left_operand <= $right_operand; |
| 29 | case '>': |
| 30 | return $left_operand > $right_operand; |
| 31 | case '>=': |
| 32 | return $left_operand >= $right_operand; |
| 33 | case '!=': |
| 34 | return $left_operand !== $right_operand; |
| 35 | case 'contains': |
| 36 | if ( is_array( $left_operand ) && is_string( $right_operand ) ) { |
| 37 | return in_array( $right_operand, $left_operand, true ); |
| 38 | } |
| 39 | if ( is_string( $right_operand ) && is_string( $left_operand ) ) { |
| 40 | return strpos( $right_operand, $left_operand ) !== false; |
| 41 | } |
| 42 | break; |
| 43 | case '!contains': |
| 44 | if ( is_array( $left_operand ) && is_string( $right_operand ) ) { |
| 45 | return ! in_array( $right_operand, $left_operand, true ); |
| 46 | } |
| 47 | if ( is_string( $right_operand ) && is_string( $left_operand ) ) { |
| 48 | return strpos( $right_operand, $left_operand ) === false; |
| 49 | } |
| 50 | break; |
| 51 | case 'in': |
| 52 | if ( is_array( $right_operand ) && is_string( $left_operand ) ) { |
| 53 | return in_array( $left_operand, $right_operand, true ); |
| 54 | } |
| 55 | if ( is_string( $left_operand ) && is_string( $right_operand ) ) { |
| 56 | return strpos( $left_operand, $right_operand ) !== false; |
| 57 | } |
| 58 | break; |
| 59 | case '!in': |
| 60 | if ( is_array( $right_operand ) && is_string( $left_operand ) ) { |
| 61 | return ! in_array( $left_operand, $right_operand, true ); |
| 62 | } |
| 63 | if ( is_string( $left_operand ) && is_string( $right_operand ) ) { |
| 64 | return strpos( $left_operand, $right_operand ) === false; |
| 65 | } |
| 66 | break; |
| 67 | case 'range': |
| 68 | if ( ! is_array( $right_operand ) || count( $right_operand ) !== 2 ) { |
| 69 | return false; |
| 70 | } |
| 71 | return $left_operand >= $right_operand[0] && $left_operand <= $right_operand[1]; |
| 72 | } |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | } |
| 77 |