You are here

function rules_action_data_convert in Rules 7.2

Action: Convert a value.

Related topics

1 string reference to 'rules_action_data_convert'
rules_data_action_info in modules/data.rules.inc
Implements hook_rules_action_info() on behalf of the pseudo data module.

File

modules/data.eval.inc, line 194
Contains rules integration for the data module needed during evaluation.

Code

function rules_action_data_convert($arguments, RulesPlugin $element, $state) {
  $value_info = $element
    ->getArgumentInfo('value');
  $from_type = $value_info['type'];
  $target_type = $arguments['type'];

  // First apply the rounding behavior if given.
  if (isset($arguments['rounding_behavior'])) {
    switch ($arguments['rounding_behavior']) {
      case 'up':
        $arguments['value'] = ceil($arguments['value']);
        break;
      case 'down':
        $arguments['value'] = floor($arguments['value']);
        break;
      default:
      case 'round':
        $arguments['value'] = round($arguments['value']);
        break;
    }
  }
  switch ($target_type) {
    case 'decimal':
      $result = floatval($arguments['value']);
      break;
    case 'integer':
      $result = intval($arguments['value']);
      break;
    case 'text':
      $result = strval($arguments['value']);
      break;
    case 'token':
      $result = strval($arguments['value']);
      break;
  }
  return array(
    'conversion_result' => $result,
  );
}