Unverified Commit 13d4f6c1 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3556987 by phenaproxima, penyaskito, fathershawn: Add a recursive...

Issue #3556987 by phenaproxima, penyaskito, fathershawn: Add a recursive sort-by-key function to SortArray

(cherry picked from commit 5071f112)
parent 83118cc0
Loading
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -125,4 +125,22 @@ public static function sortByKeyInt($a, $b, $key) {
    return $a_weight <=> $b_weight;
  }

  /**
   * Sorts an array recursively, by key, alphabetically.
   *
   * @param array $data
   *   The array to sort, passed by reference.
   */
  public static function sortByKeyRecursive(array &$data): void {
    // If the array is a list, it is by definition already sorted.
    if (!array_is_list($data)) {
      ksort($data);
    }
    foreach ($data as &$value) {
      if (is_array($value)) {
        self::sortByKeyRecursive($value);
      }
    }
  }

}
+3 −23
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

namespace Drupal\Core\Recipe;

use Drupal\Component\Utility\SortArray;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\NullStorage;
use Drupal\Core\Config\StorageInterface;
@@ -62,8 +63,8 @@ public function __construct(public readonly array $config, string $recipe_direct
        // Ensure we don't get a false mismatch due to different key order.
        // @todo When https://www.drupal.org/project/drupal/issues/3230826 is
        //   fixed in core, use that API instead to sort the config data.
        self::recursiveSortByKey($active_data);
        self::recursiveSortByKey($recipe_data);
        SortArray::sortByKeyRecursive($active_data);
        SortArray::sortByKeyRecursive($recipe_data);
        if ($active_data !== $recipe_data) {
          throw new RecipePreExistingConfigException($config_name, sprintf("The configuration '%s' exists already and does not match the recipe's configuration", $config_name));
        }
@@ -71,27 +72,6 @@ public function __construct(public readonly array $config, string $recipe_direct
    }
  }

  /**
   * Sorts an array recursively, by key, alphabetically.
   *
   * @param mixed[] $data
   *   The array to sort, passed by reference.
   *
   * @todo Remove when https://www.drupal.org/project/drupal/issues/3230826 is
   *   fixed in core.
   */
  private static function recursiveSortByKey(array &$data): void {
    // If the array is a list, it is by definition already sorted.
    if (!array_is_list($data)) {
      ksort($data);
    }
    foreach ($data as &$value) {
      if (is_array($value)) {
        self::recursiveSortByKey($value);
      }
    }
  }

  /**
   * Gets a config storage object for reading config from the recipe.
   *
+2 −9
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

namespace Drupal\Tests\rest\Functional;

use Drupal\Component\Utility\SortArray;
use Drupal\Core\Url;
use Drupal\rest\RestResourceConfigInterface;
use Drupal\Tests\ApiRequestTrait;
@@ -441,15 +442,7 @@ protected function assertResourceErrorResponse($expected_status_code, $expected_
   *   An array to sort.
   */
  protected static function recursiveKSort(array &$array) {
    // First, sort the main array.
    ksort($array);

    // Then check for child arrays.
    foreach ($array as &$value) {
      if (is_array($value)) {
        static::recursiveKSort($value);
      }
    }
    SortArray::sortByKeyRecursive($array);
  }

}
+3 −20
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

use ColinODell\PsrTestLogger\TestLogger;
use Drupal\Component\Serialization\Yaml;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\DefaultContent\ContentExportCommand;
use Drupal\Core\DefaultContent\Exporter;
use Drupal\Core\DefaultContent\Finder;
@@ -150,8 +151,8 @@ public function testExportContent(): void {
        $exported_data['default']['uid'][0]['entity'] = $imported_data['default']['uid'][0]['entity'] = $new_owner;
      }

      self::recursiveSortByKey($exported_data);
      self::recursiveSortByKey($imported_data);
      SortArray::sortByKeyRecursive($exported_data);
      SortArray::sortByKeyRecursive($imported_data);
      $this->assertSame($imported_data, $exported_data);
    }
  }
@@ -494,22 +495,4 @@ public function testExportEntitiesFilteredByBundle(): void {
    $this->assertStringContainsString('These bundles do not exist on the taxonomy_term entity type: junk', $process->getOutput());
  }

  /**
   * Recursively sorts an array by key.
   *
   * @param array $data
   *   The array to sort.
   */
  private static function recursiveSortByKey(array &$data): void {
    // If the array is a list, it is by definition already sorted.
    if (!array_is_list($data)) {
      ksort($data);
    }
    foreach ($data as &$value) {
      if (is_array($value)) {
        self::recursiveSortByKey($value);
      }
    }
  }

}
+23 −0
Original line number Diff line number Diff line
@@ -335,4 +335,27 @@ protected function assertBothNegativePositiveOrZero(int $expected, int $result):
    }
  }

  /**
   * Tests sorting arrays recursively by key.
   */
  public function testRecursiveSortByKey(): void {
    // Indexed arrays are sorted already.
    $array = ['one', 'two', 'three'];
    SortArray::sortByKeyRecursive($array);
    $this->assertSame(['one', 'two', 'three'], $array);

    $array = [
      'one key' => ['one', 'two', 'three'],
      'another key' => [
        'b' => 'see',
        'a' => 'bee',
      ],
    ];
    SortArray::sortByKeyRecursive($array);
    $this->assertSame($array, [
      'another key' => ['a' => 'bee', 'b' => 'see'],
      'one key' => ['one', 'two', 'three'],
    ]);
  }

}