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)
{
$start = 8;
$count -= 8;
}
else
{
$start = $count;
$count = 0;
}
$words = array_slice($this->words, 0, $start); return $this->output($words, $tags, $array);
$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 sentence($tags = false)
{ {
return $this->sentences(1, $tags);
}
public function sentencesArray($count = 1, $tags = false)
{
return $this->sentences($count, $tags, true);
} }
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,38 +110,62 @@ class LoremIpsum
} }
public function paragraphsArray() private function shuffle()
{ {
if ($this->first)
}
private function markup(&$strings, $tags)
{
if (!is_array($tags))
{ {
$tags = array($tags); $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 else
{ {
$tags = array_reverse($tags); shuffle($this->words);
} }
}
foreach ($strings as $key => $string) private function output($strings, $tags, $array)
{
if ($tags)
{ {
foreach ($tags as $tag) if (!is_array($tags))
{ {
if ($tag[0] == '<') $tags = array($tags);
{ }
$string = str_replace('$1', $string, $tag); else
} {
else $tags = array_reverse($tags);
{ }
$string = sprintf('<%1$s>%2$s</%1$s>', $tag, $string);
}
$strings[$key] = $string; 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;
}
} }
} }
if (!$array)
{
$strings = implode(' ', $strings);
}
return $strings;
} }
} }