You are here

public function Generator::lorem in Style Guide 8

Same name and namespace in other branches
  1. 2.x src/Generator.php \Drupal\styleguide\Generator::lorem()

Lorum ipsum text, used to generate words and phrases.

Parameters

int $size: The size of the list to return.

int $words: The number of words to return. Pass 0 for a whole paragraph.

string $case: The case of the text. Options are 'mixed', 'upper' and 'lower'.

bool $returns: Indicates whether line returns should not be stripped out of the result.

bool $punctuation: Indicates whether punctuation should not be stripped out of the result.

bool $array: Indicates that the return value should be an array instead of a string.

Return value

string|array A string or array of content.

Overrides GeneratorInterface::lorem

1 call to Generator::lorem()
Generator::paragraphs in src/Generator.php
Generate paragraph(s) of random text.

File

src/Generator.php, line 106

Class

Generator
Class Generator.

Namespace

Drupal\styleguide

Code

public function lorem($size = 5, $words = 0, $case = 'mixed', $returns = TRUE, $punctuation = TRUE, $array = FALSE) {
  $text = $this->random
    ->paragraphs($size, TRUE);
  if (!$punctuation) {
    $text = str_replace([
      ',',
      '.',
    ], '', $text);
  }
  switch ($case) {
    case 'mixed':
      break;
    case 'upper':
      $text = strtoupper($text);
      break;
    case 'lower':
      $text = strtolower($text);
      break;
  }
  $graphs = explode("\n\n", $text);
  $text = array_slice($graphs, 0, $size);
  $spacer = ' ';
  if ($returns) {
    $spacer = "\n\n";
  }
  if ($words > 0) {
    $elements = explode(' ', implode(' ', $text));
    $output = [];
    for ($i = 0; $i < $words; $i++) {
      $val = array_rand($elements);
      $output[] = $elements[$val];
    }
    return implode(' ', $output);
  }
  if (!$array) {
    return implode($spacer, $text);
  }
  return $text;
}