Refactored / consolidated some logic

Wanted to get the markup / output formatting wrapped up nice so it can be
used for the sentence and paragraph generation
This commit is contained in:
Joshua Sherman 2014-05-13 18:00:23 -04:00
parent 5cdec19a4d
commit 6239233483

View file

@ -73,47 +73,21 @@ class LoremIpsum
public function words($count = 1, $tags = false, $array = false)
{
$words = array();
$this->shuffle();
if ($this->first)
{
if ($count > 8)
{
$start = 8;
$count -= 8;
}
else
{
$start = $count;
$count = 0;
$words = array_slice($this->words, 0, $count);
return $this->output($words, $tags, $array);
}
$words = array_slice($this->words, 0, $start);
$this->first = false;
}
if ($count)
public function sentence($tags = false)
{
shuffle($this->words);
$words += array_slice($this->words, 0, $count);
return $this->sentences(1, $tags);
}
if ($tags)
public function sentencesArray($count = 1, $tags = false)
{
$this->markup($words, $tags);
}
if (!$array)
{
$words = implode(' ', $words);
}
return $words;
}
public function sentence()
{
return $this->sentences($count, $tags, true);
}
public function sentences()
@ -121,14 +95,14 @@ class LoremIpsum
}
public function sentencesArray()
public function paragraph($tags = false)
{
return $this->paragraphs(1, $tags);
}
public function paragraph()
public function paragraphsArray($count = 1, $tags = false)
{
return $this->paragraphs($count, $tags, true);
}
public function paragraphs()
@ -136,12 +110,28 @@ class LoremIpsum
}
public function paragraphsArray()
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);
}
}
private function markup(&$strings, $tags)
private function output($strings, $tags, $array)
{
if ($tags)
{
if (!is_array($tags))
{
@ -169,6 +159,14 @@ class LoremIpsum
}
}
}
if (!$array)
{
$strings = implode(' ', $strings);
}
return $strings;
}
}
?>