Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors

EvaluateOverrides::set_value_with_dot_notationpublicWC 1.0

Set a new value to $data with dot notation.

This is a slightly modified version of the simple dot notation to support objects.

Method of the class: EvaluateOverrides{}

No Hooks.

Returns

Mixed|\stdClass.

Usage

$EvaluateOverrides = new EvaluateOverrides();
$EvaluateOverrides->set_value_with_dot_notation( $data, $path, $new_value );
$data(mixed) (required) (passed by reference — &)
The data to update.
$path(string) (required)
The path to the value to update.
$new_value(mixed) (required)
The new value.

EvaluateOverrides::set_value_with_dot_notation() code WC 9.9.5

public function set_value_with_dot_notation( &$data, $path, $new_value ) {
	$keys     = explode( '.', $path );
	$last_key = array_pop( $keys );

	foreach ( $keys as $key ) {
		if ( is_numeric( $key ) ) {
			$key = (int) $key;
			if ( ! isset( $data[ $key ] ) || ! is_object( $data[ $key ] ) ) {
				$data[ $key ] = new \stdClass();
			}
			$data = &$data[ $key ];
		} else {
			if ( ! isset( $data->$key ) || ( ! is_array( $data->$key ) && ! is_object( $data->$key ) ) ) {
				$data->$key = new \stdClass();
			}
			$data = &$data->$key;
		}
	}

	// Assign the new value.
	if ( is_numeric( $last_key ) ) {
		$data[ (int) $last_key ] = $new_value;
	} else {
		$data->$last_key = $new_value;
	}

	return $data;
}