You are here

TamperConvertCase.php in Tamper 7

Contains TamperConvertCase.

File

src/Plugins/TamperConvertCase.php
View source
<?php

/**
 * @file
 * Contains TamperConvertCase.
 */

/**
 * Converts the case of a string.
 */
class TamperConvertCase extends TamperPluginBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array(
      'mode' => 'upper',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, array &$form_state) {
    $form['mode'] = array(
      '#type' => 'radios',
      '#title' => t('How to convert case'),
      '#default_value' => $this->configuration['mode'],
      '#options' => array(
        'upper' => t('UPPERCASE THE STRING.'),
        'lower' => t('lowercase the string.'),
        'ucfirst' => t('Make the first char uppercase.'),
        'ucwords' => t('Make Each Word In The String Uppercase.'),
      ),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function execute($value) {
    switch ($this->configuration['mode']) {
      case 'upper':
        return drupal_strtoupper($value);
      case 'lower':
        return drupal_strtolower($value);
      case 'ucfirst':
        return drupal_strtoupper(drupal_substr($value, 0, 1)) . drupal_substr($value, 1);
      case 'ucwords':
        global $multibyte;
        if ($multibyte == UNICODE_MULTIBYTE) {
          return mb_convert_case($value, MB_CASE_TITLE);
        }
        else {
          return ucwords($value);
        }
    }
  }

}

Classes

Namesort descending Description
TamperConvertCase Converts the case of a string.