function DevelGenerateBase::csvToArray

Convert a csv string into an array of items.

Borrowed from Drush.

Parameters

string|array|null $args: A simple csv string; e.g. 'a,b,c' or a simple list of items; e.g. array('a','b','c') or some combination; e.g. array('a,b','c') or array('a,','b,','c,').

9 calls to DevelGenerateBase::csvToArray()
BlockContentDevelGenerate::settingsFormValidate in devel_generate/src/Plugin/DevelGenerate/BlockContentDevelGenerate.php
Form validation handler.
BlockContentDevelGenerate::validateDrushParams in devel_generate/src/Plugin/DevelGenerate/BlockContentDevelGenerate.php
Responsible for validating Drush params.
ContentDevelGenerate::settingsFormValidate in devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
Form validation handler.
ContentDevelGenerate::validateDrushParams in devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
Responsible for validating Drush params.
MediaDevelGenerate::settingsFormValidate in devel_generate/src/Plugin/DevelGenerate/MediaDevelGenerate.php
Form validation handler.

... See full list

File

devel_generate/src/DevelGenerateBase.php, line 328

Class

DevelGenerateBase
Provides a base DevelGenerate plugin implementation.

Namespace

Drupal\devel_generate

Code

public static function csvToArray($args) : array {
    if ($args === NULL) {
        return [];
    }
    // 1: implode(',',$args) converts from array('a,','b,','c,') to 'a,,b,,c,'
    // 2: explode(',', ...) converts to array('a','','b','','c','')
    // 3: array_filter(...) removes the empty items
    // 4: array_map(...) trims extra whitespace from each item
    // (handles csv strings with extra whitespace, e.g. 'a, b, c')
    //
    $args = is_array($args) ? implode(',', array_map('strval', $args)) : (string) $args;
    return array_map('trim', array_filter(explode(',', $args)));
}