diff --git a/formats/rules.json b/formats/rules.json new file mode 100644 index 0000000..099bb8b --- /dev/null +++ b/formats/rules.json @@ -0,0 +1,9 @@ +{ + "type": "array", + "separator": " ", + "fields": { + "type": "object", + "separator": "=", + "fields": ["name","rule"] + } +} \ No newline at end of file diff --git a/lumen-test/app/Test.php b/lumen-test/app/Test.php deleted file mode 100644 index 37316e3..0000000 --- a/lumen-test/app/Test.php +++ /dev/null @@ -1,21 +0,0 @@ -hasMany('App\User'); - } - - public function number() - { - return $this->hasMany('Phone'); - } - -} diff --git a/lumen-test/composer.lock b/lumen-test/composer.lock index c140d50..419e454 100644 --- a/lumen-test/composer.lock +++ b/lumen-test/composer.lock @@ -3699,12 +3699,12 @@ "source": { "type": "git", "url": "https://github.com/webNeat/lumen-generators.git", - "reference": "04ce3077cd2723b38424e1ea94844582b67c6b79" + "reference": "4a60029604f1b12d81e135761dc255fe2f500b04" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/04ce3077cd2723b38424e1ea94844582b67c6b79", - "reference": "04ce3077cd2723b38424e1ea94844582b67c6b79", + "url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/4a60029604f1b12d81e135761dc255fe2f500b04", + "reference": "4a60029604f1b12d81e135761dc255fe2f500b04", "shasum": "" }, "require": { @@ -3736,7 +3736,7 @@ "lumen", "rest" ], - "time": "2015-09-23 03:58:39" + "time": "2015-09-23 04:38:56" } ], "aliases": [], diff --git a/lumen-test/tests/acceptance/ModelCommandCept.php b/lumen-test/tests/acceptance/ModelCommandCept.php index 4a2539f..04795d8 100644 --- a/lumen-test/tests/acceptance/ModelCommandCept.php +++ b/lumen-test/tests/acceptance/ModelCommandCept.php @@ -6,6 +6,7 @@ $I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp'); $I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); + $I->seeFileContentsEqual('runShellCommand('php artisan wn:model TestingModel --fillable=name,title --p $I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); -$I->seeFileContentsEqual('seeInThisFile('protected $fillable = ["name", "title"];'); $I->wantTo('generate a model with dates fields'); $I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp'); $I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); -$I->seeFileContentsEqual('seeInThisFile('protected $dates = ["started_at"];'); $I->wantTo('generate a model with relations'); -$I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts,friends:App\User,numbers:Phone --path=tests/tmp'); +$I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts --belongs-to="owner:App\User" --has-one=number:Phone --path=tests/tmp'); $I->seeInShellOutput('Model TestingModel Generated'); $I->seeFileFound('./tests/tmp/TestingModel.php'); $I->openFile('./tests/tmp/TestingModel.php'); -$I->seeFileContentsEqual('seeInThisFile(' + public function accounts() + { return $this->hasMany("Tests\\Tmp\\Account"); } - - public function friends(){ - return $this->hasMany("App\\User"); +'); +$I->seeInThisFile(' + public function owner() + { + return $this->belongsTo("App\\User"); } - - public function numbers(){ - return $this->hasMany("Tests\\Tmp\\Phone"); +'); +$I->seeInThisFile(' + public function number() + { + return $this->hasOne("Tests\\Tmp\\Phone"); } +'); -} -'); \ No newline at end of file +$I->wantTo('generate a model with validation rules'); +$I->runShellCommand('php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address" --path=tests/tmp'); +$I->seeInShellOutput('Model TestingModel Generated'); +$I->seeFileFound('./tests/tmp/TestingModel.php'); +$I->openFile('./tests/tmp/TestingModel.php'); +$I->seeInThisFile(' + public $rules = [ + "name" => "required", + "age" => "integer|min:13", + "email" => "email|unique:users,email_address", + ]; +'); diff --git a/lumen-test/tests/tmp/TestingModel.php b/lumen-test/tests/tmp/TestingModel.php index ef2d831..a1cabee 100644 --- a/lumen-test/tests/tmp/TestingModel.php +++ b/lumen-test/tests/tmp/TestingModel.php @@ -8,6 +8,12 @@ class TestingModel extends Model { protected $dates = []; + public $rules = [ + "name" => "required", + "age" => "integer|min:13", + "email" => "email|unique:users,email_address", + ]; + + // Relationships - } diff --git a/src/Commands/ModelCommand.php b/src/Commands/ModelCommand.php index f919acd..82703dc 100644 --- a/src/Commands/ModelCommand.php +++ b/src/Commands/ModelCommand.php @@ -4,11 +4,14 @@ class ModelCommand extends BaseCommand { protected $signature = 'wn:model - {name : Name of the model} - {--fillable= : the fillable fields of the model} - {--dates= : date fields of the model} - {--has-many= : on-to-many relationships of the model} - {--path=app : where to store the model php file}'; + {name : Name of the model.} + {--fillable= : the fillable fields.} + {--dates= : date fields.} + {--has-many= : hasMany relationships.} + {--has-one= : hasOne relationships.} + {--belongs-to= : belongsTo relationships.} + {--rules= : fields validation rules.} + {--path=app : where to store the model php file.}'; protected $description = 'Generates a model class for a RESTfull resource'; @@ -25,7 +28,8 @@ class ModelCommand extends BaseCommand { 'namespace' => $this->getNamespace(), 'fillable' => $this->getAsArrayFields('fillable'), 'dates' => $this->getAsArrayFields('dates'), - 'relations' => $this->getRelations() + 'relations' => $this->getRelations(), + 'rules' => $this->getRules() ]) ->get(); @@ -55,10 +59,16 @@ class ModelCommand extends BaseCommand { protected function getRelations() { $relations = array_merge([], - $this->getRelationsByType('hasMany', 'has-many') + $this->getRelationsByType('hasOne', 'has-one'), + $this->getRelationsByType('hasMany', 'has-many'), + $this->getRelationsByType('belongsTo', 'belongs-to') ); - return implode("\n\n", $relations); + if(empty($relations)){ + return "\t// Relationships"; + } + + return implode(PHP_EOL, $relations); } protected function getRelationsByType($type, $option) @@ -81,4 +91,22 @@ class ModelCommand extends BaseCommand { } return $relations; } + + protected function getRules() + { + $rules = $this->option('rules'); + if(! $rules){ + return "\t\t// Validation rules"; + } + $parser = $this->getArgumentParser('rules'); + $template = $this->getTemplate('model/rule'); + $items = $parser->parse($rules); + $rules = []; + foreach ($items as $item) { + $rules[] = $template->with($item)->get(); + } + + return implode(PHP_EOL, $rules); + } + } \ No newline at end of file diff --git a/templates/model.wnt b/templates/model.wnt index 54911f3..0d1dc43 100644 --- a/templates/model.wnt +++ b/templates/model.wnt @@ -8,6 +8,10 @@ class {{name}} extends Model { protected $dates = [{{dates}}]; + public $rules = [ +{{rules}} + ]; + {{relations}} - + } diff --git a/templates/model/relation.wnt b/templates/model/relation.wnt index 4343c9d..e9bef2a 100644 --- a/templates/model/relation.wnt +++ b/templates/model/relation.wnt @@ -1,4 +1,4 @@ public function {{name}}() { - return $this->{{type}}('{{model}}'); - } \ No newline at end of file + return $this->{{type}}("{{model}}"); + } diff --git a/templates/model/rule.wnt b/templates/model/rule.wnt new file mode 100644 index 0000000..99d0ea2 --- /dev/null +++ b/templates/model/rule.wnt @@ -0,0 +1 @@ + "{{name}}" => "{{rule}}", \ No newline at end of file