You are here

function _math_captcha_repr in CAPTCHA Pack 8

Same name and namespace in other branches
  1. 5 math_captcha/math_captcha.module \_math_captcha_repr()
  2. 6 math_captcha/math_captcha.challenge.inc \_math_captcha_repr()
  3. 7 math_captcha/math_captcha.challenge.inc \_math_captcha_repr()

Helper function for transforming a number to a textual representation.

1 call to _math_captcha_repr()
_math_captcha_build_captcha in math_captcha/math_captcha.challenge.inc
Helper function to build a math CAPTCHA form item.

File

math_captcha/math_captcha.challenge.inc, line 102
Provides challenge functions for MATH CAPTCHA administration.

Code

function _math_captcha_repr($n, $add_paratheses_when_negative = FALSE) {
  $config = Drupal::config('math_captcha.settings');

  // Start with no textual representation.
  $t = "{$n}";

  // If enabled and available: do textual representation.
  if ($config
    ->get('math_captcha_textual_numbers')) {
    $repr_map = [
      0 => t('zero'),
      1 => t('one'),
      2 => t('two'),
      3 => t('three'),
      4 => t('four'),
      5 => t('five'),
      6 => t('six'),
      7 => t('seven'),
      8 => t('eight'),
      9 => t('nine'),
      10 => t('ten'),
      11 => t('eleven'),
      12 => t('twelve'),
      13 => t('thirteen'),
      14 => t('fourteen'),
      15 => t('fifteen'),
    ];
    if (array_key_exists(abs($n), $repr_map)) {
      $t = $repr_map[abs($n)];
      if ($n < 0) {
        $t = t('minus @number', [
          '@number' => $t,
        ]);
      }
    }
  }
  if ($add_paratheses_when_negative && $n < 0) {
    $t = "({$t})";
  }
  return $t;
}