You are here

public function RandomTextAnonymizer::anonymize in General Data Protection Regulation 3.0.x

Same name and namespace in other branches
  1. 8.2 modules/anonymizer/src/Plugin/Anonymizer/RandomTextAnonymizer.php \Drupal\anonymizer\Plugin\Anonymizer\RandomTextAnonymizer::anonymize()
  2. 8 modules/anonymizer/src/Plugin/Anonymizer/RandomTextAnonymizer.php \Drupal\anonymizer\Plugin\Anonymizer\RandomTextAnonymizer::anonymize()

Return an anonymized output.

The input. @var \Drupal\Core\Field\FieldItemListInterface|null $field The field being anonymized.

Return value

int|string The anonymized output.

Overrides AnonymizerInterface::anonymize

File

modules/anonymizer/src/Plugin/Anonymizer/RandomTextAnonymizer.php, line 26

Class

RandomTextAnonymizer
Class RandomTextAnonymizer.

Namespace

Drupal\anonymizer\Plugin\Anonymizer

Code

public function anonymize($input, FieldItemListInterface $field = NULL) {
  if (empty($input)) {
    return $input;
  }
  $maxLength = NULL;
  if (NULL !== $field) {
    $maxLength = $field
      ->getDataDefinition()
      ->getSetting('max_length');
  }

  // Generate a prefixed random string.
  $value = 'anon_' . $this->faker
    ->generator()
    ->words(1, TRUE);

  // If the value is too long, trim it.
  if ($maxLength !== NULL && strlen($input) > $maxLength) {
    $value = substr(0, $maxLength);
  }
  return $value;
}