You are here

function _features_sanitize in Features 7.2

Same name and namespace in other branches
  1. 6 features.export.inc \_features_sanitize()
  2. 7 features.export.inc \_features_sanitize()

Internal. "Sanitizes" a (nested) array (or object) recursively.

The following operations are performed on every recursion level:

  • Objects are converted to array via get_object_vars().
  • "Associative" arrays are sorted by key.
  • Non-associative/serial arrays are sorted by value.
  • Empty values and empty sub-trees are removed if $remove_empty is TRUE.

@internal This function is designed for internal use within @todo Needs unit tests. @todo A better name might be "normalize", not "sanitize".

Parameters

array|object|mixed $array: Array or object to be sanitized. If $array is an object, it will be converted to array via get_object_vars(). Any nested objects will be converted to array in the same way. If $array is not an array or object, it will be left as-is.

bool $remove_empty: If TRUE, remove null or empty values for assoc arrays. This will also remove empty arrays or objects nested in the hierarchy.

See also

features_sanitize().

1 call to _features_sanitize()
features_sanitize in ./features.export.inc
Helper function to "sanitize" an array or object.

File

./features.export.inc, line 1513
Contains functions that export configuration into feature modules.

Code

function _features_sanitize(&$array, $remove_empty = TRUE) {
  if (is_object($array)) {
    $array = get_object_vars($array);
  }
  if (is_array($array)) {
    $is_assoc = _features_is_assoc($array);
    if ($is_assoc) {
      ksort($array, SORT_STRING);
      if ($remove_empty) {
        $array = array_filter($array);
      }
    }
    else {
      sort($array);
    }
    foreach ($array as $k => $v) {
      if (is_array($v) or is_object($v)) {
        _features_sanitize($array[$k]);
        if ($remove_empty && $is_assoc && empty($array[$k])) {
          unset($array[$k]);
        }
      }
    }
  }
}