From 1c94e0b238994319850fbf74ee22564015a44ee6 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Mon, 9 Sep 2024 19:26:01 +0200 Subject: [PATCH 1/2] fix: cloning arraylist validator each time --- src/Http/Validator/ArrayList.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Http/Validator/ArrayList.php b/src/Http/Validator/ArrayList.php index 2f4835ce..6895989c 100644 --- a/src/Http/Validator/ArrayList.php +++ b/src/Http/Validator/ArrayList.php @@ -11,10 +11,15 @@ */ class ArrayList extends Validator { + /** + * @var ?Validator + */ + protected ?Validator $validator; + /** * @var Validator */ - protected Validator $validator; + protected Validator $cleanValidator; /** * @var int @@ -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; } @@ -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(); } @@ -78,7 +83,7 @@ public function isArray(): bool */ public function getType(): string { - return $this->validator->getType(); + return $this->cleanValidator->getType(); } /** @@ -88,7 +93,7 @@ public function getType(): string */ public function getValidator(): Validator { - return $this->validator; + return $this->cleanValidator; } /** @@ -101,6 +106,8 @@ public function getValidator(): Validator */ public function isValid(mixed $value): bool { + $this->validator = null; + if (!\is_array($value)) { return false; } @@ -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; From a2fa010db6fbb4d3b6d4be283c44d9ade3eb221f Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Tue, 10 Sep 2024 10:58:43 +0200 Subject: [PATCH 2/2] fix: reordering arraylist validator --- src/Http/Validator/ArrayList.php | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/Http/Validator/ArrayList.php b/src/Http/Validator/ArrayList.php index 6895989c..599859e7 100644 --- a/src/Http/Validator/ArrayList.php +++ b/src/Http/Validator/ArrayList.php @@ -11,15 +11,10 @@ */ class ArrayList extends Validator { - /** - * @var ?Validator - */ - protected ?Validator $validator; - /** * @var Validator */ - protected Validator $cleanValidator; + protected Validator $validator; /** * @var int @@ -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; } @@ -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(); } @@ -83,7 +78,7 @@ public function isArray(): bool */ public function getType(): string { - return $this->cleanValidator->getType(); + return $this->validator->getType(); } /** @@ -93,7 +88,7 @@ public function getType(): string */ public function getValidator(): Validator { - return $this->cleanValidator; + return $this->validator; } /** @@ -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; } }