Skip to content

Commit 4872afe

Browse files
committed
[RF] refactor to ValidationRule class
1 parent a2b46b3 commit 4872afe

File tree

4 files changed

+79
-12
lines changed

4 files changed

+79
-12
lines changed

src/Schemas/MigrationField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private function fillObject(string $param)
5757
/**
5858
* @return string
5959
*/
60-
public function getName()
60+
public function getField()
6161
{
6262
return $this->name;
6363
}

src/Schemas/ValidationRule.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas;
4+
5+
class ValidationRule
6+
{
7+
private $type;
8+
9+
private $field;
10+
11+
private $required = true;
12+
13+
public function __construct(MigrationField $migrationField)
14+
{
15+
if ($migrationField->isNullable()) {
16+
$this->required = false;
17+
}
18+
19+
$this->type = $migrationField->getType();
20+
$this->field = $migrationField->getField();
21+
}
22+
23+
public function generateRuleString(): ?string
24+
{
25+
if ($this->isRequired()) {
26+
return 'required';
27+
}
28+
29+
return null;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getField()
36+
{
37+
return $this->field;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getType()
44+
{
45+
return $this->type;
46+
}
47+
48+
/**
49+
* @return bool
50+
*/
51+
public function isRequired()
52+
{
53+
return $this->required;
54+
}
55+
}

src/Services/BackpackCrudRequestService.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
66
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
7+
use Webfactor\Laravel\Generators\Helper\ShortSyntaxArray;
8+
use Webfactor\Laravel\Generators\Schemas\ValidationRule;
79

810
class BackpackCrudRequestService extends ServiceAbstract implements ServiceInterface
911
{
1012
protected $relativeToBasePath = 'app/Http/Requests/Admin';
1113

12-
private $rules;
14+
private $rules = [];
1315

1416
public function call()
1517
{
@@ -18,34 +20,45 @@ public function call()
1820
]);
1921

2022
$this->addLatestFileToIdeStack();
21-
$this->fillRulesInGeneratedRequestFromSchema();
23+
24+
$this->setRulesFromSchema();
25+
$this->fillRulesInGeneratedRequest();
2226
}
2327

2428
public function getName(string $entity): string
2529
{
2630
return ucfirst($entity);
2731
}
2832

29-
private function fillRulesInGeneratedRequestFromSchema()
33+
private function fillRulesInGeneratedRequest()
3034
{
3135
$requestFile = end($this->command->filesToBeOpened);
3236

3337
$request = $this->filesystem->get($requestFile);
34-
$request = str_replace('__rules__', $this->getRulesFromSchema(), $request);
38+
$request = str_replace('__rules__', $this->getRulesAsString(), $request);
3539
$this->filesystem->put($requestFile, $request);
3640
}
3741

3842
/**
3943
* @return string
4044
*/
41-
private function getRulesFromSchema()
45+
private function setRulesFromSchema()
4246
{
4347
$this->command->schema->getStructure()->each(function ($field) {
44-
if (!$field->isNullable()) {
45-
$this->rules .= "'" . $field->getName() . "' => '" . $field->makeValidationRule() . "',\n";
46-
}
48+
array_push($this->rules, new ValidationRule($field));
4749
});
50+
}
51+
52+
private function getRulesAsString()
53+
{
54+
$rulesArray = [];
55+
56+
foreach ($this->rules as $validationRule) {
57+
if ($ruleString = $validationRule->generateRuleString()) {
58+
$rulesArray[$validationRule->getField()] = $ruleString;
59+
}
60+
}
4861

49-
return $this->rules;
62+
return ShortSyntaxArray::parse($rulesArray);
5063
}
5164
}

stubs/crud-request.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class DummyClassRequest extends FormRequest
2525
*/
2626
public function rules()
2727
{
28-
return [
29-
__rules__ ];
28+
return __rules__;
3029
}
3130

3231
/**

0 commit comments

Comments
 (0)