Skip to content

Commit bb25570

Browse files
committed
#47 validations now in validateInput, getCrudOptions returns modified options
1 parent 723966e commit bb25570

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/Commands/MakeEntityCommand.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use RonasIT\Support\Generators\TranslationsGenerator;
2020
use RonasIT\Support\Generators\SeederGenerator;
2121
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
22+
use UnexpectedValueException;
2223

2324
/**
2425
* @property ControllerGenerator $controllerGenerator
@@ -35,6 +36,10 @@
3536
*/
3637
class MakeEntityCommand extends Command
3738
{
39+
const CRUD_OPTIONS = [
40+
'C', 'R', 'U', 'D'
41+
];
42+
3843
/**
3944
* The name and signature of the console command.
4045
*
@@ -180,12 +185,8 @@ protected function classExists($path, $name)
180185

181186
protected function validateInput()
182187
{
183-
if ($this->option('only-api')) {
184-
$modelName = $this->argument('name');
185-
if (!$this->classExists('services', "{$modelName}Service")) {
186-
throw new ClassNotExistsException('Cannot create API without entity.');
187-
}
188-
}
188+
$this->validateOnlyApiOption();
189+
$this->validateCrudOptions();
189190
}
190191

191192
protected function generate()
@@ -233,7 +234,7 @@ protected function runGeneration($generator)
233234

234235
protected function getCrudOptions()
235236
{
236-
return $this->validateCrudOptions();
237+
return str_split($this->option('methods'));
237238
}
238239

239240
protected function getRelations()
@@ -260,14 +261,22 @@ protected function getFields()
260261

261262
protected function validateCrudOptions()
262263
{
263-
$crudOptions = str_split($this->option('methods'));
264+
$crudOptions = $this->getCrudOptions();
264265

265266
foreach ($crudOptions as $crudOption) {
266-
if (!in_array($crudOption, EntityGenerator::CRUD_OPTIONS)) {
267-
throw new \UnexpectedValueException("Invalid method {$crudOption}.");
267+
if (!in_array($crudOption, MakeEntityCommand::CRUD_OPTIONS)) {
268+
throw new UnexpectedValueException("Invalid method {$crudOption}.");
268269
}
269270
}
271+
}
270272

271-
return $crudOptions;
273+
protected function validateOnlyApiOption()
274+
{
275+
if ($this->option('only-api')) {
276+
$modelName = $this->argument('name');
277+
if (!$this->classExists('services', "{$modelName}Service")) {
278+
throw new ClassNotExistsException('Cannot create API without entity.');
279+
}
280+
}
272281
}
273282
}

src/Generators/EntityGenerator.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ abstract class EntityGenerator
1515
'boolean-required', 'boolean', 'timestamp-required', 'timestamp', 'json'
1616
];
1717

18-
const CRUD_OPTIONS = [
19-
'C', 'R', 'U', 'D'
20-
];
21-
2218
protected $paths = [];
2319
protected $model;
2420
protected $fields;

src/Generators/TestsGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ protected function generateExistedEntityFixture()
193193

194194
protected function isFixtureNeeded($type)
195195
{
196-
$firstLetter = strtoupper(substr($type, 0, 1));
196+
$firstLetter = strtoupper($type[0]);
197197

198198
return in_array($firstLetter, $this->crudOptions);
199199
}

0 commit comments

Comments
 (0)