Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Arrays to be passed as parameter when needed #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 27 additions & 18 deletions src/PatternLab/Faker/PatternLabListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ public function fakeContent() {
*/
public function formatOptionsAndFake($formatter, $options) {

if (($formatter == "date") || ($formatter == "time")) {

if (in_array($formatter, ["randomElements", "randomElement", "shuffle", "date", "time"])) {
// don't try to parse date or time options. cross our fingers
// Also if the
if ($formatter == "date" || $formatter == "time") {
return $this->faker->$formatter($options);
}
eval('$options = ' . $options . ';');
return $this->faker->$formatter($options);

} else {

// get explodey
$options = explode(",", $options);
$count = count($options);
Expand All @@ -119,25 +121,32 @@ public function formatOptionsAndFake($formatter, $options) {
$option0 = $this->clean($options[0]);
$option1 = isset($options[1]) ? $this->clean($options[1]) : "";
$option2 = isset($options[2]) ? $this->clean($options[2]) : "";
$option3 = isset($options[3]) ? $this->clean($options[3]) : "";
$option3 = isset($options[3]) ? $this->clean($options[3]) : "";
$option4 = isset($options[4]) ? $this->clean($options[4]) : "";
$option5 = isset($options[5]) ? $this->clean($options[5]) : "";
$option6 = isset($options[6]) ? $this->clean($options[6]) : "";

// probably should have used a switch. i'm lazy
try {
if ($count === 6) {
return $this->faker->$formatter($option0,$option1,$option2,$option3,$option4,$option5);
} else if ($count === 5) {
return $this->faker->$formatter($option0,$option1,$option2,$option3,$option4);
} else if ($count === 4) {
return $this->faker->$formatter($option0,$option1,$option2,$option3);
} else if ($count === 3) {
return $this->faker->$formatter($option0,$option1,$option2);
} else if ($count === 2) {
return $this->faker->$formatter($option0,$option1);
} else {
return $this->faker->$formatter($option0);
switch ($count) {
case 6:
return $this->faker->$formatter($option0,$option1,$option2,$option3,$option4,$option5);
break;
case 5:
return $this->faker->$formatter($option0,$option1,$option2,$option3,$option4);
break;
case 4:
return $this->faker->$formatter($option0,$option1,$option2,$option3);
break;
case 3:
return $this->faker->$formatter($option0,$option1,$option2);
break;
case 2:
return $this->faker->$formatter($option0,$option1);
break;

default:
return $this->faker->$formatter($option0);
break;
}
} catch (\InvalidArgumentException $e) {
Console::writeWarning("Faker plugin error: ".$e->getMessage()."...");
Expand Down