ConvertCase.php in Tamper 8
File
src/Plugin/Tamper/ConvertCase.php
View source
<?php
namespace Drupal\tamper\Plugin\Tamper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\tamper\Exception\TamperException;
use Drupal\tamper\TamperableItemInterface;
use Drupal\tamper\TamperBase;
class ConvertCase extends TamperBase {
const SETTING_OPERATION = 'operation';
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config[self::SETTING_OPERATION] = 'ucfirst';
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[self::SETTING_OPERATION] = [
'#type' => 'select',
'#title' => $this
->t('How to convert case'),
'#options' => $this
->getOptions(),
'#default_value' => $this
->getSetting(self::SETTING_OPERATION),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this
->setConfiguration([
self::SETTING_OPERATION => $form_state
->getValue(self::SETTING_OPERATION),
]);
}
protected function getOptions() {
return [
'strtoupper' => $this
->t('Convert to uppercase'),
'strtolower' => $this
->t('Convert to lowercase'),
'ucfirst' => $this
->t('Capitalize the first character'),
'lcfirst' => $this
->t('Convert the first character to lowercase'),
'ucwords' => $this
->t('Capitalize the first character of each word'),
];
}
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);
}
}
}