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
EvaluateOverrides.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors; |
| 6 | |
| 7 | /** |
| 8 | * Evaluates `overrides` property in the spec and returns the evaluated spec. |
| 9 | */ |
| 10 | class EvaluateOverrides { |
| 11 | /** |
| 12 | * Evaluates the spec and returns a status. |
| 13 | * |
| 14 | * @param array $spec The spec to evaluate. |
| 15 | * @param array $context The context variables. |
| 16 | * |
| 17 | * @return array The evaluated spec. |
| 18 | */ |
| 19 | public function evaluate( array $spec, array $context = array() ) { |
| 20 | $rule_evaluator = new RuleEvaluator( new GetRuleProcessorForContext( $context ) ); |
| 21 | |
| 22 | foreach ( $spec as $spec_item ) { |
| 23 | if ( isset( $spec_item->overrides ) && is_array( $spec_item->overrides ) ) { |
| 24 | foreach ( $spec_item->overrides as $override ) { |
| 25 | if ( ! isset( $override->rules ) || ! is_array( $override->rules ) || ! isset( $override->field ) || ! isset( $override->value ) ) { |
| 26 | continue; |
| 27 | } |
| 28 | |
| 29 | if ( $rule_evaluator->evaluate( $override->rules ) ) { |
| 30 | // If value exisit and can be accessed directly, update it. |
| 31 | if ( isset( $spec_item->{$override->field} ) ) { |
| 32 | $spec_item->{$override->field} = $override->value; |
| 33 | } else { |
| 34 | // Otherwise, try updating it using dot notation. |
| 35 | $this->set_value_with_dot_notation( $spec_item, $override->field, $override->value ); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return $spec; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Set a new value to $data with dot notation. |
| 47 | * |
| 48 | * This is a slightly modified version of the simple dot notation to support objects. |
| 49 | * |
| 50 | * @param mixed $data The data to update. |
| 51 | * @param string $path The path to the value to update. |
| 52 | * @param mixed $new_value The new value. |
| 53 | * |
| 54 | * @return mixed|\stdClass |
| 55 | */ |
| 56 | public function set_value_with_dot_notation( &$data, $path, $new_value ) { |
| 57 | $keys = explode( '.', $path ); |
| 58 | $last_key = array_pop( $keys ); |
| 59 | |
| 60 | foreach ( $keys as $key ) { |
| 61 | if ( is_numeric( $key ) ) { |
| 62 | $key = (int) $key; |
| 63 | if ( ! isset( $data[ $key ] ) || ! is_object( $data[ $key ] ) ) { |
| 64 | $data[ $key ] = new \stdClass(); |
| 65 | } |
| 66 | $data = &$data[ $key ]; |
| 67 | } else { |
| 68 | if ( ! isset( $data->$key ) || ( ! is_array( $data->$key ) && ! is_object( $data->$key ) ) ) { |
| 69 | $data->$key = new \stdClass(); |
| 70 | } |
| 71 | $data = &$data->$key; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Assign the new value. |
| 76 | if ( is_numeric( $last_key ) ) { |
| 77 | $data[ (int) $last_key ] = $new_value; |
| 78 | } else { |
| 79 | $data->$last_key = $new_value; |
| 80 | } |
| 81 | |
| 82 | return $data; |
| 83 | } |
| 84 | } |
| 85 |