Skip to content

Commit

Permalink
create factory modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
browner12 committed Jun 28, 2016
1 parent 01c9e08 commit 1a4c410
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
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

0 comments on commit 1a4c410

Please sign in to comment.