Commit b759a483 authored by Kevin Yumang's avatar Kevin Yumang

SERV-2286 - added loremipsum element

parent f2c94dda
<?php <?php
/* /*
|------------------------------------------------------------------------------------------------ |------------------------------------------------------------------------------------------------
| Information | Information
...@@ -21,8 +20,9 @@ ...@@ -21,8 +20,9 @@
| Dependencies | Dependencies
|------------------------------------------------------------------------------------------------ |------------------------------------------------------------------------------------------------
*/ */
use View;
use Ceetrox\Views\Limitless\Element\LoremIpsum\JoshtronicLoremIpsum;
use View;
/* /*
|------------------------------------------------------------------------------------------------ |------------------------------------------------------------------------------------------------
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
public $methodAllocation = [ public $methodAllocation = [
'Limitless::ElementCardStart' => 'cardStart', 'Limitless::ElementCardStart' => 'cardStart',
'Limitless::ElementCardStop' => 'cardStop', 'Limitless::ElementCardStop' => 'cardStop',
'Limitless::LoremIpsum' => 'loremIpsum',
'Limitless::Divider' => 'divider', 'Limitless::Divider' => 'divider',
'Limitless::Example' => 'example', 'Limitless::Example' => 'example',
]; ];
...@@ -55,7 +56,6 @@ ...@@ -55,7 +56,6 @@
|-------------------------------------------------------------------------------------------- |--------------------------------------------------------------------------------------------
| Method "cardStart" | Method "cardStart"
|-------------------------------------------------------------------------------------------- |--------------------------------------------------------------------------------------------
| Method "CardStart"
*/ */
public function cardStart($parameters) public function cardStart($parameters)
{ {
...@@ -80,6 +80,20 @@ ...@@ -80,6 +80,20 @@
} }
/*
|--------------------------------------------------------------------------------------------
| Method "loremIpsum"
|--------------------------------------------------------------------------------------------
*/
public function loremIpsum($parameters)
{
return View('Limitless::Element.LoremIpsum.LoremIpsum')
->withLength( isset($parameters['length'])
? (ctype_digit((string)$parameters['length']) ? $parameters['length'] : 0)
: 0 );
}
/* /*
|-------------------------------------------------------------------------------------------- |--------------------------------------------------------------------------------------------
| Method "divider" | Method "divider"
......
<?php
/**
* Lorem Ipsum Generator
*
* PHP version 5.3+
*
* Licensed under The MIT License.
* Redistribution of these files must retain the above copyright notice.
*
* @author Josh Sherman <hello@joshtronic.com>
* @copyright Copyright 2014-2022 Josh Sherman
* @license http://www.opensource.org/licenses/mit-license.html
* @link https://github.com/joshtronic/php-loremipsum
*/
namespace Ceetrox\Views\Limitless\Element\LoremIpsum\Attachments;
class Core
{
/**
* First
*
* Whether or not we should be starting the string with "Lorem ipsum..."
*
* @access private
* @var mixed
*/
private $first = true;
/**
* Words
*
* A lorem ipsum vocabulary of sorts. Not a complete list as I'm unsure if
* a complete list exists and if so, where to get it.
*
* @access private
* @var array<string>
*/
private $words = array(
// Lorem ipsum...
'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit',
// and the rest of the vocabulary
'a', 'ac', 'accumsan', 'ad', 'aenean', 'aliquam', 'aliquet', 'ante',
'aptent', 'arcu', 'at', 'auctor', 'augue', 'bibendum', 'blandit',
'class', 'commodo', 'condimentum', 'congue', 'consequat', 'conubia',
'convallis', 'cras', 'cubilia', 'curabitur', 'curae', 'cursus',
'dapibus', 'diam', 'dictum', 'dictumst', 'dignissim', 'dis', 'donec',
'dui', 'duis', 'efficitur', 'egestas', 'eget', 'eleifend', 'elementum',
'enim', 'erat', 'eros', 'est', 'et', 'etiam', 'eu', 'euismod', 'ex',
'facilisi', 'facilisis', 'fames', 'faucibus', 'felis', 'fermentum',
'feugiat', 'finibus', 'fringilla', 'fusce', 'gravida', 'habitant',
'habitasse', 'hac', 'hendrerit', 'himenaeos', 'iaculis', 'id',
'imperdiet', 'in', 'inceptos', 'integer', 'interdum', 'justo',
'lacinia', 'lacus', 'laoreet', 'lectus', 'leo', 'libero', 'ligula',
'litora', 'lobortis', 'luctus', 'maecenas', 'magna', 'magnis',
'malesuada', 'massa', 'mattis', 'mauris', 'maximus', 'metus', 'mi',
'molestie', 'mollis', 'montes', 'morbi', 'mus', 'nam', 'nascetur',
'natoque', 'nec', 'neque', 'netus', 'nibh', 'nisi', 'nisl', 'non',
'nostra', 'nulla', 'nullam', 'nunc', 'odio', 'orci', 'ornare',
'parturient', 'pellentesque', 'penatibus', 'per', 'pharetra',
'phasellus', 'placerat', 'platea', 'porta', 'porttitor', 'posuere',
'potenti', 'praesent', 'pretium', 'primis', 'proin', 'pulvinar',
'purus', 'quam', 'quis', 'quisque', 'rhoncus', 'ridiculus', 'risus',
'rutrum', 'sagittis', 'sapien', 'scelerisque', 'sed', 'sem', 'semper',
'senectus', 'sociosqu', 'sodales', 'sollicitudin', 'suscipit',
'suspendisse', 'taciti', 'tellus', 'tempor', 'tempus', 'tincidunt',
'torquent', 'tortor', 'tristique', 'turpis', 'ullamcorper', 'ultrices',
'ultricies', 'urna', 'ut', 'varius', 'vehicula', 'vel', 'velit',
'venenatis', 'vestibulum', 'vitae', 'vivamus', 'viverra', 'volutpat',
'vulputate',
);
/**
* Word
*
* Generates a single word of lorem ipsum.
*
* @access public
* @param mixed $tags string or array of HTML tags to wrap output with
* @return string generated lorem ipsum word
*/
public function word($tags = false)
{
return strval($this->words(1, $tags));
}
/**
* Words Array
*
* Generates an array of lorem ipsum words.
*
* @access public
* @param integer $count how many words to generate
* @param mixed $tags string or array of HTML tags to wrap output with
* @return mixed generated lorem ipsum words
*/
public function wordsArray($count = 1, $tags = false)
{
return $this->words($count, $tags, true);
}
/**
* Words
*
* Generates words of lorem ipsum.
*
* @access public
* @param integer $count how many words to generate
* @param mixed $tags string or array of HTML tags to wrap output with
* @param boolean $array whether an array or a string should be returned
* @return mixed string or array of generated lorem ipsum words
*/
public function words($count = 1, $tags = false, $array = false)
{
$count = (int) $count;
$words = array();
$word_count = 0;
// Shuffles and appends the word list to compensate for count
// arguments that exceed the size of our vocabulary list
while ($word_count < $count) {
$shuffle = true;
while ($shuffle) {
$this->shuffle();
// Checks that the last word of the list and the first word of
// the list that's about to be appended are not the same
if (!$word_count || $words[$word_count - 1] != $this->words[0]) {
$words = array_merge($words, $this->words);
$word_count = count($words);
$shuffle = false;
}
}
}
$words = array_slice($words, 0, $count);
return $this->output($words, $tags, $array);
}
/**
* Sentence
*
* Generates a full sentence of lorem ipsum.
*
* @access public
* @param mixed $tags string or array of HTML tags to wrap output with
* @return string generated lorem ipsum sentence
*/
public function sentence($tags = false)
{
return strval($this->sentences(1, $tags));
}
/**
* Sentences Array
*
* Generates an array of lorem ipsum sentences.
*
* @access public
* @param integer $count how many sentences to generate
* @param mixed $tags string or array of HTML tags to wrap output with
* @return mixed generated lorem ipsum sentences
*/
public function sentencesArray($count = 1, $tags = false)
{
return $this->sentences($count, $tags, true);
}
/**
* Sentences
*
* Generates sentences of lorem ipsum.
*
* @access public
* @param integer $count how many sentences to generate
* @param mixed $tags string or array of HTML tags to wrap output with
* @param boolean $array whether an array or a string should be returned
* @return mixed string or array of generated lorem ipsum sentences
*/
public function sentences($count = 1, $tags = false, $array = false)
{
$sentences = array();
for ($i = 0; $i < $count; $i++) {
$sentences[] = $this->wordsArray($this->gauss(24.46, 5.08));
}
$this->punctuate($sentences);
return $this->output($sentences, $tags, $array);
}
/**
* Paragraph
*
* Generates a full paragraph of lorem ipsum.
*
* @access public
* @param mixed $tags string or array of HTML tags to wrap output with
* @return string generated lorem ipsum paragraph
*/
public function paragraph($tags = false)
{
return strval($this->paragraphs(1, $tags));
}
/**
* Paragraph Array
*
* Generates an array of lorem ipsum paragraphs.
*
* @access public
* @param integer $count how many paragraphs to generate
* @param mixed $tags string or array of HTML tags to wrap output with
* @return array<string> generated lorem ipsum paragraphs
*/
public function paragraphsArray($count = 1, $tags = false)
{
// The $array parameter set to true means an array is returned.
// Return type is mixed, we should probably cast to array.
// @phpstan-ignore-next-line
return $this->paragraphs($count, $tags, true);
}
/**
* Paragraphs
*
* Generates paragraphs of lorem ipsum.
*
* @access public
* @param integer $count how many paragraphs to generate
* @param mixed $tags string or array of HTML tags to wrap output with
* @param boolean $array whether an array or a string should be returned
* @return mixed string or array of generated lorem ipsum paragraphs
*/
public function paragraphs($count = 1, $tags = false, $array = false)
{
$paragraphs = array();
for ($i = 0; $i < $count; $i++) {
$paragraphs[] = strval($this->sentences($this->gauss(5.8, 1.93)));
}
if ($array) {
return $this->output($paragraphs, $tags, $array, "\n\n");
}
return strval($this->output($paragraphs, $tags, false, "\n\n"));
}
/**
* Gaussian Distribution
*
* This is some smart kid stuff. I went ahead and combined the N(0,1) logic
* with the N(m,s) logic into this single function. Used to calculate the
* number of words in a sentence, the number of sentences in a paragraph
* and the distribution of commas in a sentence.
*
* @access private
* @param double $mean average value
* @param double $std_dev stadnard deviation
* @return int calculated distribution
*/
private function gauss($mean, $std_dev)
{
$x = mt_rand() / mt_getrandmax();
$y = mt_rand() / mt_getrandmax();
$z = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
return intval($z * $std_dev + $mean);
}
/**
* Shuffle
*
* Shuffles the words, forcing "Lorem ipsum..." at the beginning if it is
* the first time we are generating the text.
*
* @access private
* @return void
*/
private function shuffle()
{
if ($this->first) {
$this->first = array_slice($this->words, 0, 8);
$this->words = array_slice($this->words, 8);
shuffle($this->words);
$this->words = $this->first + $this->words;
$this->first = false;
} else {
shuffle($this->words);
}
}
/**
* Punctuate
*
* Applies punctuation to a sentence. This includes a period at the end,
* the injection of commas as well as capitalizing the first letter of the
* first word of the sentence.
*
* @access private
* @param array<string> $sentences the sentences we would like to punctuate
* @return void
*/
private function punctuate(&$sentences)
{
foreach ($sentences as $key => $sentence) {
$words = count($sentence);
// Only worry about commas on sentences longer than 4 words
if ($words > 4) {
$mean = log($words, 6);
$std_dev = $mean / 6;
$commas = $this->gauss($mean, $std_dev);
for ($i = 1; $i <= $commas; $i++) {
$word = round($i * $words / ($commas + 1));
if ($word < ($words - 1) && $word > 0) {
$sentence[$word] .= ',';
}
}
}
$sentences[$key] = ucfirst(implode(' ', $sentence) . '.');
}
}
/**
* Output
*
* Does the rest of the processing of the strings. This includes wrapping
* the strings in HTML tags, handling transformations with the ability of
* back referencing and determining if the passed array should be converted
* into a string or not.
*
* @access private
* @param array<string> $strings an array of generated strings
* @param mixed $tags string or array of HTML tags to wrap output with
* @param boolean $array whether an array or a string should be returned
* @param string $delimiter the string to use when calling implode()
* @return mixed string or array of generated lorem ipsum text
*/
private function output($strings, $tags, $array, $delimiter = ' ')
{
if ($tags) {
if (!is_array($tags)) {
$tags = array($tags);
} else {
// Flips the array so we can work from the inside out
$tags = array_reverse($tags);
}
foreach ($strings as $key => $string) {
foreach ($tags as $tag) {
if (is_string($tag)) {
// Detects / applies back reference
if ($tag[0] == '<') {
$string = str_replace('$1', $string, $tag);
} else {
$string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
}
}
$strings[$key] = $string;
}
}
}
if (!$array) {
$strings = implode($delimiter, $strings);
}
return $strings;
}
}
\ No newline at end of file
{{-- Layout Reference --}}
@extends('Limitless::Help.Layout.Master')
@section('Limitless::Content')
{{-- Description --}}
@Limitless::ElementCardStart(['title' => "Lorem Ipsum", 'icon' => 'icon-bubble-lines3'])
<p>Generates Lorem ipsum based on passed parameter <i>length</i>.</p>
@Limitless::ElementCardStop
{{-- Elements --}}
@Limitless::ElementCardStart(['title' => "Fragments", 'icon' => 'icon-grid5'])
<div class="bg-dark rounded py-2 px-3 mb-3">
<h6 class="mb-0 font-weight-semibold">LoremIpsum</h6>
<p class="mb-3 text-muted">Generates Lorem ipsum based on passed parameter <i>length</i>.</p>
<p class="font-italic font-size-lg">Parameters</p>
<div class="row mt-2">
@Limitless::TableStart([ 'extra' => ['class' => 'table table-xs table-striped'] ])
@Limitless::TableAutofillHeader([
['name' => 'Key'],
['name' => 'Type'],
['name' => 'Description']
])
@Limitless::TableAutofillBody([
[
['value' => 'length'],
['value' => '<i>Integer|Numeric String</i> [Default = <code>0</code>]', 'secure' => false],
['value' => 'The length of random words to generate.']
]
])
@Limitless::TableStop
</div>
</div>
@Limitless::ElementCardStop
{{-- Sample Output --}}
@Limitless::ElementCardStart(['title' => "Sample Output", 'icon' => 'icon-display4' ] )
@Limitless::LoremIpsum(['length' => 10])
@Limitless::Divider
@Limitless::LoremIpsum(['length' => '20'])
@Limitless::ElementCardStop
@php
$example = base64_decode("QExpbWl0bGVzczo6TG9yZW1JcHN1bShbJ2xlbmd0aCcgPT4gMTBdKQpATGltaXRsZXNzOjpEaXZpZGVyCkBMaW1pdGxlc3M6OkxvcmVtSXBzdW0oWydsZW5ndGgnID0+ICcyMCddKQ==");
@endphp
{{-- Sample Code --}}
@Limitless::ElementCardStart(['title' => "Sample Code", 'icon' => 'icon-circle-code' ] )
@Limitless::Codemirror([ 'language' => 'javascript', 'theme' => 'zenburn', 'value' => $example ])
@Limitless::ElementCardStop
@stop
@php
$output = (new Ceetrox\Views\Limitless\Element\LoremIpsum\Attachments\Core())->words($length);
@endphp
{{ $output }}
\ No newline at end of file
...@@ -51,15 +51,18 @@ ...@@ -51,15 +51,18 @@
case "example": case "example":
$path = 'Limitless::Element.Example.Documentation.Index'; $path = 'Limitless::Element.Example.Documentation.Index';
break; break;
case "divider":
$path = 'Limitless::Element.Divider.Documentation.Index';
break;
case "card": case "card":
$path = 'Limitless::Element.Card.Documentation.Index'; $path = 'Limitless::Element.Card.Documentation.Index';
break; break;
case "codemirror": case "codemirror":
$path = 'Limitless::Codemirror.Documentation.Index'; $path = 'Limitless::Codemirror.Documentation.Index';
break; break;
case "divider":
$path = 'Limitless::Element.Divider.Documentation.Index';
break;
case "loremipsum":
$path = 'Limitless::Element.LoremIpsum.Documentation.Index';
break;
case "navigation": case "navigation":
$path = 'Limitless::Navigation.Documentation.Index'; $path = 'Limitless::Navigation.Documentation.Index';
break; break;
......
...@@ -28,9 +28,10 @@ ...@@ -28,9 +28,10 @@
@Limitless::NavigationMenuSectionTitle(['title' => 'Elements']) @Limitless::NavigationMenuSectionTitle(['title' => 'Elements'])
@Limitless::NavigationMenuItem(['url' => '?page=example', 'target' => '_self', 'icon' => 'icon-exclamation', 'title' => 'Example']) @Limitless::NavigationMenuItem(['url' => '?page=example', 'target' => '_self', 'icon' => 'icon-exclamation', 'title' => 'Example'])
@Limitless::NavigationMenuItem(['url' => '?page=card', 'target' => '_self', 'icon' => 'icon-newspaper', 'title' => 'Card']) @Limitless::NavigationMenuItem(['url' => '?page=card', 'target' => '_self', 'icon' => 'icon-newspaper', 'title' => 'Card'])
@Limitless::NavigationMenuItem(['url' => '?page=table', 'target' => '_self', 'icon' => 'icon-table2', 'title' => 'Table'])
@Limitless::NavigationMenuItem(['url' => '?page=codemirror', 'target' => '_self', 'icon' => 'icon-circle-code', 'title' => 'Code Mirror']) @Limitless::NavigationMenuItem(['url' => '?page=codemirror', 'target' => '_self', 'icon' => 'icon-circle-code', 'title' => 'Code Mirror'])
@Limitless::NavigationMenuItem(['url' => '?page=navigation', 'target' => '_self', 'icon' => 'icon-menu8', 'title' => 'Navigation'])
@Limitless::NavigationMenuItem(['url' => '?page=divider', 'target' => '_self', 'icon' => 'icon-minus2', 'title' => 'Divider']) @Limitless::NavigationMenuItem(['url' => '?page=divider', 'target' => '_self', 'icon' => 'icon-minus2', 'title' => 'Divider'])
@Limitless::NavigationMenuItem(['url' => '?page=loremipsum', 'target' => '_self', 'icon' => 'icon-bubble-lines3', 'title' => 'Lorem Ipsum'])
@Limitless::NavigationMenuItem(['url' => '?page=navigation', 'target' => '_self', 'icon' => 'icon-menu8', 'title' => 'Navigation'])
@Limitless::NavigationMenuItem(['url' => '?page=table', 'target' => '_self', 'icon' => 'icon-table2', 'title' => 'Table'])
@Limitless::NavigationStop @Limitless::NavigationStop
@stop @stop
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment