You are here

protected function ConfigSorter::sortDeep in Configuration Split 2.0.x

Sort one array with the sorting order of another.

Parameters

array $config: The array to sort.

array $model: The array to get the sorting order from.

Return value

array The sorted array.

1 call to ConfigSorter::sortDeep()
ConfigSorter::sort in src/Config/ConfigSorter.php
Cast and sort the config data in a normalised way depending on its schema.

File

src/Config/ConfigSorter.php, line 152

Class

ConfigSorter
The config sorter service core should have had.

Namespace

Drupal\config_split\Config

Code

protected function sortDeep(array $config, array $model) : array {
  if ($config === $model) {

    // Shortcut.
    return $config;
  }
  $sorted = [];
  $common = array_intersect_key($model, $config);
  $unique = array_diff_key($config, $model);
  foreach ($common as $key => $modelValue) {
    $value = $config[$key];

    // We maybe need to differentiate between mappings and sequences, use the
    // config schema and all. But as long as core doesn't give us any help we
    // just sort in the most crude way to get the job done.
    if (is_array($modelValue) && is_array($value) && !empty($value)) {

      // Recurse into nested values.
      $value = $this
        ->sortDeep($value, $modelValue);
    }

    // Fill the $sorted array in the same order as the model.
    $sorted[$key] = $value;
  }
  foreach ($unique as $key => $value) {

    // The values that do not exist in the model do not need to be sorted.
    $sorted[$key] = $value;
  }
  return $sorted;
}