Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Oct 6, 2016
1 parent ad1f68d commit 520c601
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
14 changes: 7 additions & 7 deletions Eloquent/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public function __construct(Faker $faker)
protected $definitions = [];

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

/**
* Create a new factory container.
Expand Down Expand Up @@ -81,16 +81,16 @@ public function define($class, callable $attributes, $name = 'default')
}

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

/**
Expand Down Expand Up @@ -199,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, $this->modifiers);
return new FactoryBuilder($class, $name, $this->definitions, $this->faker, $this->states);
}

/**
Expand Down
74 changes: 39 additions & 35 deletions Eloquent/FactoryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ class FactoryBuilder
*/
protected $definitions;

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

/**
* The model being built.
*
Expand All @@ -44,11 +37,18 @@ class FactoryBuilder
protected $amount = 1;

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

/**
* The states to apply.
*
* @var array
*/
protected $activeStates = [];

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

/**
Expand All @@ -90,18 +90,14 @@ public function times($amount)
}

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

$this->activeModifiers = $modifiers;
$this->activeStates = is_array($states) ? $states : func_get_args();

return $this;
}
Expand Down Expand Up @@ -168,25 +164,33 @@ 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
);
return new $this->class($this->callClosureAttributes(
array_merge($this->applyStates($definition, $attributes), $attributes)
));
});
}

$definition = array_merge($definition, $modifier);
/**
* Apply the active states to the model definition array.
*
* @param array $definition
* @param array $attributes
* @return array
*/
protected function applyStates(array $definition, array $attributes = [])
{
foreach ($this->activeStates as $state) {
if (! isset($this->states[$this->class][$state])) {
throw new InvalidArgumentException("Unable to locate [{$state}] state for [{$this->class}].");
}

$evaluated = $this->callClosureAttributes(
array_merge($definition, $attributes)
);
$definition = array_merge($definition, call_user_func(
$this->states[$this->class][$state],
$this->faker, $attributes
));
}

return new $this->class($evaluated);
});
return $definition;
}

/**
Expand Down

0 comments on commit 520c601

Please sign in to comment.