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

[5.3] Create factory modifiers #14241

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
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
22 changes: 21 additions & 1 deletion src/Illuminate/Database/Eloquent/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function __construct(Faker $faker)
*/
protected $definitions = [];

/**
* The model modifiers in the container.
*
* @var array
*/
protected $modifiers = [];

/**
* Create a new factory container.
*
Expand Down Expand Up @@ -73,6 +80,19 @@ public function define($class, callable $attributes, $name = 'default')
$this->definitions[$class][$name] = $attributes;
}

/**
* Define a modifier with a given set of attributes.
*
* @param string $class
* @param string $name
* @param callable $attributes
* @return void
*/
public function defineModifier($class, $name, callable $attributes)
{
$this->modifiers[$class][$name] = $attributes;
}

/**
* Create an instance of the given model and persist it to the database.
*
Expand Down Expand Up @@ -179,7 +199,7 @@ public function raw($class, array $attributes = [], $name = 'default')
*/
public function of($class, $name = 'default')
{
return new FactoryBuilder($class, $name, $this->definitions, $this->faker);
return new FactoryBuilder($class, $name, $this->definitions, $this->faker, $this->modifiers);
}

/**
Expand Down
48 changes: 47 additions & 1 deletion src/Illuminate/Database/Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class FactoryBuilder
*/
protected $definitions;

/**
* The model modifiers in the container.
*
* @var array
*/
protected $modifiers;

/**
* The model being built.
*
Expand All @@ -36,6 +43,13 @@ class FactoryBuilder
*/
protected $amount = 1;

/**
* The modifiers to apply.
*
* @var array
*/
protected $activeModifiers = [];

/**
* The Faker instance for the builder.
*
Expand All @@ -50,14 +64,16 @@ class FactoryBuilder
* @param string $name
* @param array $definitions
* @param \Faker\Generator $faker
* @param array $modifiers
* @return void
*/
public function __construct($class, $name, array $definitions, Faker $faker)
public function __construct($class, $name, array $definitions, Faker $faker, array $modifiers)
{
$this->name = $name;
$this->class = $class;
$this->faker = $faker;
$this->definitions = $definitions;
$this->modifiers = $modifiers;
}

/**
Expand All @@ -73,6 +89,23 @@ public function times($amount)
return $this;
}

/**
* Set the active modifiers.
*
* @param array|string $modifiers
* @return $this
*/
public function modifiers($modifiers)
{
if(is_string($modifiers)){
$modifiers = [$modifiers];
}

$this->activeModifiers = $modifiers;

return $this;
}

/**
* Create a collection of models and persist them to the database.
*
Expand Down Expand Up @@ -135,6 +168,19 @@ protected function makeInstance(array $attributes = [])
$this->faker, $attributes
);

foreach($this->activeModifiers as $activeModifier){
if( ! isset($this->modifiers[$this->class][$activeModifier])) {
throw new InvalidArgumentException("Unable to locate factory modifier with name [{$activeModifier}] [{$this->class}].");
}

$modifier = call_user_func(
$this->modifiers[$this->class][$activeModifier],
$this->faker, $attributes
);

$definition = array_merge($definition, $modifier);
}

$evaluated = $this->callClosureAttributes(
array_merge($definition, $attributes)
);
Expand Down