Problem/Motivation

The FormatInteger process plugin should prepare integer string to be used by integer field.
It should remove the several known thousands delimiters (. , space) and optionnaly prefix (e.g. $) or suffix (e.g. €).
It should optionnaly strip decimals by providing a decimal mark.

Proposed resolution

Default configuration will remove known thousands delimiters.
Decimal mark should optionnaly be given via configuration.
Prefix or suffix should be removed by optional configuration or by using a more straightforward method like filter_var($value, FILTER_SANITIZE_NUMBER_INT). Cases needs to be identified (e.g. signed integer, several values in the same source, ...).

Remaining tasks

  • Implement
  • Documentation
  • Unit tests
CommentFileSizeAuthor
#4 FormatInteger.patch2.13 KB_kash_

Comments

colorfield created an issue. See original summary.

colorfield’s picture

Issue summary: View changes
colorfield’s picture

Issue summary: View changes
_kash_’s picture

StatusFileSize
new2.13 KB
_kash_’s picture

<?php

namespace Drupal\migrate_process_extra\Plugin\migrate\process;

use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;


/**
 * Formats an Integer from a string (...).
 *
 * @todo documentation
 *
 * @MigrateProcessPlugin(
 *   id = "format_integer"
 * )
 */
class FormatInteger extends ProcessPluginBase implements ContainerFactoryPluginInterface {

  /**
   * Constructs a new FormatPhone object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
   *   The migration entity.
   *
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
    return new static($configuration, $plugin_id, $plugin_definition, $migration);
  }

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (is_string($value)) {
      return (int)preg_replace("/([^0-9\\.])/i", "", $value);
    }
    else {
      throw new MigrateException(sprintf('%s is not a string', var_export($value, TRUE)));
    }
  }

}