Skip to content

Commit

Permalink
Merge pull request #54 from RonasIT/33-remove-required-rule
Browse files Browse the repository at this point in the history
#33 Remove required rule for search request with required boolean field
  • Loading branch information
DenTray authored Nov 13, 2023
2 parents 3854fe1 + e9db24b commit e6adec0
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions src/Generators/RequestsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setRelations($relations)
return $this;
}

public function generate()
public function generate(): void
{
if (in_array('R', $this->crudOptions)) {
$this->createRequest(
Expand All @@ -48,20 +48,20 @@ public function generate()
$this->createRequest(
self::CREATE_METHOD,
false,
$this->getValidationParameters($this->fields, true)
$this->getCreateValidationParameters()
);
}

if (in_array('U', $this->crudOptions)) {
$this->createRequest(
self::UPDATE_METHOD,
true,
$this->getValidationParameters($this->fields, false)
$this->getUpdateValidationParameters()
);
}
}

protected function createRequest($method, $needToValidate = true, $parameters = [])
protected function createRequest($method, $needToValidate = true, $parameters = []): void
{
$requestsFolder = $this->getPluralName($this->model);
$modelName = $this->getEntityName($method);
Expand All @@ -81,7 +81,7 @@ protected function createRequest($method, $needToValidate = true, $parameters =
event(new SuccessCreateMessage("Created a new Request: {$method}{$modelName}Request"));
}

protected function getGetValidationParameters()
protected function getGetValidationParameters(): array
{
$parameters['array'] = ['with'];

Expand All @@ -90,50 +90,72 @@ protected function getGetValidationParameters()
return $this->getValidationParameters($parameters, true);
}

protected function getSearchValidationParameters()
protected function getCreateValidationParameters(): array
{
$parameters = Arr::except($this->fields, 'boolean-required');

if (!empty($this->fields['boolean-required'])) {
$parameters['boolean-present'] = $this->fields['boolean-required'];
}

return $this->getValidationParameters($parameters, true);
}

protected function getUpdateValidationParameters(): array
{
$parameters = Arr::except($this->fields, 'boolean-required');

if (!empty($this->fields['boolean-required'])) {
$parameters['boolean'] = array_merge($parameters['boolean'], $this->fields['boolean-required']);
}

return $this->getValidationParameters($parameters, false);
}

protected function getSearchValidationParameters(): array
{
$parameters = Arr::except($this->fields, [
'timestamp', 'timestamp-required', 'string-required', 'integer-required'
'timestamp', 'timestamp-required', 'string-required', 'integer-required', 'boolean-required'
]);

$parameters['boolean'] = array_merge($this->fields['boolean-required'], ['desc']);

$parameters['integer'] = array_merge($this->fields['integer'], [
'page', 'per_page', 'all',
]);

$parameters['array'] = ['with'];

$parameters['boolean'] = ['desc'];

$parameters['string'] = ['order_by'];

$parameters['string-nullable'] = ['query'];

$parameters['string-required'] = ['with.*'];

return $this->getValidationParameters($parameters, true);
return $this->getValidationParameters($parameters, false);
}

public function getValidationParameters($parameters, $requiredAvailable)
public function getValidationParameters($parameters, $requiredAvailable): array
{
$result = [];

foreach ($parameters as $type => $parameterNames) {
$isRequired = Str::contains($type, 'required');
$isNullable = Str::contains($type, 'nullable');
$isPresent = Str::contains($type, 'present');
$type = head(explode('-', $type));

foreach ($parameterNames as $name) {
$required = $isRequired && $requiredAvailable;
$nullable = $isNullable;

$result[] = $this->getRules($name, $type, $required, $nullable);
$result[] = $this->getRules($name, $type, $required, $isNullable, $isPresent);
}
}

return $result;
}

protected function getRules($name, $type, $required, $nullable)
protected function getRules($name, $type, $required, $nullable, $present): array
{
$replaces = [
'timestamp' => 'date',
Expand Down Expand Up @@ -161,13 +183,18 @@ protected function getRules($name, $type, $required, $nullable)
$rules[] = 'nullable';
}

if ($present) {
$rules[] = 'present';
}

return [
'name' => $name,
'rules' => $rules
];
}

private function getEntityName($method) {
private function getEntityName($method): string
{
if ($method === self::SEARCH_METHOD) {
return Str::plural($this->model);
}
Expand Down

0 comments on commit e6adec0

Please sign in to comment.