Code cleanup
Converted tabs to spaces, dropped closing ?> and reformatted README to 80 columns.
This commit is contained in:
parent
68e162388d
commit
966f2d26bc
3 changed files with 487 additions and 465 deletions
46
README.md
46
README.md
|
@ -10,23 +10,40 @@
|
||||||
[packagist]: https://packagist.org/packages/joshtronic/php-loremipsum
|
[packagist]: https://packagist.org/packages/joshtronic/php-loremipsum
|
||||||
[gittip]: https://www.gittip.com/joshtronic/
|
[gittip]: https://www.gittip.com/joshtronic/
|
||||||
|
|
||||||
Lorem ipsum generator in PHP without dependencies. Compatible with PHP 5.3+ as well as HHVM.
|
Lorem ipsum generator in PHP without dependencies. Compatible with PHP 5.3+ as
|
||||||
|
well as HHVM.
|
||||||
|
|
||||||
## Origins
|
## Origins
|
||||||
|
|
||||||
Once upon a time, I was attempting to find a lorem ipsum generator over on [Packagist](https://packagist.org/search/?q=lorem%20ipsum). I was presented with many options, and some of those options were good. Unfortunately, the bulk of those options depended on Symphony or the Zend Framework. This wouldn’t have been a big deal but under the circumstances, I wanted something that was not tightly coupled to these frameworks because I wanted to use the generator in my _own_ framework.
|
Once upon a time, I was attempting to find a lorem ipsum generator over on
|
||||||
|
[Packagist](https://packagist.org/search/?q=lorem%20ipsum). I was presented
|
||||||
|
with many options, and some of those options were good. Unfortunately, the
|
||||||
|
bulk of those options depended on Symphony or the Zend Framework. This
|
||||||
|
wouldn’t have been a big deal but under the circumstances, I wanted something
|
||||||
|
that was not tightly coupled to these frameworks because I wanted to use the
|
||||||
|
generator in my _own_ framework.
|
||||||
|
|
||||||
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
|
Also, the aforementioned package had a bunch of “setter” and “getter” methods
|
||||||
|
that were grossing me out :P
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
The preferred installation method is via `composer`. First add the following to your `composer.json`
|
The preferred installation method is via `composer`. First add the following
|
||||||
|
to your `composer.json`
|
||||||
|
|
||||||
```json
|
```json
|
||||||
"require": {
|
"require": {
|
||||||
"joshtronic/php-loremipsum": "dev-master"
|
"joshtronic/php-loremipsum": "dev-master"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -64,7 +81,8 @@ echo '5 paragraphs: ' . $lipsum->paragraphs(5);
|
||||||
|
|
||||||
### Wrapping text with HTML tags
|
### Wrapping text with HTML tags
|
||||||
|
|
||||||
If you would like to wrap the generated text with a tag, pass it as the second parameter:
|
If you would like to wrap the generated text with a tag, pass it as the second
|
||||||
|
parameter:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
echo $lipsum->paragraphs(3, 'p');
|
echo $lipsum->paragraphs(3, 'p');
|
||||||
|
@ -77,7 +95,7 @@ Multiple tags can also be specified:
|
||||||
```php
|
```php
|
||||||
echo $lipsum->sentences(3, ['article', 'p']);
|
echo $lipsum->sentences(3, ['article', 'p']);
|
||||||
|
|
||||||
// Generptes: <article><p>...</p></article><article><p>...</p></article><article><p>...</p></article>
|
// Generates: <article><p>...</p></article><article><p>...</p></article><article><p>...</p></article>
|
||||||
```
|
```
|
||||||
|
|
||||||
And you can back reference using `$1`:
|
And you can back reference using `$1`:
|
||||||
|
@ -106,10 +124,16 @@ print_r($lipsum->wordsArray(5), 'li');
|
||||||
|
|
||||||
## Assumptions
|
## Assumptions
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
Suggestions and bug reports are always welcome, but karma points are earned for pull requests.
|
Suggestions and bug reports are always welcome, but karma points are 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 .`
|
Unit tests are required for all contributions. You can run the test suite
|
||||||
|
from the `tests` directory simply by running `phpunit .`
|
||||||
|
|
|
@ -18,395 +18,394 @@ namespace joshtronic;
|
||||||
|
|
||||||
class LoremIpsum
|
class LoremIpsum
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* First
|
* First
|
||||||
*
|
*
|
||||||
* Whether or not we should be starting the string with "Lorem ipsum..."
|
* Whether or not we should be starting the string with "Lorem ipsum..."
|
||||||
*
|
*
|
||||||
* @access private
|
* @access private
|
||||||
* @var boolean
|
* @var boolean
|
||||||
*/
|
*/
|
||||||
private $first = true;
|
private $first = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Words
|
* Words
|
||||||
*
|
*
|
||||||
* A lorem ipsum vocabulary of sorts. Not a complete list as I'm unsure if
|
* 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.
|
* a complete list exists and if so, where to get it.
|
||||||
*
|
*
|
||||||
* @access private
|
* @access private
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $words = array(
|
public $words = array(
|
||||||
// Lorem ipsum...
|
// Lorem ipsum...
|
||||||
'lorem', 'ipsum', 'dolor', 'sit',
|
'lorem', 'ipsum', 'dolor', 'sit',
|
||||||
'amet', 'consectetur', 'adipiscing', 'elit',
|
'amet', 'consectetur', 'adipiscing', 'elit',
|
||||||
// The rest of the vocabulary
|
// The rest of the vocabulary
|
||||||
'a', 'ac', 'accumsan', 'ad',
|
'a', 'ac', 'accumsan', 'ad',
|
||||||
'aenean', 'aliquam', 'aliquet', 'ante',
|
'aenean', 'aliquam', 'aliquet', 'ante',
|
||||||
'aptent', 'arcu', 'at', 'auctor',
|
'aptent', 'arcu', 'at', 'auctor',
|
||||||
'augue', 'bibendum', 'blandit', 'class',
|
'augue', 'bibendum', 'blandit', 'class',
|
||||||
'commodo', 'condimentum', 'congue', 'consequat',
|
'commodo', 'condimentum', 'congue', 'consequat',
|
||||||
'conubia', 'convallis', 'cras', 'cubilia',
|
'conubia', 'convallis', 'cras', 'cubilia',
|
||||||
'cum', 'curabitur', 'curae', 'cursus',
|
'cum', 'curabitur', 'curae', 'cursus',
|
||||||
'dapibus', 'diam', 'dictum', 'dictumst',
|
'dapibus', 'diam', 'dictum', 'dictumst',
|
||||||
'dignissim', 'dis', 'donec', 'dui',
|
'dignissim', 'dis', 'donec', 'dui',
|
||||||
'duis', 'egestas', 'eget', 'eleifend',
|
'duis', 'egestas', 'eget', 'eleifend',
|
||||||
'elementum', 'enim', 'erat', 'eros',
|
'elementum', 'enim', 'erat', 'eros',
|
||||||
'est', 'et', 'etiam', 'eu',
|
'est', 'et', 'etiam', 'eu',
|
||||||
'euismod', 'facilisi', 'facilisis', 'fames',
|
'euismod', 'facilisi', 'facilisis', 'fames',
|
||||||
'faucibus', 'felis', 'fermentum', 'feugiat',
|
'faucibus', 'felis', 'fermentum', 'feugiat',
|
||||||
'fringilla', 'fusce', 'gravida', 'habitant',
|
'fringilla', 'fusce', 'gravida', 'habitant',
|
||||||
'habitasse', 'hac', 'hendrerit', 'himenaeos',
|
'habitasse', 'hac', 'hendrerit', 'himenaeos',
|
||||||
'iaculis', 'id', 'imperdiet', 'in',
|
'iaculis', 'id', 'imperdiet', 'in',
|
||||||
'inceptos', 'integer', 'interdum', 'justo',
|
'inceptos', 'integer', 'interdum', 'justo',
|
||||||
'lacinia', 'lacus', 'laoreet', 'lectus',
|
'lacinia', 'lacus', 'laoreet', 'lectus',
|
||||||
'leo', 'libero', 'ligula', 'litora',
|
'leo', 'libero', 'ligula', 'litora',
|
||||||
'lobortis', 'luctus', 'maecenas', 'magna',
|
'lobortis', 'luctus', 'maecenas', 'magna',
|
||||||
'magnis', 'malesuada', 'massa', 'mattis',
|
'magnis', 'malesuada', 'massa', 'mattis',
|
||||||
'mauris', 'metus', 'mi', 'molestie',
|
'mauris', 'metus', 'mi', 'molestie',
|
||||||
'mollis', 'montes', 'morbi', 'mus',
|
'mollis', 'montes', 'morbi', 'mus',
|
||||||
'nam', 'nascetur', 'natoque', 'nec',
|
'nam', 'nascetur', 'natoque', 'nec',
|
||||||
'neque', 'netus', 'nibh', 'nisi',
|
'neque', 'netus', 'nibh', 'nisi',
|
||||||
'nisl', 'non', 'nostra', 'nulla',
|
'nisl', 'non', 'nostra', 'nulla',
|
||||||
'nullam', 'nunc', 'odio', 'orci',
|
'nullam', 'nunc', 'odio', 'orci',
|
||||||
'ornare', 'parturient', 'pellentesque', 'penatibus',
|
'ornare', 'parturient', 'pellentesque', 'penatibus',
|
||||||
'per', 'pharetra', 'phasellus', 'placerat',
|
'per', 'pharetra', 'phasellus', 'placerat',
|
||||||
'platea', 'porta', 'porttitor', 'posuere',
|
'platea', 'porta', 'porttitor', 'posuere',
|
||||||
'potenti', 'praesent', 'pretium', 'primis',
|
'potenti', 'praesent', 'pretium', 'primis',
|
||||||
'proin', 'pulvinar', 'purus', 'quam',
|
'proin', 'pulvinar', 'purus', 'quam',
|
||||||
'quis', 'quisque', 'rhoncus', 'ridiculus',
|
'quis', 'quisque', 'rhoncus', 'ridiculus',
|
||||||
'risus', 'rutrum', 'sagittis', 'sapien',
|
'risus', 'rutrum', 'sagittis', 'sapien',
|
||||||
'scelerisque', 'sed', 'sem', 'semper',
|
'scelerisque', 'sed', 'sem', 'semper',
|
||||||
'senectus', 'sociis', 'sociosqu', 'sodales',
|
'senectus', 'sociis', 'sociosqu', 'sodales',
|
||||||
'sollicitudin', 'suscipit', 'suspendisse', 'taciti',
|
'sollicitudin', 'suscipit', 'suspendisse', 'taciti',
|
||||||
'tellus', 'tempor', 'tempus', 'tincidunt',
|
'tellus', 'tempor', 'tempus', 'tincidunt',
|
||||||
'torquent', 'tortor', 'tristique', 'turpis',
|
'torquent', 'tortor', 'tristique', 'turpis',
|
||||||
'ullamcorper', 'ultrices', 'ultricies', 'urna',
|
'ullamcorper', 'ultrices', 'ultricies', 'urna',
|
||||||
'ut', 'varius', 'vehicula', 'vel',
|
'ut', 'varius', 'vehicula', 'vel',
|
||||||
'velit', 'venenatis', 'vestibulum', 'vitae',
|
'velit', 'venenatis', 'vestibulum', 'vitae',
|
||||||
'vivamus', 'viverra', 'volutpat', 'vulputate',
|
'vivamus', 'viverra', 'volutpat', 'vulputate',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Word
|
* Word
|
||||||
*
|
*
|
||||||
* Generates a single word of lorem ipsum.
|
* Generates a single word of lorem ipsum.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||||
* @return string generated lorem ipsum word
|
* @return string generated lorem ipsum word
|
||||||
*/
|
*/
|
||||||
public function word($tags = false)
|
public function word($tags = false)
|
||||||
{
|
{
|
||||||
return $this->words(1, $tags);
|
return $this->words(1, $tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Words Array
|
* Words Array
|
||||||
*
|
*
|
||||||
* Generates an array of lorem ipsum words.
|
* Generates an array of lorem ipsum words.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $count how many words to generate
|
* @param integer $count how many words to generate
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||||
* @return array generated lorem ipsum words
|
* @return array generated lorem ipsum words
|
||||||
*/
|
*/
|
||||||
public function wordsArray($count = 1, $tags = false)
|
public function wordsArray($count = 1, $tags = false)
|
||||||
{
|
{
|
||||||
return $this->words($count, $tags, true);
|
return $this->words($count, $tags, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Words
|
* Words
|
||||||
*
|
*
|
||||||
* Generates words of lorem ipsum.
|
* Generates words of lorem ipsum.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $count how many words to generate
|
* @param integer $count how many words to generate
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @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 boolean $array whether an array or a string should be returned
|
||||||
* @return mixed string or array of generated lorem ipsum words
|
* @return mixed string or array of generated lorem ipsum words
|
||||||
*/
|
*/
|
||||||
public function words($count = 1, $tags = false, $array = false)
|
public function words($count = 1, $tags = false, $array = false)
|
||||||
{
|
{
|
||||||
$words = array();
|
$words = array();
|
||||||
$word_count = 0;
|
$word_count = 0;
|
||||||
|
|
||||||
// Shuffles and appends the word list to compensate for count
|
// Shuffles and appends the word list to compensate for count
|
||||||
// arguments that exceed the size of our vocabulary list
|
// arguments that exceed the size of our vocabulary list
|
||||||
while ($word_count < $count)
|
while ($word_count < $count)
|
||||||
{
|
{
|
||||||
$shuffle = true;
|
$shuffle = true;
|
||||||
|
|
||||||
while ($shuffle)
|
while ($shuffle)
|
||||||
{
|
{
|
||||||
$this->shuffle();
|
$this->shuffle();
|
||||||
|
|
||||||
// Checks that the last word of the list and the first word of
|
// 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
|
// the list that's about to be appended are not the same
|
||||||
if (!$word_count || $words[$word_count - 1] != $this->words[0])
|
if (!$word_count || $words[$word_count - 1] != $this->words[0])
|
||||||
{
|
{
|
||||||
$words = array_merge($words, $this->words);
|
$words = array_merge($words, $this->words);
|
||||||
$word_count = count($words);
|
$word_count = count($words);
|
||||||
$shuffle = false;
|
$shuffle = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$words = array_slice($words, 0, $count);
|
$words = array_slice($words, 0, $count);
|
||||||
|
|
||||||
return $this->output($words, $tags, $array);
|
return $this->output($words, $tags, $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sentence
|
* Sentence
|
||||||
*
|
*
|
||||||
* Generates a full sentence of lorem ipsum.
|
* Generates a full sentence of lorem ipsum.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||||
* @return string generated lorem ipsum sentence
|
* @return string generated lorem ipsum sentence
|
||||||
*/
|
*/
|
||||||
public function sentence($tags = false)
|
public function sentence($tags = false)
|
||||||
{
|
{
|
||||||
return $this->sentences(1, $tags);
|
return $this->sentences(1, $tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sentences Array
|
* Sentences Array
|
||||||
*
|
*
|
||||||
* Generates an array of lorem ipsum sentences.
|
* Generates an array of lorem ipsum sentences.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $count how many sentences to generate
|
* @param integer $count how many sentences to generate
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||||
* @return array generated lorem ipsum sentences
|
* @return array generated lorem ipsum sentences
|
||||||
*/
|
*/
|
||||||
public function sentencesArray($count = 1, $tags = false)
|
public function sentencesArray($count = 1, $tags = false)
|
||||||
{
|
{
|
||||||
return $this->sentences($count, $tags, true);
|
return $this->sentences($count, $tags, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sentences
|
* Sentences
|
||||||
*
|
*
|
||||||
* Generates sentences of lorem ipsum.
|
* Generates sentences of lorem ipsum.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $count how many sentences to generate
|
* @param integer $count how many sentences to generate
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @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 boolean $array whether an array or a string should be returned
|
||||||
* @return mixed string or array of generated lorem ipsum sentences
|
* @return mixed string or array of generated lorem ipsum sentences
|
||||||
*/
|
*/
|
||||||
public function sentences($count = 1, $tags = false, $array = false)
|
public function sentences($count = 1, $tags = false, $array = false)
|
||||||
{
|
{
|
||||||
$sentences = array();
|
$sentences = array();
|
||||||
|
|
||||||
for ($i = 0; $i < $count; $i++)
|
for ($i = 0; $i < $count; $i++)
|
||||||
{
|
{
|
||||||
$sentences[] = $this->wordsArray($this->gauss(24.46, 5.08));
|
$sentences[] = $this->wordsArray($this->gauss(24.46, 5.08));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->punctuate($sentences);
|
$this->punctuate($sentences);
|
||||||
|
|
||||||
return $this->output($sentences, $tags, $array);
|
return $this->output($sentences, $tags, $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paragraph
|
* Paragraph
|
||||||
*
|
*
|
||||||
* Generates a full paragraph of lorem ipsum.
|
* Generates a full paragraph of lorem ipsum.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||||
* @return string generated lorem ipsum paragraph
|
* @return string generated lorem ipsum paragraph
|
||||||
*/
|
*/
|
||||||
public function paragraph($tags = false)
|
public function paragraph($tags = false)
|
||||||
{
|
{
|
||||||
return $this->paragraphs(1, $tags);
|
return $this->paragraphs(1, $tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paragraph Array
|
* Paragraph Array
|
||||||
*
|
*
|
||||||
* Generates an array of lorem ipsum paragraphs.
|
* Generates an array of lorem ipsum paragraphs.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $count how many paragraphs to generate
|
* @param integer $count how many paragraphs to generate
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @param mixed $tags string or array of HTML tags to wrap output with
|
||||||
* @return array generated lorem ipsum paragraphs
|
* @return array generated lorem ipsum paragraphs
|
||||||
*/
|
*/
|
||||||
public function paragraphsArray($count = 1, $tags = false)
|
public function paragraphsArray($count = 1, $tags = false)
|
||||||
{
|
{
|
||||||
return $this->paragraphs($count, $tags, true);
|
return $this->paragraphs($count, $tags, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Paragraphss
|
* Paragraphss
|
||||||
*
|
*
|
||||||
* Generates paragraphs of lorem ipsum.
|
* Generates paragraphs of lorem ipsum.
|
||||||
*
|
*
|
||||||
* @access public
|
* @access public
|
||||||
* @param integer $count how many paragraphs to generate
|
* @param integer $count how many paragraphs to generate
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @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 boolean $array whether an array or a string should be returned
|
||||||
* @return mixed string or array of generated lorem ipsum paragraphs
|
* @return mixed string or array of generated lorem ipsum paragraphs
|
||||||
*/
|
*/
|
||||||
public function paragraphs($count = 1, $tags = false, $array = false)
|
public function paragraphs($count = 1, $tags = false, $array = false)
|
||||||
{
|
{
|
||||||
$paragraphs = array();
|
$paragraphs = array();
|
||||||
|
|
||||||
for ($i = 0; $i < $count; $i++)
|
for ($i = 0; $i < $count; $i++)
|
||||||
{
|
{
|
||||||
$paragraphs[] = $this->sentences($this->gauss(5.8, 1.93));
|
$paragraphs[] = $this->sentences($this->gauss(5.8, 1.93));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->output($paragraphs, $tags, $array, "\n\n");
|
return $this->output($paragraphs, $tags, $array, "\n\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gaussian Distribution
|
* Gaussian Distribution
|
||||||
*
|
*
|
||||||
* This is some smart kid stuff. I went ahead and combined the N(0,1) logic
|
* 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
|
* 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
|
* number of words in a sentence, the number of sentences in a paragraph
|
||||||
* and the distribution of commas in a sentence.
|
* and the distribution of commas in a sentence.
|
||||||
*
|
*
|
||||||
* @access private
|
* @access private
|
||||||
* @param double $mean average value
|
* @param double $mean average value
|
||||||
* @param double $std_dev stadnard deviation
|
* @param double $std_dev stadnard deviation
|
||||||
* @return double calculated distribution
|
* @return double calculated distribution
|
||||||
*/
|
*/
|
||||||
private function gauss($mean, $std_dev)
|
private function gauss($mean, $std_dev)
|
||||||
{
|
{
|
||||||
$x = mt_rand() / mt_getrandmax();
|
$x = mt_rand() / mt_getrandmax();
|
||||||
$y = mt_rand() / mt_getrandmax();
|
$y = mt_rand() / mt_getrandmax();
|
||||||
$z = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
|
$z = sqrt(-2 * log($x)) * cos(2 * pi() * $y);
|
||||||
|
|
||||||
return $z * $std_dev + $mean;
|
return $z * $std_dev + $mean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shuffle
|
* Shuffle
|
||||||
*
|
*
|
||||||
* Shuffles the words, forcing "Lorem ipsum..." at the beginning if it is
|
* Shuffles the words, forcing "Lorem ipsum..." at the beginning if it is
|
||||||
* the first time we are generating the text.
|
* the first time we are generating the text.
|
||||||
*
|
*
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
private function shuffle()
|
private function shuffle()
|
||||||
{
|
{
|
||||||
if ($this->first)
|
if ($this->first)
|
||||||
{
|
{
|
||||||
$this->first = array_slice($this->words, 0, 8);
|
$this->first = array_slice($this->words, 0, 8);
|
||||||
$this->words = array_slice($this->words, 8);
|
$this->words = array_slice($this->words, 8);
|
||||||
|
|
||||||
shuffle($this->words);
|
shuffle($this->words);
|
||||||
|
|
||||||
$this->words = $this->first + $this->words;
|
$this->words = $this->first + $this->words;
|
||||||
|
|
||||||
$this->first = false;
|
$this->first = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
shuffle($this->words);
|
shuffle($this->words);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Punctuate
|
* Punctuate
|
||||||
*
|
*
|
||||||
* Applies punctuation to a sentence. This includes a period at the end,
|
* 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
|
* the injection of commas as well as capitalizing the first letter of the
|
||||||
* first word of the sentence.
|
* first word of the sentence.
|
||||||
*
|
*
|
||||||
* @access private
|
* @access private
|
||||||
* @param array $sentences the sentences we would like to punctuate
|
* @param array $sentences the sentences we would like to punctuate
|
||||||
*/
|
*/
|
||||||
private function punctuate(&$sentences)
|
private function punctuate(&$sentences)
|
||||||
{
|
{
|
||||||
foreach ($sentences as $key => $sentence)
|
foreach ($sentences as $key => $sentence)
|
||||||
{
|
{
|
||||||
$words = count($sentence);
|
$words = count($sentence);
|
||||||
|
|
||||||
// Only worry about commas on sentences longer than 4 words
|
// Only worry about commas on sentences longer than 4 words
|
||||||
if ($words > 4)
|
if ($words > 4)
|
||||||
{
|
{
|
||||||
$mean = log($words, 6);
|
$mean = log($words, 6);
|
||||||
$std_dev = $mean / 6;
|
$std_dev = $mean / 6;
|
||||||
$commas = round($this->gauss($mean, $std_dev));
|
$commas = round($this->gauss($mean, $std_dev));
|
||||||
|
|
||||||
for ($i = 1; $i <= $commas; $i++)
|
for ($i = 1; $i <= $commas; $i++)
|
||||||
{
|
{
|
||||||
$word = round($i * $words / ($commas + 1));
|
$word = round($i * $words / ($commas + 1));
|
||||||
|
|
||||||
if ($word < ($words - 1) && $word > 0)
|
if ($word < ($words - 1) && $word > 0)
|
||||||
{
|
{
|
||||||
$sentence[$word] .= ',';
|
$sentence[$word] .= ',';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$sentences[$key] = ucfirst(implode(' ', $sentence) . '.');
|
$sentences[$key] = ucfirst(implode(' ', $sentence) . '.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output
|
* Output
|
||||||
*
|
*
|
||||||
* Does the rest of the processing of the strings. This includes wrapping
|
* Does the rest of the processing of the strings. This includes wrapping
|
||||||
* the strings in HTML tags, handling transformations with the ability of
|
* the strings in HTML tags, handling transformations with the ability of
|
||||||
* back referencing and determining if the passed array should be converted
|
* back referencing and determining if the passed array should be converted
|
||||||
* into a string or not.
|
* into a string or not.
|
||||||
*
|
*
|
||||||
* @access private
|
* @access private
|
||||||
* @param array $strings an array of generated strings
|
* @param array $strings an array of generated strings
|
||||||
* @param mixed $tags string or array of HTML tags to wrap output with
|
* @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 boolean $array whether an array or a string should be returned
|
||||||
* @param string $delimiter the string to use when calling implode()
|
* @param string $delimiter the string to use when calling implode()
|
||||||
* @return mixed string or array of generated lorem ipsum text
|
* @return mixed string or array of generated lorem ipsum text
|
||||||
*/
|
*/
|
||||||
private function output($strings, $tags, $array, $delimiter = ' ')
|
private function output($strings, $tags, $array, $delimiter = ' ')
|
||||||
{
|
{
|
||||||
if ($tags)
|
if ($tags)
|
||||||
{
|
{
|
||||||
if (!is_array($tags))
|
if (!is_array($tags))
|
||||||
{
|
{
|
||||||
$tags = array($tags);
|
$tags = array($tags);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Flips the array so we can work from the inside out
|
// Flips the array so we can work from the inside out
|
||||||
$tags = array_reverse($tags);
|
$tags = array_reverse($tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($strings as $key => $string)
|
foreach ($strings as $key => $string)
|
||||||
{
|
{
|
||||||
foreach ($tags as $tag)
|
foreach ($tags as $tag)
|
||||||
{
|
{
|
||||||
// Detects / applies back reference
|
// Detects / applies back reference
|
||||||
if ($tag[0] == '<')
|
if ($tag[0] == '<')
|
||||||
{
|
{
|
||||||
$string = str_replace('$1', $string, $tag);
|
$string = str_replace('$1', $string, $tag);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
|
$string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
|
||||||
}
|
}
|
||||||
|
|
||||||
$strings[$key] = $string;
|
$strings[$key] = $string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$array)
|
if (!$array)
|
||||||
{
|
{
|
||||||
$strings = implode($delimiter, $strings);
|
$strings = implode($delimiter, $strings);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $strings;
|
return $strings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
|
@ -4,125 +4,124 @@ require_once '../src/LoremIpsum.php';
|
||||||
|
|
||||||
class LoremIpsumTest extends PHPUnit_Framework_TestCase
|
class LoremIpsumTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $lipsum;
|
private $lipsum;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
$this->lipsum = new joshtronic\LoremIpsum();
|
$this->lipsum = new joshtronic\LoremIpsum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWord()
|
public function testWord()
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^[a-z]+$/i', $this->lipsum->word());
|
$this->assertRegExp('/^[a-z]+$/i', $this->lipsum->word());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWords()
|
public function testWords()
|
||||||
{
|
{
|
||||||
$this->assertRegExp(
|
$this->assertRegExp(
|
||||||
'/^[a-z]+ [a-z]+ [a-z]+$/i',
|
'/^[a-z]+ [a-z]+ [a-z]+$/i',
|
||||||
$this->lipsum->words(3)
|
$this->lipsum->words(3)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWordsArray()
|
public function testWordsArray()
|
||||||
{
|
{
|
||||||
$words = $this->lipsum->wordsArray(3);
|
$words = $this->lipsum->wordsArray(3);
|
||||||
$this->assertTrue(is_array($words));
|
$this->assertTrue(is_array($words));
|
||||||
$this->assertCount(3, $words);
|
$this->assertCount(3, $words);
|
||||||
|
|
||||||
foreach ($words as $word)
|
foreach ($words as $word)
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^[a-z]+$/i', $word);
|
$this->assertRegExp('/^[a-z]+$/i', $word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWordsExceedingVocab()
|
public function testWordsExceedingVocab()
|
||||||
{
|
{
|
||||||
$this->assertCount(500, $this->lipsum->wordsArray(500));
|
$this->assertCount(500, $this->lipsum->wordsArray(500));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSentence()
|
public function testSentence()
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^[a-z, ]+\.$/i', $this->lipsum->sentence());
|
$this->assertRegExp('/^[a-z, ]+\.$/i', $this->lipsum->sentence());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSentences()
|
public function testSentences()
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^[a-z, ]+\. [a-z, ]+\. [a-z, ]+\.$/i', $this->lipsum->sentences(3));
|
$this->assertRegExp('/^[a-z, ]+\. [a-z, ]+\. [a-z, ]+\.$/i', $this->lipsum->sentences(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSentencesArray()
|
public function testSentencesArray()
|
||||||
{
|
{
|
||||||
$sentences = $this->lipsum->sentencesArray(3);
|
$sentences = $this->lipsum->sentencesArray(3);
|
||||||
$this->assertTrue(is_array($sentences));
|
$this->assertTrue(is_array($sentences));
|
||||||
$this->assertCount(3, $sentences);
|
$this->assertCount(3, $sentences);
|
||||||
|
|
||||||
foreach ($sentences as $sentence)
|
foreach ($sentences as $sentence)
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^[a-z, ]+\.$/i', $sentence);
|
$this->assertRegExp('/^[a-z, ]+\.$/i', $sentence);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testParagraph()
|
public function testParagraph()
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^([a-z, ]+\.)+$/i', $this->lipsum->paragraph());
|
$this->assertRegExp('/^([a-z, ]+\.)+$/i', $this->lipsum->paragraph());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testParagraphs()
|
public function testParagraphs()
|
||||||
{
|
{
|
||||||
$this->assertRegExp(
|
$this->assertRegExp(
|
||||||
'/^([a-z, ]+\.)+\n\n([a-z, ]+\.)+\n\n([a-z, ]+\.)+$/i',
|
'/^([a-z, ]+\.)+\n\n([a-z, ]+\.)+\n\n([a-z, ]+\.)+$/i',
|
||||||
$this->lipsum->paragraphs(3)
|
$this->lipsum->paragraphs(3)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testParagraphsArray()
|
public function testParagraphsArray()
|
||||||
{
|
{
|
||||||
$paragraphs = $this->lipsum->paragraphsArray(3);
|
$paragraphs = $this->lipsum->paragraphsArray(3);
|
||||||
$this->assertTrue(is_array($paragraphs));
|
$this->assertTrue(is_array($paragraphs));
|
||||||
$this->assertCount(3, $paragraphs);
|
$this->assertCount(3, $paragraphs);
|
||||||
|
|
||||||
foreach ($paragraphs as $paragraph)
|
foreach ($paragraphs as $paragraph)
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^([a-z, ]+\.)+$/i', $paragraph);
|
$this->assertRegExp('/^([a-z, ]+\.)+$/i', $paragraph);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMarkupString()
|
public function testMarkupString()
|
||||||
{
|
{
|
||||||
$this->assertRegExp(
|
$this->assertRegExp(
|
||||||
'/^<li>[a-z]+<\/li>$/i',
|
'/^<li>[a-z]+<\/li>$/i',
|
||||||
$this->lipsum->word('li')
|
$this->lipsum->word('li')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMarkupArray()
|
public function testMarkupArray()
|
||||||
{
|
{
|
||||||
$this->assertRegExp(
|
$this->assertRegExp(
|
||||||
'/^<div><p>[a-z]+<\/p><\/div>$/i',
|
'/^<div><p>[a-z]+<\/p><\/div>$/i',
|
||||||
$this->lipsum->word(array('div', 'p'))
|
$this->lipsum->word(array('div', 'p'))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMarkupBackReference()
|
public function testMarkupBackReference()
|
||||||
{
|
{
|
||||||
$this->assertRegExp(
|
$this->assertRegExp(
|
||||||
'/^<li><a href="[a-z]+">[a-z]+<\/a><\/li>$/i',
|
'/^<li><a href="[a-z]+">[a-z]+<\/a><\/li>$/i',
|
||||||
$this->lipsum->word('<li><a href="$1">$1</a></li>')
|
$this->lipsum->word('<li><a href="$1">$1</a></li>')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMarkupArrayReturn()
|
public function testMarkupArrayReturn()
|
||||||
{
|
{
|
||||||
$words = $this->lipsum->wordsArray(3, 'li');
|
$words = $this->lipsum->wordsArray(3, 'li');
|
||||||
$this->assertTrue(is_array($words));
|
$this->assertTrue(is_array($words));
|
||||||
$this->assertCount(3, $words);
|
$this->assertCount(3, $words);
|
||||||
|
|
||||||
foreach ($words as $word)
|
foreach ($words as $word)
|
||||||
{
|
{
|
||||||
$this->assertRegExp('/^<li>[a-z]+<\/li>$/i', $word);
|
$this->assertRegExp('/^<li>[a-z]+<\/li>$/i', $word);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue