From a75add2c222f7e3397b26dadd7166df399f35aee Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 17:37:03 +0530 Subject: [PATCH 1/4] Add multiple validator --- composer.lock | 80 +++++++++++----------- src/Validator/Multiple.php | 114 +++++++++++++++++++++++++++++++ tests/Validator/MultipleTest.php | 32 +++++++++ 3 files changed, 186 insertions(+), 40 deletions(-) create mode 100644 src/Validator/Multiple.php create mode 100644 tests/Validator/MultipleTest.php diff --git a/composer.lock b/composer.lock index 3d8bf18f..9bcef239 100644 --- a/composer.lock +++ b/composer.lock @@ -357,16 +357,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.17.1", + "version": "v4.18.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d" + "reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", - "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/1bcbb2179f97633e98bbbc87044ee2611c7d7999", + "reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999", "shasum": "" }, "require": { @@ -407,9 +407,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.17.1" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.18.0" }, - "time": "2023-08-13T19:53:39+00:00" + "time": "2023-12-10T21:03:43+00:00" }, { "name": "phar-io/manifest", @@ -724,16 +724,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.48", + "version": "1.10.50", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "087ed4b5f4a7a6e8f3bbdfbfe98ce5c181380bc6" + "reference": "06a98513ac72c03e8366b5a0cb00750b487032e4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/087ed4b5f4a7a6e8f3bbdfbfe98ce5c181380bc6", - "reference": "087ed4b5f4a7a6e8f3bbdfbfe98ce5c181380bc6", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/06a98513ac72c03e8366b5a0cb00750b487032e4", + "reference": "06a98513ac72c03e8366b5a0cb00750b487032e4", "shasum": "" }, "require": { @@ -782,27 +782,27 @@ "type": "tidelift" } ], - "time": "2023-12-08T14:34:28+00:00" + "time": "2023-12-13T10:59:42+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.29", + "version": "9.2.30", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "6a3a87ac2bbe33b25042753df8195ba4aa534c76" + "reference": "ca2bd87d2f9215904682a9cb9bb37dda98e76089" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/6a3a87ac2bbe33b25042753df8195ba4aa534c76", - "reference": "6a3a87ac2bbe33b25042753df8195ba4aa534c76", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ca2bd87d2f9215904682a9cb9bb37dda98e76089", + "reference": "ca2bd87d2f9215904682a9cb9bb37dda98e76089", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.15", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3", "phpunit/php-file-iterator": "^3.0.3", "phpunit/php-text-template": "^2.0.2", @@ -852,7 +852,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.29" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.30" }, "funding": [ { @@ -860,7 +860,7 @@ "type": "github" } ], - "time": "2023-09-19T04:57:46+00:00" + "time": "2023-12-22T06:47:57+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1601,20 +1601,20 @@ }, { "name": "sebastian/complexity", - "version": "2.0.2", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/complexity.git", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88" + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/739b35e53379900cc9ac327b2147867b8b6efd88", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88", + "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/25f207c40d62b8b7aa32f5ab026c53561964053a", + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a", "shasum": "" }, "require": { - "nikic/php-parser": "^4.7", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -1646,7 +1646,7 @@ "homepage": "https://github.com/sebastianbergmann/complexity", "support": { "issues": "https://github.com/sebastianbergmann/complexity/issues", - "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.2" + "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.3" }, "funding": [ { @@ -1654,7 +1654,7 @@ "type": "github" } ], - "time": "2020-10-26T15:52:27+00:00" + "time": "2023-12-22T06:19:30+00:00" }, { "name": "sebastian/diff", @@ -1928,20 +1928,20 @@ }, { "name": "sebastian/lines-of-code", - "version": "1.0.3", + "version": "1.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/lines-of-code.git", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc" + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/c1c2e997aa3146983ed888ad08b15470a2e22ecc", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc", + "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/e1e4a170560925c26d424b6a03aed157e7dcc5c5", + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5", "shasum": "" }, "require": { - "nikic/php-parser": "^4.6", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -1973,7 +1973,7 @@ "homepage": "https://github.com/sebastianbergmann/lines-of-code", "support": { "issues": "https://github.com/sebastianbergmann/lines-of-code/issues", - "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.3" + "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.4" }, "funding": [ { @@ -1981,7 +1981,7 @@ "type": "github" } ], - "time": "2020-11-28T06:42:11+00:00" + "time": "2023-12-22T06:20:34+00:00" }, { "name": "sebastian/object-enumerator", @@ -2324,16 +2324,16 @@ }, { "name": "seld/jsonlint", - "version": "1.10.0", + "version": "1.10.1", "source": { "type": "git", "url": "https://github.com/Seldaek/jsonlint.git", - "reference": "594fd6462aad8ecee0b45ca5045acea4776667f1" + "reference": "76d449a358ece77d6f1d6331c68453e657172202" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/594fd6462aad8ecee0b45ca5045acea4776667f1", - "reference": "594fd6462aad8ecee0b45ca5045acea4776667f1", + "url": "https://api.github.com/repos/Seldaek/jsonlint/zipball/76d449a358ece77d6f1d6331c68453e657172202", + "reference": "76d449a358ece77d6f1d6331c68453e657172202", "shasum": "" }, "require": { @@ -2360,7 +2360,7 @@ { "name": "Jordi Boggiano", "email": "j.boggiano@seld.be", - "homepage": "http://seld.be" + "homepage": "https://seld.be" } ], "description": "JSON Linter", @@ -2372,7 +2372,7 @@ ], "support": { "issues": "https://github.com/Seldaek/jsonlint/issues", - "source": "https://github.com/Seldaek/jsonlint/tree/1.10.0" + "source": "https://github.com/Seldaek/jsonlint/tree/1.10.1" }, "funding": [ { @@ -2384,7 +2384,7 @@ "type": "tidelift" } ], - "time": "2023-05-11T13:16:46+00:00" + "time": "2023-12-18T13:03:25+00:00" }, { "name": "symfony/console", @@ -3408,5 +3408,5 @@ "php": ">=8.0" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php new file mode 100644 index 00000000..5e629807 --- /dev/null +++ b/src/Validator/Multiple.php @@ -0,0 +1,114 @@ +addRule($rule); + } + } + + /** + * Add rule + * + * Add a new rule to the end of the rules containing array + * + * @param Validator $rule + * @return $this + */ + public function addRule(Validator $rule) + { + $this->rules[] = $rule; + + return $this; + } + + /** + * Get Description + * + * Returns validator description + * + * @return string + */ + public function getDescription(): string + { + $description = ''; + foreach ($this->rules as $key => $rule) { + $description .= ++$key . '. ' . $rule->getDescription() . " \n"; + } + + 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->rules as $rule) { /* @var $rule Validator */ + if (false === $rule->isValid($value)) { + return false; + } + } + + return true; + } + + /** + * Get Type + * + * Returns validator type. + * + * @return string + */ + public function getType(): string + { + return self::TYPE_MIXED; + } + + /** + * Is array + * + * Function will return true if object is array. + * + * @return bool + */ + public function isArray(): bool + { + return true; + } +} diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php new file mode 100644 index 00000000..d882d1bc --- /dev/null +++ b/tests/Validator/MultipleTest.php @@ -0,0 +1,32 @@ +validator = new Multiple(new Text(20), new URL()); + } + + public function testIsValid() + { + // 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('')); + } +} From 92854d401118ef615b069cc161ca54249ab6d041 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 18:59:33 +0530 Subject: [PATCH 2/4] Updated constructor to accept type --- src/Validator/Multiple.php | 19 +++++++++++-------- tests/Validator/MultipleTest.php | 4 +++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php index 5e629807..ea0811bb 100644 --- a/src/Validator/Multiple.php +++ b/src/Validator/Multiple.php @@ -18,6 +18,8 @@ class Multiple extends Validator */ protected $rules = []; + protected $type = self::TYPE_MIXED; + /** * Constructor * @@ -25,18 +27,19 @@ class Multiple extends Validator * * Example: * - * $multiple = new Multiple($validator1, $validator2, $validator3); + * $multiple = new Multiple([$validator1, $validator2]); + * $multiple = new Multiple([$validator1, $validator2, $validator3], SELF::TYPE_STRING); */ - public function __construct() + public function __construct(array $rules, ?string $type = null) { - // array of all method arguments - $rules = \func_get_args(); - foreach ($rules as $rule) { $this->addRule($rule); } - } + if ($type !== null) { + $this->type = $type; + } + } /** * Add rule * @@ -63,7 +66,7 @@ public function getDescription(): string { $description = ''; foreach ($this->rules as $key => $rule) { - $description .= ++$key . '. ' . $rule->getDescription() . " \n"; + $description .= ++$key . '. ' . $rule->getDescription() . " \\n"; } return $description; @@ -97,7 +100,7 @@ public function isValid(mixed $value): bool */ public function getType(): string { - return self::TYPE_MIXED; + return $this->type; } /** diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php index d882d1bc..e565f523 100644 --- a/tests/Validator/MultipleTest.php +++ b/tests/Validator/MultipleTest.php @@ -10,11 +10,13 @@ class MultipleTest extends TestCase public function setUp(): void { - $this->validator = new Multiple(new Text(20), new URL()); + $this->validator = new Multiple([new Text(20), new URL()]); } public function testIsValid() { + $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')); From 06323064234e3e6c76cb56870ac0fc7221c1a4ff Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 19:28:23 +0530 Subject: [PATCH 3/4] Addressed PR comments --- src/Validator/Multiple.php | 2 +- tests/Validator/MultipleTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php index ea0811bb..990c6517 100644 --- a/src/Validator/Multiple.php +++ b/src/Validator/Multiple.php @@ -66,7 +66,7 @@ public function getDescription(): string { $description = ''; foreach ($this->rules as $key => $rule) { - $description .= ++$key . '. ' . $rule->getDescription() . " \\n"; + $description .= ++$key . '. ' . $rule->getDescription() . " \n"; } return $description; diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php index e565f523..1dbff270 100644 --- a/tests/Validator/MultipleTest.php +++ b/tests/Validator/MultipleTest.php @@ -15,7 +15,7 @@ public function setUp(): void public function testIsValid() { - $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()); + $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')); From f1a2fee79bcf380b2adc0e8e919e487f501467d3 Mon Sep 17 00:00:00 2001 From: Khushboo Verma <43381712+vermakhushboo@users.noreply.github.com> Date: Tue, 26 Dec 2023 19:46:11 +0530 Subject: [PATCH 4/4] Remove null check from constructor --- src/Validator/Multiple.php | 8 +++----- tests/Validator/MultipleTest.php | 3 ++- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Validator/Multiple.php b/src/Validator/Multiple.php index 990c6517..bcc9205d 100644 --- a/src/Validator/Multiple.php +++ b/src/Validator/Multiple.php @@ -28,17 +28,15 @@ class Multiple extends Validator * Example: * * $multiple = new Multiple([$validator1, $validator2]); - * $multiple = new Multiple([$validator1, $validator2, $validator3], SELF::TYPE_STRING); + * $multiple = new Multiple([$validator1, $validator2, $validator3], self::TYPE_STRING); */ - public function __construct(array $rules, ?string $type = null) + public function __construct(array $rules, ?string $type = self::TYPE_MIXED) { foreach ($rules as $rule) { $this->addRule($rule); } - if ($type !== null) { - $this->type = $type; - } + $this->type = $type; } /** * Add rule diff --git a/tests/Validator/MultipleTest.php b/tests/Validator/MultipleTest.php index 1dbff270..8ef1ee59 100644 --- a/tests/Validator/MultipleTest.php +++ b/tests/Validator/MultipleTest.php @@ -10,11 +10,12 @@ class MultipleTest extends TestCase public function setUp(): void { - $this->validator = new Multiple([new Text(20), new URL()]); + $this->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