function Explode::transform

Same name and namespace in other branches
  1. 9 core/modules/migrate/src/Plugin/migrate/process/Explode.php \Drupal\migrate\Plugin\migrate\process\Explode::transform()
  2. 8.9.x core/modules/migrate/src/Plugin/migrate/process/Explode.php \Drupal\migrate\Plugin\migrate\process\Explode::transform()
  3. 11.x core/modules/migrate/src/Plugin/migrate/process/Explode.php \Drupal\migrate\Plugin\migrate\process\Explode::transform()

Overrides ProcessPluginBase::transform

File

core/modules/migrate/src/Plugin/migrate/process/Explode.php, line 97

Class

Explode
Splits the source string into an array of strings, using a delimiter.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (empty($this->configuration['delimiter'])) {
        throw new MigrateException('delimiter is empty');
    }
    $strict = array_key_exists('strict', $this->configuration) ? $this->configuration['strict'] : TRUE;
    if ($strict && !is_string($value)) {
        throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
    }
    elseif (!$strict) {
        // Check if the incoming value can cast to a string.
        $original = $value;
        if (!is_string($original) && $original != ($value = @strval($value))) {
            throw new MigrateException(sprintf('%s cannot be casted to a string', var_export($original, TRUE)));
        }
        // Empty strings should be exploded to empty arrays.
        if ($value === '') {
            return [];
        }
    }
    $limit = $this->configuration['limit'] ?? PHP_INT_MAX;
    return explode($this->configuration['delimiter'], $value, $limit);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.