You are here

public static function WebformOptionsHelper::range in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Utility/WebformOptionsHelper.php \Drupal\webform\Utility\WebformOptionsHelper::range()

Build an associative array containing a range of options.

Parameters

int|string $min: First value of the sequence.

int $max: The sequence is ended upon reaching the end value.

int $step: Increments between the range. Default value is 1.

int $pad_length: Number of character to be prepended to the range.

string $pad_str: The character to default the string.

Return value

array An associative array containing a range of options.

2 calls to WebformOptionsHelper::range()
WebformOptionsHelperTest::testRange in tests/src/Unit/Utility/WebformOptionsHelperTest.php
Tests WebformOptionsHelper::range().
webform_webform_options_range_alter in includes/webform.options.inc
Implements hook_webform_options_WEBFORM_OPTIONS_ID_alter() for range options.

File

src/Utility/WebformOptionsHelper.php, line 281

Class

WebformOptionsHelper
Helper class webform options based methods.

Namespace

Drupal\webform\Utility

Code

public static function range($min = 1, $max = 100, $step = 1, $pad_length = NULL, $pad_str = '0') {

  // Create range.
  $range = range($min, $max, $step);

  // Pad left on range.
  if ($pad_length) {
    $range = array_map(function ($item) use ($pad_length, $pad_str) {
      return str_pad($item, $pad_length, $pad_str, STR_PAD_LEFT);
    }, $range);
  }

  // Return associative array of range options.
  return array_combine($range, $range);
}