Added word generation
Markup injection is working as well. Next up sentences!
This commit is contained in:
parent
6ea259cb77
commit
3859b0da7c
4 changed files with 494 additions and 6 deletions
19
README.md
19
README.md
|
@ -1,7 +1,7 @@
|
||||||
php-loremipsum
|
php-loremipsum
|
||||||
==============
|
==============
|
||||||
|
|
||||||
Lorem ipsum generator without dependencies.
|
Lorem ipsum generator in PHP without dependencies.
|
||||||
|
|
||||||
Origins
|
Origins
|
||||||
-------
|
-------
|
||||||
|
@ -10,6 +10,8 @@ Once upon a time, I was attempting to find a lorem ipsum generator over on [Pack
|
||||||
|
|
||||||
I had decided to use [badcow/lorem-ipsum](https://packagist.org/packages/badcow/lorem-ipsum) because it did not have any dependencies nor did it rely on any external APIs. As I started to use the library, I found that I was going to have to fight with it to get it to do what I wanted. After digging through the code, I realized that I was going to end up gutting most of it to bend it to my will. I know when you overhaul someone’s code the liklihood of them accepting a pull request goes down dramatically, hence building this library while taking cues from it’s predecessor.
|
I had decided to use [badcow/lorem-ipsum](https://packagist.org/packages/badcow/lorem-ipsum) because it did not have any dependencies nor did it rely on any external APIs. As I started to use the library, I found that I was going to have to fight with it to get it to do what I wanted. After digging through the code, I realized that I was going to end up gutting most of it to bend it to my will. I know when you overhaul someone’s code the liklihood of them accepting a pull request goes down dramatically, hence building this library while taking cues from it’s predecessor.
|
||||||
|
|
||||||
|
Also, the aforementioned package had a bunch of “setter” and “getter” methods that were grossing me out :P
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
|
@ -29,8 +31,8 @@ Usage
|
||||||
### Getting Started
|
### Getting Started
|
||||||
|
|
||||||
```php
|
```php
|
||||||
require_once 'GravityBlvd/LoremIpsum.php';
|
require_once 'joshtronic/LoremIpsum.php';
|
||||||
$lipsum = new GravityBlvd\LoremIpsum();
|
$lipsum = new joshtronic\LoremIpsum();
|
||||||
```
|
```
|
||||||
|
|
||||||
### Generating Words
|
### Generating Words
|
||||||
|
@ -99,6 +101,11 @@ print_r($lipsum->wordsArray(5), 'li');
|
||||||
Assumptions
|
Assumptions
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
* The first sentence or paragraph will always start with “Lorem ipsum…”
|
Instead of having an option as to whether or not a string should start the generated output with “Lorem ipsum dolor sit amet, consectetur adipiscing elit.” a few assumptions are baked in. The first string generated will always start with the traditional “Lorem ipsum…”. Subsequent strings may contain those words but will not explicitly start with them.
|
||||||
* Subsequent sentences and paragraphs will never start with “Lorem ipsum…”
|
|
||||||
* Words will never contain “lorem” or “ipsum”
|
Contributing
|
||||||
|
------------
|
||||||
|
|
||||||
|
Suggestions and bug reports are always welcome, but karma points was earned for pull requests.
|
||||||
|
|
||||||
|
Unit tests are required for all contributions. You can run the test suite from the `tests` directory simply by running `phpunit .`
|
||||||
|
|
174
src/LoremIpsum.php
Normal file
174
src/LoremIpsum.php
Normal file
|
@ -0,0 +1,174 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace joshtronic;
|
||||||
|
|
||||||
|
class LoremIpsum
|
||||||
|
{
|
||||||
|
private $first = true;
|
||||||
|
private $sentence_mean = 24.46;
|
||||||
|
private $sentence_stdev = 5.08;
|
||||||
|
private $paragraph_mean = 5.8;
|
||||||
|
private $paragraph_stdev = 1.93;
|
||||||
|
|
||||||
|
public $words = array(
|
||||||
|
// 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',
|
||||||
|
);
|
||||||
|
|
||||||
|
public function word($tags = false)
|
||||||
|
{
|
||||||
|
return $this->words(1, $tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function wordsArray($count = 1, $tags = false)
|
||||||
|
{
|
||||||
|
return $this->words($count, $tags, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function words($count = 1, $tags = false, $array = false)
|
||||||
|
{
|
||||||
|
$words = array();
|
||||||
|
|
||||||
|
if ($this->first)
|
||||||
|
{
|
||||||
|
if ($count > 8)
|
||||||
|
{
|
||||||
|
$start = 8;
|
||||||
|
$count -= 8;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$start = $count;
|
||||||
|
$count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$words = array_slice($this->words, 0, $start);
|
||||||
|
$this->first = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($count)
|
||||||
|
{
|
||||||
|
shuffle($this->words);
|
||||||
|
$words += array_slice($this->words, 0, $count);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($tags)
|
||||||
|
{
|
||||||
|
$this->markup($words, $tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$array)
|
||||||
|
{
|
||||||
|
$words = implode(' ', $words);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $words;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sentence()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sentences()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function sentencesArray()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function paragraph()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function paragraphs()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function paragraphsArray()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function markup(&$strings, $tags)
|
||||||
|
{
|
||||||
|
if (!is_array($tags))
|
||||||
|
{
|
||||||
|
$tags = array($tags);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$tags = array_reverse($tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($strings as $key => $string)
|
||||||
|
{
|
||||||
|
foreach ($tags as $tag)
|
||||||
|
{
|
||||||
|
if ($tag[0] == '<')
|
||||||
|
{
|
||||||
|
$string = str_replace('$1', $string, $tag);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
|
||||||
|
}
|
||||||
|
|
||||||
|
$strings[$key] = $string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
187
src/words
Normal file
187
src/words
Normal file
|
@ -0,0 +1,187 @@
|
||||||
|
array (
|
||||||
|
0 => 'lorem',
|
||||||
|
1 => 'ipsum',
|
||||||
|
2 => 'dolor',
|
||||||
|
3 => 'sit',
|
||||||
|
4 => 'amet',
|
||||||
|
5 => 'consectetur',
|
||||||
|
6 => 'adipiscing',
|
||||||
|
7 => 'elit',
|
||||||
|
|
||||||
|
8 => 'morbi',
|
||||||
|
9 => 'pulvinar',
|
||||||
|
10 => 'nisl',
|
||||||
|
11 => 'nec',
|
||||||
|
12 => 'mauris',
|
||||||
|
13 => 'tincidunt',
|
||||||
|
14 => 'in',
|
||||||
|
15 => 'luctus',
|
||||||
|
16 => 'purus',
|
||||||
|
17 => 'ut',
|
||||||
|
18 => 'nulla',
|
||||||
|
19 => 'tristique',
|
||||||
|
20 => 'non',
|
||||||
|
21 => 'porttitor',
|
||||||
|
22 => 'dictum',
|
||||||
|
23 => 'nam',
|
||||||
|
24 => 'ac',
|
||||||
|
25 => 'metus',
|
||||||
|
26 => 'et',
|
||||||
|
27 => 'tempus',
|
||||||
|
28 => 'venenatis',
|
||||||
|
29 => 'rutrum',
|
||||||
|
30 => 'praesent',
|
||||||
|
31 => 'at',
|
||||||
|
32 => 'volutpat',
|
||||||
|
33 => 'quam',
|
||||||
|
34 => 'curabitur',
|
||||||
|
35 => 'tortor',
|
||||||
|
36 => 'vitae',
|
||||||
|
37 => 'odio',
|
||||||
|
38 => 'condimentum',
|
||||||
|
39 => 'tempor',
|
||||||
|
40 => 'id',
|
||||||
|
41 => 'massa',
|
||||||
|
42 => 'sed',
|
||||||
|
43 => 'sollicitudin',
|
||||||
|
44 => 'hendrerit',
|
||||||
|
45 => 'sodales',
|
||||||
|
46 => 'iaculis',
|
||||||
|
47 => 'enim',
|
||||||
|
48 => 'pellentesque',
|
||||||
|
49 => 'blandit',
|
||||||
|
50 => 'lectus',
|
||||||
|
51 => 'turpis',
|
||||||
|
52 => 'maecenas',
|
||||||
|
53 => 'vel',
|
||||||
|
54 => 'eget',
|
||||||
|
55 => 'justo',
|
||||||
|
56 => 'integer',
|
||||||
|
57 => 'faucibus',
|
||||||
|
58 => 'pretium',
|
||||||
|
59 => 'lacinia',
|
||||||
|
60 => 'nullam',
|
||||||
|
61 => 'a',
|
||||||
|
62 => 'velit',
|
||||||
|
63 => 'posuere',
|
||||||
|
64 => 'arcu',
|
||||||
|
65 => 'augue',
|
||||||
|
66 => 'magna',
|
||||||
|
67 => 'commodo',
|
||||||
|
68 => 'dui',
|
||||||
|
69 => 'duis',
|
||||||
|
70 => 'vehicula',
|
||||||
|
71 => 'egestas',
|
||||||
|
72 => 'convallis',
|
||||||
|
73 => 'aenean',
|
||||||
|
74 => 'erat',
|
||||||
|
75 => 'sem',
|
||||||
|
76 => 'imperdiet',
|
||||||
|
77 => 'cras',
|
||||||
|
78 => 'diam',
|
||||||
|
79 => 'ullamcorper',
|
||||||
|
80 => 'laoreet',
|
||||||
|
81 => 'ornare',
|
||||||
|
82 => 'eu',
|
||||||
|
83 => 'vulputate',
|
||||||
|
84 => 'vestibulum',
|
||||||
|
85 => 'porta',
|
||||||
|
86 => 'dapibus',
|
||||||
|
87 => 'nisi',
|
||||||
|
88 => 'malesuada',
|
||||||
|
89 => 'congue',
|
||||||
|
90 => 'orci',
|
||||||
|
91 => 'sagittis',
|
||||||
|
92 => 'nunc',
|
||||||
|
93 => 'quis',
|
||||||
|
94 => 'lacus',
|
||||||
|
95 => 'eros',
|
||||||
|
96 => 'phasellus',
|
||||||
|
97 => 'varius',
|
||||||
|
98 => 'mollis',
|
||||||
|
99 => 'aliquam',
|
||||||
|
100 => 'nibh',
|
||||||
|
101 => 'molestie',
|
||||||
|
102 => 'placerat',
|
||||||
|
103 => 'libero',
|
||||||
|
104 => 'tellus',
|
||||||
|
105 => 'donec',
|
||||||
|
106 => 'accumsan',
|
||||||
|
107 => 'est',
|
||||||
|
108 => 'ultrices',
|
||||||
|
109 => 'mattis',
|
||||||
|
110 => 'leo',
|
||||||
|
111 => 'neque',
|
||||||
|
112 => 'felis',
|
||||||
|
113 => 'ante',
|
||||||
|
114 => 'facilisis',
|
||||||
|
115 => 'bibendum',
|
||||||
|
116 => 'etiam',
|
||||||
|
117 => 'fermentum',
|
||||||
|
118 => 'feugiat',
|
||||||
|
119 => 'lobortis',
|
||||||
|
120 => 'cursus',
|
||||||
|
121 => 'dignissim',
|
||||||
|
122 => 'primis',
|
||||||
|
123 => 'cubilia',
|
||||||
|
124 => 'curae;',
|
||||||
|
125 => 'risus',
|
||||||
|
126 => 'proin',
|
||||||
|
127 => 'gravida',
|
||||||
|
128 => 'interdum',
|
||||||
|
129 => 'elementum',
|
||||||
|
130 => 'fusce',
|
||||||
|
131 => 'scelerisque',
|
||||||
|
132 => 'auctor',
|
||||||
|
133 => 'pharetra',
|
||||||
|
134 => 'ligula',
|
||||||
|
135 => 'fringilla',
|
||||||
|
136 => 'viverra',
|
||||||
|
137 => 'euismod',
|
||||||
|
138 => 'ultricies',
|
||||||
|
139 => 'suspendisse',
|
||||||
|
140 => 'vivamus',
|
||||||
|
141 => 'consequat',
|
||||||
|
142 => 'eleifend',
|
||||||
|
143 => 'aliquet',
|
||||||
|
144 => 'sapien',
|
||||||
|
145 => 'quisque',
|
||||||
|
146 => 'facilisi',
|
||||||
|
147 => 'rhoncus',
|
||||||
|
148 => 'urna',
|
||||||
|
149 => 'semper',
|
||||||
|
150 => 'mi',
|
||||||
|
151 => 'suscipit',
|
||||||
|
152 => 'cum',
|
||||||
|
153 => 'sociis',
|
||||||
|
154 => 'natoque',
|
||||||
|
155 => 'penatibus',
|
||||||
|
156 => 'magnis',
|
||||||
|
157 => 'dis',
|
||||||
|
158 => 'parturient',
|
||||||
|
159 => 'montes',
|
||||||
|
160 => 'nascetur',
|
||||||
|
161 => 'ridiculus',
|
||||||
|
162 => 'mus',
|
||||||
|
163 => 'class',
|
||||||
|
164 => 'aptent',
|
||||||
|
165 => 'taciti',
|
||||||
|
166 => 'sociosqu',
|
||||||
|
167 => 'ad',
|
||||||
|
168 => 'litora',
|
||||||
|
169 => 'torquent',
|
||||||
|
170 => 'per',
|
||||||
|
171 => 'conubia',
|
||||||
|
172 => 'nostra',
|
||||||
|
173 => 'inceptos',
|
||||||
|
174 => 'himenaeos',
|
||||||
|
175 => 'hac',
|
||||||
|
176 => 'habitasse',
|
||||||
|
177 => 'platea',
|
||||||
|
178 => 'dictumst',
|
||||||
|
179 => 'potenti',
|
||||||
|
180 => 'fames',
|
||||||
|
181 => 'habitant',
|
||||||
|
182 => 'senectus',
|
||||||
|
183 => 'netus',
|
||||||
|
)
|
120
tests/LoremIpsumTest.php
Normal file
120
tests/LoremIpsumTest.php
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once '../src/LoremIpsum.php';
|
||||||
|
|
||||||
|
class LoremIpsumTest extends PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
private $lipsum;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
$this->lipsum = new joshtronic\LoremIpsum();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWord()
|
||||||
|
{
|
||||||
|
$this->assertRegExp('/^[a-z]+$/i', $this->lipsum->word());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWords()
|
||||||
|
{
|
||||||
|
$this->assertRegExp(
|
||||||
|
'/^[a-z]+ [a-z]+ [a-z]+$/i',
|
||||||
|
$this->lipsum->words(3)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWordsArray()
|
||||||
|
{
|
||||||
|
$words = $this->lipsum->wordsArray(3);
|
||||||
|
$this->assertTrue(is_array($words));
|
||||||
|
$this->assertCount(3, $words);
|
||||||
|
|
||||||
|
foreach ($words as $word)
|
||||||
|
{
|
||||||
|
$this->assertRegExp('/^[a-z]+$/i', $word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public function testSentence()
|
||||||
|
{
|
||||||
|
$this->assertRegExp('/^[a-z, ]+\.$/i', $this->lipsum->sentence());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSentences()
|
||||||
|
{
|
||||||
|
$this->assertRegExp('/^[a-z, ]+\. [a-z, ]+\. [a-z, ]+\.$/i', $this->lipsum->sentences(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSentencesArray()
|
||||||
|
{
|
||||||
|
$sentences = $this->lipsum->sentencesArray(3);
|
||||||
|
$this->assertTrue(is_array($sentences));
|
||||||
|
$this->assertCount(3, $sentences);
|
||||||
|
|
||||||
|
foreach ($words as $word)
|
||||||
|
{
|
||||||
|
$this->assertRegExp('/^[a-z, ]+\.$/i', $sentence);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParagraph()
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->fail('Not yet implemented.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParagraphs()
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->fail('Not yet implemented.');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testParagraphsArray()
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->fail('Not yet implemented.');
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function testMarkupString()
|
||||||
|
{
|
||||||
|
$this->assertRegExp(
|
||||||
|
'/^<li>[a-z]+<\/li>$/i',
|
||||||
|
$this->lipsum->word('li')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMarkupArray()
|
||||||
|
{
|
||||||
|
$this->assertRegExp(
|
||||||
|
'/^<div><p>[a-z]+<\/p><\/div>$/i',
|
||||||
|
$this->lipsum->word(array('div', 'p'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testMarkupBackReference()
|
||||||
|
{
|
||||||
|
$this->assertRegExp(
|
||||||
|
'/^<li><a href="[a-z]+">[a-z]+<\/a><\/li>$/i',
|
||||||
|
$this->lipsum->word('<li><a href="$1">$1</a></li>')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public function testMarkupArrayReturn()
|
||||||
|
{
|
||||||
|
$words = $this->lipsum->wordsArray(3, 'li');
|
||||||
|
$this->assertTrue(is_array($words));
|
||||||
|
$this->assertCount(3, $words);
|
||||||
|
|
||||||
|
foreach ($words as $word)
|
||||||
|
{
|
||||||
|
$this->assertRegExp('/^<li>[a-z]+<\/li>$/i', $word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
Add table
Add a link
Reference in a new issue