Skip to content

Commit

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

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

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

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

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

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

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

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

if (!\is_array($value)) {
return false;
}
Expand All @@ -109,6 +116,8 @@ public function isValid(mixed $value): bool
return false;
}

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

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

0 comments on commit 1c94e0b

Please sign in to comment.