Source of file example.php
Size: 34,981 Bytes - Last Modified: 2021-12-23T10:47:07+00:00
/var/www/docs.ssmods.com/process/src/docs/en/example.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911 | <?php function create_test() { $useJSON = true; if (isset($_GET['nojson']) && '1' === $_GET['nojson']) { $useJSON = false; } $templateRow = ''; if ($useJSON) { $templateRow = ' <tr class="tfstr" id="{{=it.RowID}}"> <td><a href="#" class="more">+</a></td> <td><a href="#" class="adf" title="Add to Favourites">♥</a></td> <th scope="row"> <span data-filter="SKU">{{=it.SKU}}</span><br> <p style="display: none;" class="hidden"> Some more content goes here. </p> </th> <td><span data-filter="Type">{{=it.Type}}</span></td> <td><span data-filter="OriginalProducer">{{=it.OriginalProducer}}</span></td> <td><span data-filter="Colour" class="dl">{{=it.Colour}}</span></td> <td><span data-filter="Size">{{=it.Size}}</span></td> <td><span data-filter="Weight">{{=it.Weight}}</span></td> <td><span data-filter="Price">{{=it.Price}}</span></td> <td><span data-filter="Rating">{{=it.Rating}}</span></td> <td> {{?it.Tags}} <ul> {{~it.Tags : Tag}} <li><span data-filter="Tags" class="dl">{{=Tag}}</span></li> {{~}} </ul> {{?}} <div style="display: none;" class="hidden"> <p>Lorem ipsum dolor sit amet consectetur adipiscing elit cum nullam, risus posuere ligula eget ullamcorper orci ultricies neque lobortis suspendisse, sodales accumsan mus eleifend vulputate magna ornare at.</p> </div> </td> <td> your description: <input name="rating" data-filter="your description" type="text" data-tfsvalue="{{=it.InputValue}}"> your selected: <select name="selection" data-filter="your selection" data-tfsvalue="{{=it.SelectValue}}"> <option value="yes">yes</option> <option value="no">no</option> <option value="maybe">maybe</option> </select> </td> </tr> '; } $lipsum = new LoremIpsum(); $type = 'Test Product'; $producer = 'Sunny Side Up'; $html = ''; $limit = isset($_GET['i']) ? $_GET['i'] : 300; $jsonArray = []; $colours = RandomTestValues::get_sample_array_by_type('colours'); $veg = RandomTestValues::get_sample_array_by_type('veg'); $sizes = RandomTestValues::get_sample_array_by_type('sizes'); for ($i = 0; $i < $limit; ++$i) { $id = 'tfs' . $i; $jsonArray[$id] = [ 'SKU' => ($i + 1), 'Type' => $type, 'OriginalProducer' => $producer, 'Colour' => $colours[rand(0, count($colours) - 1)], 'Size' => $sizes[rand(0, count($sizes) - 1)], 'Weight' => rand(1, 100), 'Price' => (rand(0, 99999) / 100), 'Rating' => rand(1, 5), 'Tags' => [ $veg[rand(0, count($veg) - 1)], $veg[rand(0, count($veg) - 1)], $veg[rand(0, count($veg) - 1)], ], 'InputValue' => 'tba', 'SelectValue' => 'yes', ]; $rowData = $jsonArray[$id]; if ($useJSON) { $html = $templateRow; } else { $html .= ' <tr class="tfstr hide" id="' . $id . '"> <td><a href="#" class="more">+</a></td> <td><a href="#" class="adf" title="Add to Favourites">♥</a></td> <th scope="row"> <span data-filter="SKU">' . $rowData['SKU'] . '</span><br /> <p style="display: none;" class="hidden"> Some more content goes here. </p> </th> <td><span data-filter="Type">' . $type . '</span></td> <td><span data-filter="OriginalProducer">' . $producer . '</span></td> <td><span data-filter="Colour" class="dl">' . $rowData['Colour'] . '</span></td> <td><span data-filter="Size">' . $rowData['Size'] . '</span></td> <td><span data-filter="Weight">' . $rowData['Weight'] . 'kg.</span></td> <td><span data-filter="Price">$' . $rowData['Price'] . '</span></td> <td><span data-filter="Rating">' . $rowData['Rating'] . ' Stars</span></td> <td> <ul> <li><span data-filter="Tags" class="dl">' . $rowData['Tags'][0] . '</span></li> <li><span data-filter="Tags" class="dl">' . $rowData['Tags'][1] . '</span></li> <li><span data-filter="Tags" class="dl">' . $rowData['Tags'][2] . '</span></li> </ul> <div style="display: none;" class="hidden"> <p>' . $lipsum->sentence() . '</p> </div> </td> <td> your description: <input name="rating" data-filter="your description" type="text" value="" /> your selected: <select name="selection" data-filter="your selection"> <option value="yes">yes</option> <option value="no">no</option> <option value="maybe">maybe</option> </select> </tr>'; } } return [ 'HTML' => $html, 'Data' => ($useJSON ? $jsonArray : []), 'useJSON' => $useJSON, ]; } $data = create_test(); $html = $data['HTML']; $jsonArray = $data['Data']; $useJSON = $data['useJSON']; ?><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example Filter Table</title> <link rel="stylesheet" type="text/css" href="../../css/awesomplete.css?x=<?php echo rand(0, 9999999999); ?>"> <link rel="stylesheet" type="text/css" href="../../css/awesomplete.theme.css?x=<?php echo rand(0, 9999999999); ?>"> <link rel="stylesheet" type="text/css" href="../../css/TableFilterSort.css?x=<?php echo rand(0, 9999999999); ?>"> <link rel="stylesheet" type="text/css" href="../../css/TableFilterSort.theme.css?x=<?php echo rand(0, 9999999999); ?>"> <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" /> <style> form.update-page-form {width: 400px; margin-left: auto; margin-right: auto; box-sizing: border-box; background-color: #ccc; border: 1px solid #999; border-radius: 5px;} form.update-page-form div {padding: 10px;} form.update-page-form input {display: block; float: left;} form.update-page-form input[type="number"] {width: 170px;} form.update-page-form input[type="checkbox"] {margin-left: calc(170px - 0.5em);} form.update-page-form input[type="submit"] {float: none; margin-left: 200px;} form.update-page-form label {display: block; margin-left: 200px;} </style> </head> <body> <header> <h1>Example of a Filter and Sort Table ... </h1> <form action="?submitted" method="get" class="update-page-form"> <div><input type="number" name="i" value="<?php echo isset($_GET['i']) ? (int) $_GET['i'] : 300; ?>"/><label for="i">number of rows</label></div> <div><input type="checkbox" name="nojson" value="1" <?php echo isset($_GET['nojson']) && 1 === $_GET['nojson'] ? 'checked="checked"' : ''; ?> /><label for="nojson">without json data</label></div> <div><input type="submit" value="reload" /></div> </form> <p>Below the table is an examle of the <a href="#html">HTML</a> you need to use.</p> </header> <main class="tfs-holder loading"> <div class="loading-screen"> <p class="loader">loading ...</p> <div class="load-bar"> <div class="bar"></div> <div class="bar"></div> <div class="bar"></div> </div> </div> <div class="tfs-more-entries pagination-top"> <span class="line"> <span class="pagination"></span> </span> </div> <div class="tfs-current-favourites header-block"> <ul> <li class="filter-for-favourites tfs-action"> <a href="#">Show Favourites</a> </li> <li class="tfs-save-and-load load favourites tfs-action"> <a href="#">Load Favourites</a> </li> <li class="tfs-save-and-load save favourites tfs-action"> <a href="#">Save Favourites</a> </li> </ul> </div> <div class="tfs-common-content-holder header-block" data-title="Common Info"></div> <div class="tfs-filter-form-holder header-block"> <div class="tfs-filter-form-holder-section top left"> <ul> <li class="tfs-open-filter-form tfs-action open-filter"> <a href="#">Create Filter</a> </li> <li class="quick-keyword tfs-action"> <input name="QuickKeyword" placeholder="Quick Search ..." /> </li> </ul> </div> <div class="tfs-filter-form-holder-section top right"> <ul> <li class="tfs-save-and-load save filters tfs-action"> <a href="#">Save Filter</a> </li> <li class="tfs-save-and-load load filters tfs-action"> <a href="#">Load Filter</a> </li> <li class="tfs-current-search-holder" data-no-filter-text="No filter selected"></li> <li class="tfs-match-count-holder"> <strong>Results:</strong> <span class="match-row-number">0</span> / <span class="total-row-number">0</span> </li> <li class="tfs-no-match-count-holder"> <strong>Total Rows:</strong> <span class="total-row-number">0</span> </li> </ul> </div> <div class="tfs-filter-form-inner"></div> <div class="tfs-filter-form-holder-section bottom left"> <ul> <li class="tfs-clear tfs-action"> <a href="#">Clear Filter</a> </li> </ul> </div> <div class="tfs-filter-form-holder-section bottom right"> <ul> <li class="tfs-open-filter-form tfs-action"> <a href="#">Close and Apply</a> </li> </ul> </div> </div> <table class="tfs-table"> <thead class="header-block"> <tr> <th>MORE</th> <th>MARK</th> <th scope="col"> <a href="#" class="sortable" data-sort-field="SKU" data-sort-direction="asc" data-sort-type="number" >SKU</a> </th> <th scope="col">Type</th> <th scope="col">Producer</th> <th scope="col">Colour</th> <th scope="col"> <a href="#" class="sortable" data-sort-field="Size" data-sort-direction="asc" data-sort-type="string" >Size</a> </th> <th scope="col"> <a href="#" class="sortable" data-sort-field="Weight" data-sort-direction="asc" data-sort-type="number" >Weight</a> </th> <th scope="col"> <a href="#" class="sortable" data-sort-field="Price" data-sort-direction="desc" data-sort-type="number" data-sort-default="true" >Price</a> </th> <th scope="col"> <a href="#" class="sortable" data-sort-field="Rating" data-sort-direction="desc" data-sort-type="number" >Rating</a> </th> <th scope="col">Description</th> <th scope="col">User Input <a href="#" class="sortable" data-sort-field="your selection" data-sort-direction="asc" data-sort-type="string" >Selection?</a> <a href="#" class="sortable" data-sort-field="your description" data-sort-direction="asc" data-sort-type="string" >description?</a> </th> </tr> </thead> <tbody> <?php echo $html; ?> </tbody> </table> <p class="message warning no-matches-message">No entries match your filter</p> <div class="tfs-more-entries pagination-bottom"> <span class="line"> <span class="pagination"></span> </span> </div> <div class="tfs-more-entries-always-show"> <span class="line"> <strong>Entries per Page:</strong> <span class="total-showing-row-number"> <input type="number" min="1" max="10000" step="50" name="VisibleRowCount" class="visible-row-count" /> </span> - <strong>Currently Showing:</strong> <span class="min-row-number">0</span> - <span class="max-row-number">0</span> of <span class="match-row-number"></span> / <span class="total-row-number">0</span> </span> </div> </main> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="../../javascript/jsurl.js?x=<?php echo rand(0, 9999999999); ?>"></script> <script src="../../javascript/jquery.simplemodal-1.4.5?x=<?php echo rand(0, 9999999999); ?>"></script> <script src="../../javascript/js.cookies.js?x=<?php echo rand(0, 9999999999); ?>"></script> <script src="../../javascript/awesomplete.js?x=<?php echo rand(0, 9999999999); ?>"></script> <script src="../../javascript/doT.js?x=<?php echo rand(0, 9999999999); ?>"></script> <script src="../../javascript/TableFilterSort.js?x=<?php echo rand(0, 9999999999); ?>"></script> <script type="text/javascript"> if(! Array.isArray(TableFilterSortVars)) { var TableFilterSortVars = []; } TableFilterSortVars.push( { debug: true, mySelector: ".tfs-holder", rowRawData: <?php echo $useJSON ? json_encode($jsonArray) : 'null'; ?>, filtersParentPageID: 'Test Filter', favouritesParentPageID: 'Test Favourites', dataDictionary: { OriginalProducer: { Label: 'Original Producer' } } } ); </script> <script> var html = jQuery('.tfs-holder').first().clone(); var firstRow = html.find('table tbody tr').get(0).outerHTML; html.find('table tbody').remove(); html.find('table').append('\n\n<tbody>\n ' + firstRow + '\n</tbody>'+'\n\n\n '); html = html.html(); html = html.split('<').join('<') html = html.split('>').join('>') document.write('<h2 id="html">Template Example</h2><pre style="white-space: pre-wrap; word-wrap: break-word;"><main class="tfs-holder">' + html + '</main></pre>'); </script> </html> <?php class RandomTestValues { protected $data = [ 'colours' => [ 'orange', 'red', 'blue', 'indigo', 'violet', 'green', 'yellow', 'Crazy Dots', ], 'sizes' => [ 'Small', 'Medium', 'Large', 'X-Large', 'XX-Large', ], 'veg' => [ 'Artichoke', 'Artichoke Large', 'ArtichokeLarge', 'ArtichokeTest', 'Arugula', 'Asparagus', 'Aubergine (UK) = Eggplant (US)', 'Amaranth', 'Legumes', 'Alfalfa sprouts', 'Azuki beans (or adzuki)', 'Bean sprouts', 'Black beans', 'Black-eyed peas', 'Borlotti bean', 'Broad beans', 'Chickpeas, Garbanzos, or ceci beans', 'Green beans', 'Kidney beans', 'Lentils', 'Lima beans or Butter bean', 'Mung beans', 'Navy beans', 'Pinto beans', 'Runner beans', 'Split peas', 'Soy beans', 'Peas', 'Mangetout or Snap peas', 'Beet greens (see also chard)', 'Bok choy (known as Bok choy in UK and US)', 'Broccoflower (a hybrid)', 'Broccoli', 'Brussels sprouts', 'Cabbage', 'Calabrese', 'Carrots', 'Cauliflower', 'Celery', 'Chard', 'Collard greens', 'Corn salad', 'Endive', 'Fiddleheads (young coiled fern leaves)', 'Frisee', 'Fennel', 'Herbs and spices', 'Anise', 'Basil', 'Caraway', 'Cilantro seeds are Coriander', 'Chamomile', 'Dill', 'Fennel', 'Lavender', 'Lemon Grass', 'Marjoram', 'Oregano', 'Parsley', 'Rosemary', 'Sage', 'Thyme', 'Kale', 'Kohlrabi', 'Lettuce Lactuca sativa', 'Maize (UK) = Corn (US) = Sweetcorn (actually a grain)', 'Mushrooms (actually a fungus, not a plant)', 'Mustard greens', 'Nettles', 'New Zealand spinach', 'Okra', 'Onion family', 'Chives', 'Garlic', 'Leek Allium porrum', 'Onion', 'Shallot', 'Spring onion (UK) == Green onion (US) == Scallion', 'Parsley', 'Peppers (biologically fruits, but taxed as vegetables)', 'Green pepper and Red pepper == bell pepper == pimento', 'Chili pepper == Capsicum', 'Jalapeño', 'Habanero', 'Paprika', 'Tabasco pepper', 'Cayenne pepper', 'Radicchio', 'Rhubarb', 'Root vegetables', 'Beetroot (UK) == Beet (US)', 'mangel-wurzel: a variety of beet used mostly as cattlefeed', 'Carrot', 'Celeriac', 'Daikon', 'Ginger', 'Parsnip', 'Rutabaga', 'Turnip', 'Radish', 'Swede (UK) == Rutabaga (US)', 'Turnip', 'Wasabi', 'Horseradish', 'White radish', 'Salsify (usually Purple Salsify or Oyster Plant)', 'Skirret', 'Spinach', 'Topinambur', 'Squashes (biologically fruits, but taxed as vegetables)', 'Acorn squash', 'Butternut squash', 'Banana squash', 'Courgette (UK) == Zucchini (US)', 'Cucumber (biologically fruits, but taxed as vegetables)', 'Delicata', 'Gem squash', 'Hubbard squash', 'Marrow (UK) == Squash (US) Cucurbita maxima', 'Patty pans', 'Pumpkin', 'Spaghetti squash', 'Tat soi', 'Tomato (biologically a fruit, but taxed as a vegetable)', 'Tubers', 'Jicama', 'Jerusalem artichoke', 'Potato', 'Sunchokes', 'Sweet potato', 'Taro', 'Yam (Yam and Sweet Potato are NOT the same)', 'Turnip greens', 'Water chestnut', 'Watercress', 'Zucchini', ], ]; public static function get_sample_array_by_type($type) { $obj = new RandomTestValues(); return $obj->getData($type); } public function getData($type) { return $this->data[$type]; } } /** * 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 <josh@gravityblvd.com> * @copyright Copyright 2014, 2015, 2016 Josh Sherman * @license http://www.opensource.org/licenses/mit-license.html * * @see https://github.com/joshtronic/php-loremipsum */ class LoremIpsum { /** * 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. * * @var array */ public $words = [ // Lorem ipsum... 'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', // 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', 'cum', 'curabitur', 'curae', 'cursus', 'dapibus', 'diam', 'dictum', 'dictumst', 'dignissim', 'dis', 'donec', 'dui', 'duis', 'egestas', 'eget', 'eleifend', 'elementum', 'enim', 'erat', 'eros', 'est', 'et', 'etiam', 'eu', 'euismod', 'facilisi', 'facilisis', 'fames', 'faucibus', 'felis', 'fermentum', 'feugiat', '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', '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', 'sociis', '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', ]; /** * First. * * Whether or not we should be starting the string with "Lorem ipsum..." * * @var bool */ private $first = true; /** * Word. * * Generates a single word of lorem ipsum. * * @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 $this->words(1, $tags); } /** * Words Array. * * Generates an array of lorem ipsum words. * * @param int $count how many words to generate * @param mixed $tags string or array of HTML tags to wrap output with * * @return array generated lorem ipsum words */ public function wordsArray($count = 1, $tags = false) { return $this->words($count, $tags, true); } /** * Words. * * Generates words of lorem ipsum. * * @param int $count how many words to generate * @param mixed $tags string or array of HTML tags to wrap output with * @param bool $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) { $words = []; $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. * * @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 $this->sentences(1, $tags); } /** * Sentences Array. * * Generates an array of lorem ipsum sentences. * * @param int $count how many sentences to generate * @param mixed $tags string or array of HTML tags to wrap output with * * @return array generated lorem ipsum sentences */ public function sentencesArray($count = 1, $tags = false) { return $this->sentences($count, $tags, true); } /** * Sentences. * * Generates sentences of lorem ipsum. * * @param int $count how many sentences to generate * @param mixed $tags string or array of HTML tags to wrap output with * @param bool $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 = []; 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. * * @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 $this->paragraphs(1, $tags); } /** * Paragraph Array. * * Generates an array of lorem ipsum paragraphs. * * @param int $count how many paragraphs to generate * @param mixed $tags string or array of HTML tags to wrap output with * * @return array generated lorem ipsum paragraphs */ public function paragraphsArray($count = 1, $tags = false) { return $this->paragraphs($count, $tags, true); } /** * Paragraphss. * * Generates paragraphs of lorem ipsum. * * @param int $count how many paragraphs to generate * @param mixed $tags string or array of HTML tags to wrap output with * @param bool $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 = []; for ($i = 0; $i < $count; ++$i) { $paragraphs[] = $this->sentences($this->gauss(5.8, 1.93)); } return $this->output($paragraphs, $tags, $array, "\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. * * @param float $mean average value * @param float $std_dev stadnard deviation * * @return float 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 * M_PI * $y); return $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. */ 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. * * @param array $sentences the sentences we would like to punctuate */ 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 = round($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. * * @param array $strings an array of generated strings * @param mixed $tags string or array of HTML tags to wrap output with * @param bool $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) { $tags = is_array($tags) ? array_reverse($tags) : [$tags]; foreach ($strings as $key => $string) { foreach ($tags as $tag) { $string = '<' === $tag[0] ? str_replace('$1', $string, $tag) : sprintf('<%1$s>%2$s</%1$s>', $tag, $string); $strings[$key] = $string; } } } if (! $array) { $strings = implode($delimiter, $strings); } return $strings; } } |