function drush_value_format

8.0.x drush.inc drush_value_format($value, $format)
master drush.inc drush_value_format($value, $format)
2 calls to drush_value_format()
drush_state_set in commands/core/state.drush.inc
State set command callback.
drush_variable_set in commands/core/variable.drush.inc
Command callback. Set a variable.

File

includes/drush.inc, line 1779
The drush API implementation and helpers.

Code

function drush_value_format($value, $format) {
  if ($format == 'auto') {
    if (is_numeric($value)) {
      $value = $value + 0; // http://php.net/manual/en/function.is-numeric.php#107326
      $format = gettype($value);
    }
    elseif (($value == 'TRUE') || ($value == 'FALSE')) {
      $format = 'bool';
    }
  }

  // Now, we parse the object.
  switch ($format) {
    case 'integer':
      $value = (integer) $value;
      break;
      // from: http://php.net/gettype
      // for historical reasons "double" is returned in case of a float, and not simply "float"
    case 'double':
    case 'float':
      $value = (float) $value;
      break;
    case 'bool':
    case 'boolean':
      if ($value == 'TRUE') {
        $value = TRUE;
      }
      elseif ($value == 'FALSE') {
        $value = FALSE;
      }
      else {
        $value = (bool) $value;
      }
      break;

    case 'json':
      $value = drush_json_decode($value);
      break;

    case 'yaml':
      $value = \Symfony\Component\Yaml\Yaml::parse($value, FALSE, TRUE);
      break;
  }
  return $value;
}