Skip to content

Commit

Permalink
fix: reordering arraylist validator
Browse files Browse the repository at this point in the history
  • Loading branch information
byawitz committed Sep 10, 2024
1 parent 1c94e0b commit a2fa010
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/Http/Validator/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@
*/
class ArrayList extends Validator
{
/**
* @var ?Validator
*/
protected ?Validator $validator;

/**
* @var Validator
*/
protected Validator $cleanValidator;
protected Validator $validator;

/**
* @var int
Expand All @@ -36,7 +31,7 @@ class ArrayList extends Validator
*/
public function __construct(Validator $validator, int $length = 0)
{
$this->cleanValidator = $validator;
$this->validator = $validator;
$this->length = $length;
}

Expand All @@ -55,7 +50,7 @@ public function getDescription(): string
$msg .= ' no longer than ' . $this->length . ' items';
}

if ($this->validator != null && !empty($this->validator->getDescription())) {
if (!empty($this->validator->getDescription())) {
$msg .= ' and ' . $this->validator->getDescription();
}

Expand Down Expand Up @@ -83,7 +78,7 @@ public function isArray(): bool
*/
public function getType(): string
{
return $this->cleanValidator->getType();
return $this->validator->getType();
}

/**
Expand All @@ -93,7 +88,7 @@ public function getType(): string
*/
public function getValidator(): Validator
{
return $this->cleanValidator;
return $this->validator;
}

/**
Expand All @@ -106,24 +101,20 @@ public function getValidator(): Validator
*/
public function isValid(mixed $value): bool
{
$this->validator = null;

if (!\is_array($value)) {
return false;
}

if ($this->length && \count($value) > $this->length) {
return false;
}

$this->validator = clone $this->cleanValidator;

foreach ($value as $element) {
if (!$this->validator->isValid($element)) {
return false;
}
}

if ($this->length && \count($value) > $this->length) {
return false;
}

return true;
}
}

0 comments on commit a2fa010

Please sign in to comment.