Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Feb 10, 2024
1 parent c015068 commit 8f03781
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace App\Containers\AppSection\Authentication\Classes;

use App\Containers\AppSection\Authentication\Values\IncomingLoginField;
use App\Containers\AppSection\Authentication\Values\LoginField;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class LoginFieldParser
{
Expand Down Expand Up @@ -113,35 +113,34 @@ public static function mergeValidationRules(array $rules): array
}

if (1 === count($elements)) {
$fieldName = array_key_first($elements);
// $fieldRules = self::getUniqueRules($elements[$fieldName]);
$loginField = new LoginField($fieldName, $elements[$fieldName]);
$fieldRules = $loginField->rules()[0];
$key = array_key_first($elements);
$fieldRules = self::getUniqueRules($elements[$key]);

if (!str_contains($fieldRules, 'required')) {
if (!Str::contains($fieldRules, 'required')) {
$fieldRules = 'required|' . $fieldRules;
}

$rules["{$prefix}{$fieldName}"] = self::trimPipes($fieldRules);
$fieldName = "{$prefix}{$key}";
$rules[$fieldName] = self::trimPipes($fieldRules);

return $rules;
}

foreach ($elements as $fieldName => $fieldRules) {
$currentLoginField = new LoginField($fieldName, $elements[$fieldName]);
$otherFields = Arr::except($elements, $currentLoginField->name());
$otherFieldsNames = array_keys($otherFields);
$otherFieldsNamesWithPrefix = preg_filter('/^/', $prefix, $otherFieldsNames);
$otherFieldsNamesConcatenated = implode(',', $otherFieldsNamesWithPrefix);
$fieldRules = $currentLoginField->rules()[0];
if (str_contains($fieldRules, 'required')) {
foreach ($elements as $key => $fieldRules) {
// build all other login fields together
$otherLoginFields = Arr::except($elements, $key);
$otherLoginFields = array_keys($otherLoginFields);
$otherLoginFields = preg_filter('/^/', $prefix, $otherLoginFields);
$otherLoginFields = implode(',', $otherLoginFields);
$fieldRules = self::getUniqueRules($fieldRules);
if (Str::contains($fieldRules, 'required')) {
$fieldRules = str_replace('required', '', $fieldRules);
}
$fieldNameWithPrefix = "{$prefix}{$currentLoginField->name()}";
$fieldRules = "required_without_all:{$otherFieldsNamesConcatenated}|{$fieldRules}";
$fieldName = "{$prefix}{$key}";
$fieldRules = "required_without_all:{$otherLoginFields}|{$fieldRules}";
$fieldRules = self::trimPipes($fieldRules);

$rules[$fieldNameWithPrefix] = $fieldRules;
$rules[$fieldName] = $fieldRules;
}

return $rules;
Expand Down
37 changes: 3 additions & 34 deletions app/Containers/AppSection/Authentication/Values/LoginField.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace App\Containers\AppSection\Authentication\Values;

use App\Ship\Parents\Values\Value as ParentValue;
use Illuminate\Validation\ValidationRuleParser;

final class LoginField extends ParentValue implements \Stringable
{
public function __construct(
private string $name,
private array $rules,
private readonly string $name,
private readonly array $rules,
) {
}

Expand All @@ -25,41 +24,11 @@ public function toArray(): array

public function rules(): array
{
return [$this->uniqueRules()];
return $this->rules;
}

public function name(): string
{
return $this->name;
}

private function uniqueRules(): string
{
ValidationRuleParser::parse($this->rules);

return implode('|', array_unique(explode('|', implode('|', $this->rules))));
}

private function isRequired(): bool
{
return !str_contains($this->uniqueRules(), 'required');
}

public function makeRequired(bool $required = true): self
{
if ($required && $this->isRequired()) {
$this->rules[] = 'required';
}

return $this;
}

private function trimPipes(): self
{
$instance = clone $this;

$instance->rules = [trim(str_replace('||', '|', $instance->rules), '|')];

return $instance;
}
}

0 comments on commit 8f03781

Please sign in to comment.