From 9f07728c7f8384c49fddb15ad19fd97cae8b4c3f Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Thu, 1 Aug 2024 13:10:41 +0530 Subject: [PATCH] Added AnyOf Validator --- src/Validator/AnyOf.php | 87 ++++++++++++++++++++++++++++++ tests/Validator/MultipleOfTest.php | 27 ++++++++++ tests/Validator/MultipleTest.php | 35 ------------ 3 files changed, 114 insertions(+), 35 deletions(-) create mode 100644 src/Validator/AnyOf.php create mode 100644 tests/Validator/MultipleOfTest.php delete mode 100644 tests/Validator/MultipleTest.php diff --git a/src/Validator/AnyOf.php b/src/Validator/AnyOf.php new file mode 100644 index 0000000..115f067 --- /dev/null +++ b/src/Validator/AnyOf.php @@ -0,0 +1,87 @@ + $validators + */ + public function __construct(protected array $validators, protected string $type = self::TYPE_MIXED) + { + } + + /** + * Get Description + * + * Returns validator description + * + * @return string + */ + public function getDescription(): string + { + if (!(\is_null($this->failedRule))) { + $description = $this->failedRule->getDescription(); + } else { + $description = $this->validators[0]->getDescription(); + } + + return $description; + } + + /** + * Is valid + * + * Validation will pass when all rules are valid if only one of the rules is invalid validation will fail. + * + * @param mixed $value + * @return bool + */ + public function isValid(mixed $value): bool + { + foreach ($this->validators as $rule) { + $valid = $rule->isValid($value); + + $this->failedRule = $rule; + + if ($valid) { + return true; + } + } + + return false; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } +} diff --git a/tests/Validator/MultipleOfTest.php b/tests/Validator/MultipleOfTest.php new file mode 100644 index 0000000..f5f51d4 --- /dev/null +++ b/tests/Validator/MultipleOfTest.php @@ -0,0 +1,27 @@ +assertTrue($vaidator->isValid($validTextValidUrl)); + $this->assertTrue($vaidator->isValid($validTextInvalidUrl)); + $this->assertTrue($vaidator->isValid($invalidTextValidUrl)); + $this->assertFalse($vaidator->isValid($invalidTextInvalidUrl)); + } +} diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php deleted file mode 100644 index 8ef1ee5..0000000 --- a/tests/Validator/MultipleTest.php +++ /dev/null @@ -1,35 +0,0 @@ -validator = new Multiple([new Text(20), new URL()], Multiple::TYPE_STRING); - } - - public function testIsValid() - { - $this->assertEquals('string', $this->validator->getType()); - $this->assertEquals("1. Value must be a valid string and at least 1 chars and no longer than 20 chars \n2. Value must be a valid URL \n", $this->validator->getDescription()); - - // Valid URL but invalid text length - $this->assertFalse($this->validator->isValid('http://example.com/very-long-url')); - - // Valid text within length, but invalid URL - $this->assertFalse($this->validator->isValid('hello world')); - - // Both conditions satisfied - $this->assertTrue($this->validator->isValid('http://example.com')); - $this->assertTrue($this->validator->isValid('https://google.com')); - - // Neither condition satisfied - $this->assertFalse($this->validator->isValid('example.com/hello-world')); - $this->assertFalse($this->validator->isValid('')); - } -}