You are here

public static function Element::oxford in Lightning Core 8.2

Same name and namespace in other branches
  1. 8.5 src/Element.php \Drupal\lightning_core\Element::oxford()
  2. 8 src/Element.php \Drupal\lightning_core\Element::oxford()
  3. 8.3 src/Element.php \Drupal\lightning_core\Element::oxford()
  4. 8.4 src/Element.php \Drupal\lightning_core\Element::oxford()

Formats a set of strings with an Oxford comma.

Parameters

string[] $items: The set of strings to format.

string $conjunction: (optional) The translated conjunction to insert before the final item. Defaults to 'and'.

Return value

string The single Oxford-ized string.

2 calls to Element::oxford()
ElementTest::testOxford in tests/src/Unit/ElementTest.php
@covers ::oxford
ElementTest::testOxford in tests/src/Kernel/ElementTest.php
@covers ::oxford

File

src/Element.php, line 110

Class

Element
Helpful functions for dealing with renderable arrays and elements.

Namespace

Drupal\lightning_core

Code

public static function oxford(array $items, $conjunction = 'and') {
  $count = count($items);
  if ($count < 2) {
    return (string) reset($items);
  }
  elseif ($count === 2) {
    return reset($items) . ' ' . $conjunction . ' ' . end($items);
  }
  else {
    $items[] = $conjunction . ' ' . array_pop($items);
    return implode(', ', $items);
  }
}