You are here

public function ConvertCase::tamper in Tamper 8

Tamper data.

Performs the operations on the data to transform it.

Parameters

mixed $data: The data to tamper.

\Drupal\tamper\TamperableItemInterface $item: Item that can be tampered as part of a plugin's execution.

Return value

mixed The tampered data.

Throws

\Drupal\tamper\Exception\TamperException When the plugin can not tamper the given data.

\Drupal\tamper\Exception\SkipTamperDataException When the calling tamper process should be skipped for the given data.

\Drupal\tamper\Exception\SkipTamperItemException When the calling tamper process should be skipped for the given item.

Overrides TamperInterface::tamper

File

src/Plugin/Tamper/ConvertCase.php, line 74

Class

ConvertCase
Plugin implementation for converting case.

Namespace

Drupal\tamper\Plugin\Tamper

Code

public function tamper($data, TamperableItemInterface $item = NULL) {
  if (!is_string($data)) {
    throw new TamperException('Input should be a string.');
  }
  $operation = $this
    ->getSetting(self::SETTING_OPERATION);
  switch ($operation) {
    case 'strtoupper':
      return mb_strtoupper($data);
    case 'strtolower':
      return mb_strtolower($data);
    default:
      if (!is_callable([
        '\\Drupal\\Component\\Utility\\Unicode',
        $operation,
      ])) {
        throw new TamperException('Invalid operation ' . $operation);
      }
      return call_user_func([
        '\\Drupal\\Component\\Utility\\Unicode',
        $operation,
      ], $data);
  }
}