Skip to content

Commit

Permalink
Factory making improved
Browse files Browse the repository at this point in the history
  • Loading branch information
digitaldreams committed Dec 31, 2018
1 parent 12d5634 commit 7fb5699
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/lara-crud/Console/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function handle()
if (class_exists($modelFullName)) {
$modelObj = new $modelFullName;
$requestResource = new RequestResourceCrud($modelObj->getTable(), false, $api);
$requestResource->setModel($modelFullName);
$requestResource->save();
$this->info('Request controller classes created successfully');
}
Expand Down
3 changes: 2 additions & 1 deletion src/lara-crud/Console/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace LaraCrud\Console;

use Illuminate\Console\Command;
use LaraCrud\Crud\ModelFactory;

class Factory extends Command
{
Expand Down Expand Up @@ -31,7 +32,7 @@ public function handle()
$model = $this->argument('model');
$name = $this->option('name');

$factoryCrud = new \LaraCrud\Crud\ModelFactory($model, $name);
$factoryCrud = new ModelFactory($model, $name);
$factoryCrud->save();
$this->info('Factory class created successfully');
} catch (\Exception $ex) {
Expand Down
6 changes: 5 additions & 1 deletion src/lara-crud/Crud/ModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DbReader\Table;
use Illuminate\Database\Eloquent\Model;
use LaraCrud\Contracts\Crud;
use LaraCrud\Helpers\FakerColumn;
use LaraCrud\Helpers\Helper;
use LaraCrud\Helpers\TemplateManager;

Expand Down Expand Up @@ -85,7 +86,10 @@ protected function makeColumns()
if ($column->isProtected()) {
continue;
}
$arr .= "\t\t" . '"' . $column->name() . '"=>\'\',' . PHP_EOL;
$fakerColumn = new FakerColumn($column);
$default = $fakerColumn->default();
$columnValue = !empty($default) ? $default.',' : '\'\',';
$arr .= "\t\t" . '"' . $column->name() . '" => ' . $columnValue . PHP_EOL;
};
return $arr;
}
Expand Down
109 changes: 97 additions & 12 deletions src/lara-crud/Helpers/FakerColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,82 @@

class FakerColumn
{
protected $map = [];
protected $map = [
'firstName' => [
'first_name',
'first_name',
'fname'
],
'lastName' => [
'last_name',
'lastname',
'lname'
],
'name' => [
'full_name',
'name'
],
'safeEmail' => [
'email',
'email_address',
'emailaddress'
],
'address' => [
'address',
'street_address',
'street',
],
'city' => [
'city',
'suburb',
'locality',
'state',
'village',
'town'
],
'country' => [
'country',
],
'realText()' => [
'title',
'subject',
'message',
'reply',
'comments',
'comment',
'feedback',
'body',
'content',
'description',
'about',
'profile'
],
'slug' => [
'slug'
],
'phoneNumber' => [
'phone',
'phone_number',
'mobile',
'cell',
'mobile_number',
'cell_number',
'telephone',
'personal_number',
'business_number',
'emergency_cell',
'emergency_phone',
],
'imageUrl()' => [
'avatar',
'photo',
'image',
'image_url',
'document',
'file'
]

];
/**
* @var Column
*/
Expand All @@ -29,47 +104,57 @@ public function get()

}

protected function default()
public function default()
{
switch ($this->column->type()) {
case 'varchar':
foreach ($this->map as $faker => $columns) {
if (in_array($this->column->name(), $columns)) {
return '$faker->' . $faker;
}
}
break;
case 'enum':
return 'array_rand([\'' . implode("','", $this->column->options()) . '\'], 1)';
break;
case 'longText':
case 'longtext':
case 'mediumtext':
case 'text':
case 'tinytext':
if (in_array($this->column->name(), $this->map['realText()'])) {
return '$faker->realText()';
} else {
return '$faker->text';
}
break;
// Numeric data Type
case 'bigint':
break;
case 'mediumint':
break;
case 'int':
return '$faker->randomNumber()';
break;
case 'smallint':
break;
case 'tinyint':
return '$faker->numberBetween(1,99)';
break;
case 'decimal':
break;
case 'float':
break;
case 'double':
return '$faker->randomNumber()';
break;
// Date Time
case 'date':
return '$faker->date()';
break;
case 'datetime':
case 'timestamp':
return '$faker->dateTime()';
break;
case 'time':
return '$faker->time()';
case 'year':
return '$faker->year';
break;
break;
case 'timestamp':
break;

}
}
}
1 change: 1 addition & 0 deletions src/lara-crud/LaraCrudServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DbReader\Database;
use Illuminate\Support\ServiceProvider;
use LaraCrud\Console\Controller;
use LaraCrud\Console\ControllerMethod;
use LaraCrud\Console\Factory;
use LaraCrud\Console\Migration;
use LaraCrud\Console\Model;
Expand Down

0 comments on commit 7fb5699

Please sign in to comment.