ValidatorCommand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Collection;
  5. use Prettus\Repository\Generators\FileAlreadyExistsException;
  6. use Prettus\Repository\Generators\ValidatorGenerator;
  7. use Symfony\Component\Console\Input\InputArgument;
  8. use Symfony\Component\Console\Input\InputOption;
  9. /**
  10. * Class ValidatorCommand
  11. * @package Prettus\Repository\Generators\Commands
  12. */
  13. class ValidatorCommand extends Command
  14. {
  15. /**
  16. * The name of command.
  17. *
  18. * @var string
  19. */
  20. protected $name = 'make:validator';
  21. /**
  22. * The description of command.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'Create a new validator.';
  27. /**
  28. * The type of class being generated.
  29. *
  30. * @var string
  31. */
  32. protected $type = 'Validator';
  33. /**
  34. * Execute the command.
  35. *
  36. * @return void
  37. * @see fire()
  38. */
  39. public function handle()
  40. {
  41. $this->laravel->call([$this, 'fire'], func_get_args());
  42. }
  43. /**
  44. * Execute the command.
  45. *
  46. * @return void
  47. */
  48. public function fire()
  49. {
  50. try {
  51. (new ValidatorGenerator([
  52. 'name' => $this->argument('name'),
  53. 'rules' => $this->option('rules'),
  54. 'force' => $this->option('force'),
  55. ]))->run();
  56. $this->info("Validator created successfully.");
  57. } catch (FileAlreadyExistsException $e) {
  58. $this->error($this->type . ' already exists!');
  59. return false;
  60. }
  61. }
  62. /**
  63. * The array of command arguments.
  64. *
  65. * @return array
  66. */
  67. public function getArguments()
  68. {
  69. return [
  70. [
  71. 'name',
  72. InputArgument::REQUIRED,
  73. 'The name of model for which the validator is being generated.',
  74. null
  75. ],
  76. ];
  77. }
  78. /**
  79. * The array of command options.
  80. *
  81. * @return array
  82. */
  83. public function getOptions()
  84. {
  85. return [
  86. [
  87. 'rules',
  88. null,
  89. InputOption::VALUE_OPTIONAL,
  90. 'The rules of validation attributes.',
  91. null
  92. ],
  93. [
  94. 'force',
  95. 'f',
  96. InputOption::VALUE_NONE,
  97. 'Force the creation if file already exists.',
  98. null
  99. ],
  100. ];
  101. }
  102. }