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) public function words($count = 1, $tags = false, $array = false)
{ {
$words = array(); $this->shuffle();
if ($this->first) $words = array_slice($this->words, 0, $count);
{
if ($count > 8) return $this->output($words, $tags, $array);
{
$start = 8;
$count -= 8;
}
else
{
$start = $count;
$count = 0;
} }
$words = array_slice($this->words, 0, $start); public function sentence($tags = false)
$this->first = false;
}
if ($count)
{ {
shuffle($this->words); return $this->sentences(1, $tags);
$words += array_slice($this->words, 0, $count);
} }
if ($tags) public function sentencesArray($count = 1, $tags = false)
{ {
$this->markup($words, $tags); return $this->sentences($count, $tags, true);
}
if (!$array)
{
$words = implode(' ', $words);
}
return $words;
}
public function sentence()
{
} }
public function sentences() 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() 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)) if (!is_array($tags))
{ {
@ -169,6 +159,14 @@ class LoremIpsum
} }
} }
} }
if (!$array)
{
$strings = implode(' ', $strings);
}
return $strings;
}
} }
?> ?>