Skip to content

Commit

Permalink
Merge pull request #49 from philsturgeon/psr2
Browse files Browse the repository at this point in the history
Improved Faker default + PSR2
  • Loading branch information
scottrobertson committed Jul 4, 2014
2 parents 8a2a0ff + 1d2fbf4 commit d567c32
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 255 deletions.
12 changes: 5 additions & 7 deletions src/Zizaco/FactoryMuff/Facade/FactoryMuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ class FactoryMuff
* @static
*/
private static $fmInstance;


/**
* Get or stance FactoryMuff obj
*
*
* @access private
* @static
*
* @return Zizaco\FactoryMuff\FactoryMuff
*/
private static function fmInstance()
{
if (! static::$fmInstance)
{
if (! static::$fmInstance) {
static::$fmInstance = new \Zizaco\FactoryMuff\FactoryMuff;
}

Expand All @@ -40,7 +38,7 @@ private static function fmInstance()
/**
* Creates and saves in db an instance
* of Model with mock attributes
*
*
* @param string $model Model class name.
* @param array $attr Model attributes.
*
Expand All @@ -57,7 +55,7 @@ public static function create( $model, $attr = array() )
/**
* Return an instance of the model, which is
* not saved in the database
*
*
* @param string $model Model class name.
* @param array $attr Model attributes.
*
Expand All @@ -73,7 +71,7 @@ public static function instance( $model, $attr = array() )
/**
* Returns an array of mock attributes
* for the especified model
*
*
* @param string $model Model class name.
* @param array $attr Model attributes.
*
Expand Down
62 changes: 21 additions & 41 deletions src/Zizaco/FactoryMuff/FactoryMuff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Zizaco\FactoryMuff;

use Zizaco\FactoryMuff\Kind;

/**
* Creates models with random attributes
*
Expand All @@ -14,13 +12,10 @@
*/
class FactoryMuff
{

/**
* $factories
*
* @var array
*
* @access private
*/
private $factories = array();

Expand All @@ -31,23 +26,19 @@ class FactoryMuff
* @param string $model Model class name.
* @param array $attr Model attributes.
*
* @access public
*
* @return mixed Returns the model instance.
*/
public function create( $model, $attr = array() )
public function create($model, $attr = array())
{
$obj = $this->instance( $model, $attr );
$obj = $this->instance($model, $attr);

$result = $obj->save();
if ( !$result ) {
if (!$result) {

$message = '';

if(isset($obj->validationErrors))
{
if($obj->validationErrors)
{
if (isset($obj->validationErrors)) {
if ($obj->validationErrors) {
$message = $obj->validationErrors.' - ';
}
}
Expand All @@ -65,14 +56,12 @@ public function create( $model, $attr = array() )
* @param string $model Model class name.
* @param array $attr Model attributes.
*
* @access public
*
* @return mixed Returns the model instance.
*/
public function instance( $model, $attr = array() )
public function instance($model, $attr = array())
{
// Get the factory attributes for that model
$attr_array = $this->attributesFor( $model, $attr );
$attr_array = $this->attributesFor($model, $attr);

// Create, set, save and return instance
$obj = new $model();
Expand All @@ -91,18 +80,16 @@ public function instance( $model, $attr = array() )
* @param string $model Model class name.
* @param array $attr Model attributes.
*
* @access public
*
* @return array Returns an attributes array.
*/
public function attributesFor( $model, $attr = array() )
public function attributesFor($model, $attr = array())
{
$factory_attrs = $this->getFactoryAttrs($model);

// Prepare attributes
foreach ( $factory_attrs as $key => $kind ) {
if ( ! isset($attr[$key]) ){
$attr[$key] = $this->generateAttr( $kind, $model );
foreach ($factory_attrs as $key => $kind) {
if (! isset($attr[$key])) {
$attr[$key] = $this->generateAttr($kind, $model);
}
}

Expand All @@ -112,10 +99,8 @@ public function attributesFor( $model, $attr = array() )
/**
* Define a new model factory
*
* @param string $model Model class name.
* @param array $definition Array with definition of attributes.
*
* @access public
* @param string $model Model class name.
* @param array $definition Array with definition of attributes.
*
* @return void
*/
Expand All @@ -129,25 +114,21 @@ public function define($model, array $definition = array())
*
* @param string $model Model class name.
*
* @access private
*
* @return array Returns an factory definition array.
*/
private function getFactoryAttrs($model)
{
if(isset($this->factories[$model])) {
if (isset($this->factories[$model])) {
return $this->factories[$model];
}

if(method_exists($model, 'factory'))
{
if (method_exists($model, 'factory')) {
return $model::factory();
}
else {
} else {
// Get the $factory static and check for errors
$static_vars = get_class_vars( $model );
$static_vars = get_class_vars($model);

if (isset( $static_vars['factory'] ) ) {
if (isset($static_vars['factory'])) {
return $static_vars['factory'];
}
}
Expand All @@ -158,16 +139,15 @@ private function getFactoryAttrs($model)
/**
* Generate an attribute based in the wordlist
*
* @param string $kind The kind of attribute that will be generate.
* @param string $kind The kind of attribute that will be generate.
* @param string $model The name of the model class
*
* @access private
*
* @return mixed String or an instance of related model.
*/
public function generateAttr( $kind, $model = NULL )
public function generateAttr($kind, $model = NULL)
{
$kind = Kind::detect($kind, $model);

return $kind->generate();
}
}
Loading

0 comments on commit d567c32

Please sign in to comment.