You are here

public static function YamlFormArrayHelper::toString in YAML Form 8

Implode an array with commas separating the elements and with an "and" before the last element.

Parameters

array $array: The array to be convert to a string.

string $conjunction: (optional) The word, which should be 'and' or 'or' used to join the values of the array. Defaults to 'and'.

Return value

string The array converted to a string.

10 calls to YamlFormArrayHelper::toString()
OptionsBase::formatText in src/Plugin/YamlFormElement/OptionsBase.php
Format an element's value as plain text.
YamlFormAdminSettingsForm::buildForm in src/Form/YamlFormAdminSettingsForm.php
Form constructor.
YamlFormArrayHelperTest::testToString in tests/src/Unit/YamlFormArrayHelperTest.php
Tests converting arrays to readable string with YamlFormArrayHelper::toString().
YamlFormElementBase::buildConfigurationForm in src/YamlFormElementBase.php
Form constructor.
YamlFormElementBase::validateConfigurationForm in src/YamlFormElementBase.php
Form validation handler.

... See full list

File

src/Utility/YamlFormArrayHelper.php, line 22

Class

YamlFormArrayHelper
Provides helper to operate on arrays.

Namespace

Drupal\yamlform\Utility

Code

public static function toString(array $array, $conjunction = NULL) {
  if ($conjunction === NULL) {
    $conjunction = t('and');
  }
  switch (count($array)) {
    case 0:
      return '';
    case 1:
      return reset($array);
    case 2:
      return implode(' ' . $conjunction . ' ', $array);
    default:
      $last = array_pop($array);
      return implode(', ', $array) . ', ' . $conjunction . ' ' . $last;
  }
}