class Generator in Style Guide 8
Same name and namespace in other branches
- 2.x src/Generator.php \Drupal\styleguide\Generator
Class Generator.
@package Drupal\styleguide
Hierarchy
- class \Drupal\styleguide\Generator implements GeneratorInterface
Expanded class hierarchy of Generator
1 string reference to 'Generator'
1 service uses Generator
File
- src/
Generator.php, line 17
Namespace
Drupal\styleguideView source
class Generator implements GeneratorInterface {
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $currentRequest;
/**
* The utility class for creating random data.
*
* @var \Drupal\Component\Utility\Random
*/
protected $random;
/**
* The page manager service.
*
* @var \Drupal\Core\Pager\PagerManagerInterface
*/
protected $pagerManager;
/**
* The configuration entry point.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $config;
/**
* Constructor.
*/
public function __construct(RequestStack $request_stack, PagerManagerInterface $page_manager, ConfigFactoryInterface $config) {
$this->currentRequest = $request_stack
->getCurrentRequest();
$this->random = new Random();
$this->pagerManager = $page_manager;
$this->config = $config;
}
/**
* {@inheritdoc}
*/
public function wordList($size = 5, $words = 3) {
$items = [];
for ($i = 0; $i < $size; $i++) {
$items[] = $this
->words($words, 'ucfirst');
}
return $items;
}
/**
* {@inheritdoc}
*/
public function words($size = 1, $case = 'strtolower') {
$words = [];
for ($i = 0; $i < $size; $i++) {
$words[] = $this->random
->word(rand(4, 12));
}
$words = implode(' ', $words);
$functions = [
'ucfirst',
'ucwords',
'strtoupper',
'strtolower',
];
if (!is_null($case) && function_exists($case) && in_array($case, $functions)) {
$words = $case($words);
}
return $words;
}
/**
* {@inheritdoc}
*/
public function tableHeader($size = 5) {
$header = $this
->wordList($size);
return $header;
}
/**
* {@inheritdoc}
*/
public function tableRows($size = 5) {
$rows = [];
for ($i = 0; $i < $size; $i++) {
$rows[] = $this
->wordList($size);
}
return $rows;
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function paragraphs($size = 5, $render = FALSE) {
$text = $this
->lorem($size, 0, 'mixed', TRUE, TRUE, TRUE);
$output = [];
foreach ($text as $item) {
$output[] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => trim($item),
];
}
return $render ? render($output) : $output;
}
/**
* {@inheritdoc}
*/
public function image($image = 'vertical', $type = 'jpg', $min_resolution = '240x320', $max_resolution = '240x320') {
$file_path = $this->config
->get('system.file')
->get('default_scheme') . '://styleguide-image-' . $image . '.' . $type;
if ($image == 'horizontal') {
$min_resolution = $max_resolution = '320x240';
}
$file_path = $this->random
->image($file_path, $min_resolution, $max_resolution);
return file_exists($file_path) ? $file_path : NULL;
}
/**
* {@inheritdoc}
*/
public function sentence($link = FALSE) {
$graph = $this->random
->sentences(mt_rand(60, 180));
$explode = explode('.', $graph);
$rand = array_rand($explode);
$sentence = trim($explode[$rand]);
if ($link) {
$explode = explode(' ', $sentence);
$link = [
'#type' => 'link',
'#title' => $explode[0],
'#url' => $link,
'#options' => [
'attributes' => [],
'html' => FALSE,
],
'#text' => $explode[0],
];
$explode[0] = render($link);
$sentence = implode(' ', $explode);
}
return Markup::create($sentence . '.');
}
/**
* {@inheritdoc}
*/
public function pager($size = 8, $total = 20) {
$this->pagerManager
->createPager($total, $size)
->getCurrentPage();
return [
'#type' => 'pager',
];
}
/**
* {@inheritdoc}
*/
public function links($url, $size = 4) {
$links = [];
for ($i = 0; $i < 5; $i++) {
$links[] = [
'title' => $this
->words(3),
'url' => Url::fromUserInput($url),
];
}
return $links;
}
/**
* {@inheritdoc}
*/
public function menuItem($url) {
$menu_item = [
'#type' => 'link',
'#title' => $this
->sentence(),
'#url' => Url::fromUserInput($url),
];
return $menu_item;
}
/**
* {@inheritdoc}
*/
public function ulLinks() {
$links = [];
for ($i = 0; $i <= 10; $i++) {
$word = $this
->words();
$links[$word] = [
'title' => $word,
'url' => Url::fromUserInput($this->currentRequest
->getRequestUri()),
'fragment' => 'ul_links',
];
}
return $links;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Generator:: |
protected | property | The configuration entry point. | |
Generator:: |
protected | property | The current request. | |
Generator:: |
protected | property | The page manager service. | |
Generator:: |
protected | property | The utility class for creating random data. | |
Generator:: |
public | function |
Generate a default image for display. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Generate a array of random links. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Lorum ipsum text, used to generate words and phrases. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Generate a random menu item. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Simulate Drupal pagination,. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Generate paragraph(s) of random text. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Generate a random sentence. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Return a random table header array. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Return a random table row array. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Generate a links array for theme_links. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Return a simple array of words. Overrides GeneratorInterface:: |
|
Generator:: |
public | function |
Return a random word or words. Overrides GeneratorInterface:: |
|
Generator:: |
public | function | Constructor. |