You are here

class WebformTextHelper in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Utility/WebformTextHelper.php \Drupal\webform\Utility\WebformTextHelper

Provides helper to operate on strings.

Hierarchy

Expanded class hierarchy of WebformTextHelper

3 files declare their use of WebformTextHelper
TextBase.php in src/Plugin/WebformElement/TextBase.php
Webform.php in src/Entity/Webform.php
webform_shortcuts.module in modules/webform_shortcuts/webform_shortcuts.module
Provides keyboard shortcuts to create and save webform elements.

File

src/Utility/WebformTextHelper.php, line 10

Namespace

Drupal\webform\Utility
View source
class WebformTextHelper {

  /**
   * CamelCase to Underscore name converter.
   *
   * @var \Symfony\Component\Serializer\NameConverter\NameConverterInterface
   */
  protected static $converter;

  /**
   * Get camel case to snake case converter.
   *
   * @return \Symfony\Component\Serializer\NameConverter\NameConverterInterface
   *   Camel case to snake case converter.
   */
  protected static function getCamelCaseToSnakeCaseNameConverter() {
    if (!isset(static::$converter)) {
      static::$converter = new CamelCaseToSnakeCaseNameConverter();
    }
    return static::$converter;
  }

  /**
   * Converts camel case to snake case (i.e. underscores).
   *
   * @param string $string
   *   String to be converted.
   *
   * @return string
   *   String with camel case converted to snake case.
   */
  public static function camelToSnake($string) {
    return static::getCamelCaseToSnakeCaseNameConverter()
      ->normalize($string);
  }

  /**
   * Converts snake case to camel case.
   *
   * @param string $string
   *   String to be converted.
   *
   * @return string
   *   String with snake case converted to camel case.
   */
  public static function snakeToCamel($string) {
    return static::getCamelCaseToSnakeCaseNameConverter()
      ->denormalize($string);
  }

  /**
   * Counts the number of words inside a string.
   *
   * This counts the number of words by counting the space between the words.
   *
   * str_word_count() is locale dependent and returns varying word counts
   * based on the current language.
   *
   * This approach matches how the jQuery Text Counter Plugin counts words.
   *
   * @param string $string
   *   The string.
   *
   * @return int
   *   The number of words inside the string.
   *
   * @see str_word_count()
   * @see https://github.com/ractoon/jQuery-Text-Counter
   * @see $.textcounter.wordCount
   */
  public static function wordCount($string) {
    return count(explode(' ', preg_replace('#\\s+#', ' ', trim($string))));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WebformTextHelper::$converter protected static property CamelCase to Underscore name converter.
WebformTextHelper::camelToSnake public static function Converts camel case to snake case (i.e. underscores).
WebformTextHelper::getCamelCaseToSnakeCaseNameConverter protected static function Get camel case to snake case converter.
WebformTextHelper::snakeToCamel public static function Converts snake case to camel case.
WebformTextHelper::wordCount public static function Counts the number of words inside a string.