UrlDecode.php in Tamper 8
File
src/Plugin/Tamper/UrlDecode.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 UrlDecode extends TamperBase {
const SETTING_METHOD = 'method';
public function defaultConfiguration() {
$config = parent::defaultConfiguration();
$config[self::SETTING_METHOD] = 'rawurldecode';
return $config;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form[self::SETTING_METHOD] = [
'#type' => 'radios',
'#title' => $this
->t('Decode method'),
'#options' => $this
->getOptions(),
'#default_value' => $this
->getSetting(self::SETTING_METHOD),
'#description' => $this
->t('Run values through the <a href="http://us3.php.net/urldecode">urldecode()</a> function.'),
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this
->setConfiguration([
self::SETTING_METHOD => $form_state
->getValue(self::SETTING_METHOD),
]);
}
protected function getOptions() {
return [
'rawurldecode' => $this
->t('Raw'),
'urldecode' => $this
->t('Legacy: Decodes + symbols into spaces.'),
];
}
public function tamper($data, TamperableItemInterface $item = NULL) {
if (!is_string($data)) {
throw new TamperException('Input should be a string.');
}
$operation = $this
->getSetting(self::SETTING_METHOD);
return call_user_func($operation, $data);
}
}