You are here

function autoban_var_json_export in Automatic IP ban (Autoban) 7

Export a variable in pretty formatted JSON.

1 call to autoban_var_json_export()
autoban_export_form in ./autoban.admin.inc
Export autoban rules.

File

./autoban.module, line 657
Main file for autoban module.

Code

function autoban_var_json_export($var, $prefix = '') {
  if (is_array($var) && $var) {

    // Defines whether we use a JSON array or object.
    $use_array = $var == array_values($var);
    $output = $use_array ? "[" : "{";
    foreach ($var as $key => $value) {
      if ($use_array) {
        $values[] = autoban_var_json_export($value, '  ');
      }
      else {
        $values[] = autoban_var_json_export((string) $key, '  ') . ' : ' . autoban_var_json_export($value, '  ');
      }
    }

    // Use several lines for long content. However for objects with a single
    // entry keep the key in the first line.
    if (strlen($content = implode(', ', $values)) > 70 && ($use_array || count($values) > 1)) {
      $output .= "\n  " . implode(",\n  ", $values) . "\n";
    }
    elseif (strpos($content, "\n") !== FALSE) {
      $output .= " " . $content . "\n";
    }
    else {
      $output .= " " . $content . ' ';
    }
    $output .= $use_array ? ']' : '}';
  }
  else {
    $output = drupal_json_encode($var);
  }
  if ($prefix) {
    $output = str_replace("\n", "\n{$prefix}", $output);
  }
  return $output;
}