You are here

public static function RealisticDummyContentEnvironment::sortCandidateFiles in Realistic Dummy Content 3.x

Same name and namespace in other branches
  1. 8.2 api/src/includes/RealisticDummyContentEnvironment.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentEnvironment::sortCandidateFiles()
  2. 7.2 api/src/includes/RealisticDummyContentEnvironment.php \Drupal\realistic_dummy_content_api\includes\RealisticDummyContentEnvironment::sortCandidateFiles()

Given a list of candidate files, sort them by names and parts.

Parameters

array $candidate_files: An array keyed by filename which contains drupal file objects, like this:

'one.txt' => [file object] 'two.txt.attribute.txt' => [file object] 'two.txt.attribute1.txt' => [file object] 'three.txt' => [file object].

null|array $extensions: (Default is NULL). If set, only return file groups whose base file is in one of the extenstions. For example, given an extension jpg,png, and a file structure with

a.jpg a.jpg.alt.txt b.txt

This function will return:

a.jpg => file => [a.jpg] attributes => alt => [a.jpg.alt.txt].

Return value

array A sorted array which looks like:

one.txt => array('file' => [file object]), two.txt = array( attributes => array( 'attribute' => [file object], 'attribute1' => [file object], ) ), three.txt => array('file' => [file object]),

Throws

RealisticDummyContentException

2 calls to RealisticDummyContentEnvironment::sortCandidateFiles()
RealisticDummyContentEnvironment::getAllFileGroups in api/src/includes/RealisticDummyContentEnvironment.php
Returns all files with a given extension for a given filepath.
RealisticDummyContentEnvironmentTest::testFiles in api/tests/src/Unit/includes/RealisticDummyContentEnvironmentTest.php
Test that file names are properly parsed and combined.

File

api/src/includes/RealisticDummyContentEnvironment.php, line 208

Class

RealisticDummyContentEnvironment
The abstract base environment.

Namespace

Drupal\realistic_dummy_content_api\includes

Code

public static function sortCandidateFiles(array $candidate_files, $extensions = NULL) {
  foreach ($candidate_files as $candidate_filename => $candidate_file) {
    if (!is_string($candidate_filename)) {

      // Explicitly load the Exception class, because during unit tests the
      // registry is not present.
      module_load_include('inc', 'realistic_dummy_content_api', 'includes/RealisticDummyContentException');
      throw new RealisticDummyContentException('array keys should be strings');
    }
    if (!is_object($candidate_file)) {

      // Explicitly load the Exception class, because during unit tests the
      // registry is not present.
      module_load_include('inc', 'realistic_dummy_content_api', 'includes/RealisticDummyContentException');
      throw new RealisticDummyContentException('array values should be file objects');
    }
    if (strpos($candidate_filename, '/') !== FALSE) {

      // Explicitly load the Exception class, because during unit tests the
      // registry is not present.
      module_load_include('inc', 'realistic_dummy_content_api', 'includes/RealisticDummyContentException');
      throw new RealisticDummyContentException('Please do not pass file paths with slashes (/) to ' . __FUNCTION__);
    }
  }
  $return = self::implementSortCandidateFiles($candidate_files, $extensions);
  return $return;
}