Skip to content

Commit

Permalink
Merge pull request #1664 from hungthai1401/implicit-rule
Browse files Browse the repository at this point in the history
Implicit rule
  • Loading branch information
dcblogdev committed Oct 26, 2023
2 parents 1a7265e + 40b80fb commit 47d29d6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/Commands/RuleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class RuleMakeCommand extends GeneratorCommand
{
Expand Down Expand Up @@ -53,14 +54,30 @@ protected function getArguments()
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['implicit', 'i', InputOption::VALUE_NONE, 'Generate an implicit rule'],
];
}

/**
* @return mixed
*/
protected function getTemplateContents()
{
$module = $this->laravel['modules']->findOrFail($this->getModuleName());

return (new Stub('/rule.stub', [
$stub = $this->option('implicit')
? '/rule.implicit.stub'
: '/rule.stub';

return (new Stub($stub, [
'NAMESPACE' => $this->getClassNamespace($module),
'CLASS' => $this->getFileName(),
]))->render();
Expand Down
22 changes: 22 additions & 0 deletions src/Commands/stubs/rule.implicit.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace $NAMESPACE$;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class $CLASS$ implements ValidationRule
{
/**
* Indicates whether the rule should be implicit.
*/
public bool $implicit = true;

/**
* Run the validation rule.
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
//
}
}
12 changes: 12 additions & 0 deletions tests/Commands/RuleMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public function it_makes_rule()
$this->assertSame(0, $code);
}

/** @test */
public function it_makes_implicit_rule()
{
$code = $this->artisan('module:make-rule', ['name' => 'ImplicitUniqueRule', 'module' => 'Blog', '--implicit' => true]);

$ruleFile = $this->modulePath . '/Rules/ImplicitUniqueRule.php';

$this->assertTrue(is_file($ruleFile), 'Rule file was not created.');
$this->assertMatchesSnapshot($this->finder->get($ruleFile));
$this->assertSame(0, $code);
}

/** @test */
public function it_can_change_the_default_namespace()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Modules\Blog\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class ImplicitUniqueRule implements ValidationRule
{
/**
* Indicates whether the rule should be implicit.
*/
public bool $implicit = true;

/**
* Run the validation rule.
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
//
}
}

0 comments on commit 47d29d6

Please sign in to comment.